This commit is contained in:
Nathnael
2026-08-25 13:05:55 +00:00
parent ac325a6282
commit 08aba3fb4d

View File

@@ -25,6 +25,22 @@ interface BasePosition {
isDelegate: boolean;
}
/**
* `x-delegated-position-id` names the DELEGATOR's position and the API matches
* it against `position.id`. This used to be set for every selection, carrying
* `employeePositionId` — so it matched nothing, and because the API resolves
* the delegation header *before* the current-position one, it swallowed the
* whole selection: every request ran as the employee's first position no
* matter what the picker said. Only a genuine delegate carries it.
*/
const applyDelegationCookie = (position?: BasePosition | null) => {
if (position?.isDelegate && position.id) {
Cookies.set("delegatedPositionId", position.id);
return;
}
Cookies.remove("delegatedPositionId");
};
export const PositionSelect = () => {
const {
unFilteredUserDetails,
@@ -63,26 +79,32 @@ export const PositionSelect = () => {
setSelectedPositionId(currentPosition.employeePositionId);
}
if (Cookies.get("current-position-id") !== currentPosition.id) {
Cookies.set("current-position-id", currentPosition.id);
}
// The API matches x-current-position-id against employeePositionId, NOT
// position.id — writing the latter never matches, so the guard silently
// falls back to the employee's first position and the picker does nothing.
// (useAuthUser already self-heals this cookie for the same reason.)
if (
currentPosition.employeePositionId &&
Cookies.get("delegatedPositionId") !== currentPosition.employeePositionId
Cookies.get("current-position-id") !== currentPosition.employeePositionId
) {
Cookies.set("delegatedPositionId", currentPosition.employeePositionId);
Cookies.set("current-position-id", currentPosition.employeePositionId);
}
applyDelegationCookie(currentPosition);
}, [currentPosition, isLoading, selectedPositionId, setSelectedPositionId]);
if (isLoading || selectablePositions.length === 0) return null;
const handleChange = (value: string) => {
setSelectedPositionId(value);
const selected = selectablePositions.find((pos) => pos.id === value);
Cookies.set("current-position-id", value);
Cookies.set("delegatedPositionId", selected?.employeePositionId || "");
// Both the picker state and the cookie key off employeePositionId — the
// dropdown's own value is position.id, which the API does not match on.
setSelectedPositionId(selected?.employeePositionId ?? value);
if (selected?.employeePositionId) {
Cookies.set("current-position-id", selected.employeePositionId);
}
applyDelegationCookie(selected);
// Invalidate relevant queries
[