2021-08-26 08:39:35 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
2023-01-20 14:28:32 +00:00
|
|
|
import { PropsWithChildren, useState } from "react";
|
2023-03-07 09:29:40 +00:00
|
|
|
import { createNamedContext, useRequiredContext } from "ui-shared";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
|
|
|
type SubGroupsProps = {
|
|
|
|
subGroups: GroupRepresentation[];
|
|
|
|
setSubGroups: (group: GroupRepresentation[]) => void;
|
|
|
|
clear: () => void;
|
|
|
|
remove: (group: GroupRepresentation) => void;
|
2022-02-21 14:58:28 +00:00
|
|
|
currentGroup: () => GroupRepresentation | undefined;
|
2021-03-16 12:37:57 +00:00
|
|
|
};
|
|
|
|
|
2022-08-03 13:15:04 +00:00
|
|
|
const SubGroupsContext = createNamedContext<SubGroupsProps | undefined>(
|
|
|
|
"SubGroupsContext",
|
|
|
|
undefined
|
|
|
|
);
|
2021-03-16 12:37:57 +00:00
|
|
|
|
2023-02-13 07:18:16 +00:00
|
|
|
export const SubGroups = ({ children }: PropsWithChildren) => {
|
2021-03-16 12:37:57 +00:00
|
|
|
const [subGroups, setSubGroups] = useState<GroupRepresentation[]>([]);
|
|
|
|
|
|
|
|
const clear = () => setSubGroups([]);
|
|
|
|
const remove = (group: GroupRepresentation) =>
|
|
|
|
setSubGroups(
|
2021-05-18 12:25:46 +00:00
|
|
|
subGroups.slice(0, subGroups.findIndex((g) => g.id === group.id) + 1)
|
2021-03-16 12:37:57 +00:00
|
|
|
);
|
|
|
|
const currentGroup = () => subGroups[subGroups.length - 1];
|
|
|
|
return (
|
2022-08-03 13:15:04 +00:00
|
|
|
<SubGroupsContext.Provider
|
2021-03-16 12:37:57 +00:00
|
|
|
value={{ subGroups, setSubGroups, clear, remove, currentGroup }}
|
|
|
|
>
|
|
|
|
{children}
|
2022-08-03 13:15:04 +00:00
|
|
|
</SubGroupsContext.Provider>
|
2021-03-16 12:37:57 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-03 13:15:04 +00:00
|
|
|
export const useSubGroups = () => useRequiredContext(SubGroupsContext);
|