2021-08-26 08:39:35 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
2021-08-10 11:18:48 +00:00
|
|
|
import React, { createContext, FunctionComponent, useState } from "react";
|
2021-07-21 09:30:18 +00:00
|
|
|
import useRequiredContext from "../utils/useRequiredContext";
|
2021-03-16 12:37:57 +00:00
|
|
|
|
|
|
|
type SubGroupsProps = {
|
|
|
|
subGroups: GroupRepresentation[];
|
|
|
|
setSubGroups: (group: GroupRepresentation[]) => void;
|
|
|
|
clear: () => void;
|
|
|
|
remove: (group: GroupRepresentation) => void;
|
|
|
|
currentGroup: () => GroupRepresentation;
|
|
|
|
};
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
const SubGroupContext = createContext<SubGroupsProps | undefined>(undefined);
|
2021-03-16 12:37:57 +00:00
|
|
|
|
2021-08-10 11:18:48 +00:00
|
|
|
export const SubGroups: FunctionComponent = ({ children }) => {
|
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 (
|
|
|
|
<SubGroupContext.Provider
|
|
|
|
value={{ subGroups, setSubGroups, clear, remove, currentGroup }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</SubGroupContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
export const useSubGroups = () => useRequiredContext(SubGroupContext);
|