50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
// The create-agent form's permissions logic, kept out of the component so
|
|
// `npm test` can run it.
|
|
|
|
/** `selected` with `value` added, or removed when it is there. */
|
|
export function togglePermission(selected: string[], value: string): string[] {
|
|
return selected.includes(value)
|
|
? selected.filter((p) => p !== value)
|
|
: [...selected, value];
|
|
}
|
|
|
|
/**
|
|
* The `permissions` to send with a create: `undefined` while `selected` is
|
|
* the default set (in any order), so the agent's key stays unset and follows
|
|
* the default; otherwise the list, which may be empty.
|
|
*/
|
|
export function requestPermissions(
|
|
selected: string[],
|
|
defaults: string[],
|
|
): string[] | undefined {
|
|
const same =
|
|
selected.length === defaults.length &&
|
|
selected.every((p) => defaults.includes(p));
|
|
return same ? undefined : selected;
|
|
}
|
|
|
|
/**
|
|
* The form's starting selection: `defaults` plus each of `extra` the
|
|
* catalogue lists (`all`), so a name the controller does not know is not
|
|
* sent.
|
|
*/
|
|
export function initialSelection(
|
|
all: string[],
|
|
defaults: string[],
|
|
extra: string[],
|
|
): string[] {
|
|
return [
|
|
...defaults,
|
|
...extra.filter((p) => all.includes(p) && !defaults.includes(p)),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* The create request's internet field: `unrestricted_internet: true` while
|
|
* the box is ticked, nothing otherwise, as the server writes `false` for it.
|
|
*/
|
|
export function requestInternet(ticked: boolean): {
|
|
unrestricted_internet?: true;
|
|
} {
|
|
return ticked ? { unrestricted_internet: true } : {};
|
|
}
|