From 10b3619ae905f3d64b4f1d0c94cf27235dacf5ab Mon Sep 17 00:00:00 2001 From: jenny-s51 Date: Fri, 16 Apr 2021 16:15:02 -0400 Subject: [PATCH] wip add groups to user form --- src/user/JoinGroupDialog.tsx | 88 +++++++++++++-------- src/user/UserForm.tsx | 144 ++++++++++++++++++++++++++++++++++- src/user/messages.json | 1 + src/user/user-section.css | 14 ++++ 4 files changed, 216 insertions(+), 31 deletions(-) diff --git a/src/user/JoinGroupDialog.tsx b/src/user/JoinGroupDialog.tsx index 2802acd5bd..8f417028e2 100644 --- a/src/user/JoinGroupDialog.tsx +++ b/src/user/JoinGroupDialog.tsx @@ -31,8 +31,9 @@ export type JoinGroupDialogProps = { open: boolean; toggleDialog: () => void; onClose: () => void; - username: string; + username?: string; onConfirm: (newGroups: Group[]) => void; + chips?: any; }; type Group = GroupRepresentation & { @@ -45,6 +46,7 @@ export const JoinGroupDialog = ({ toggleDialog, onConfirm, username, + chips, }: JoinGroupDialogProps) => { const { t } = useTranslation("roles"); const adminClient = useAdminClient(); @@ -61,41 +63,67 @@ export const JoinGroupDialog = ({ const { id } = useParams<{ id: string }>(); - useEffect( - () => - asyncStateFetch( - async () => { - const existingUserGroups = await adminClient.users.listGroups({ id }); - const allGroups = await adminClient.groups.find(); + if (id) { + useEffect( + () => + asyncStateFetch( + async () => { + const existingUserGroups = await adminClient.users.listGroups({ + id, + }); + const allGroups = await adminClient.groups.find(); - if (groupId) { - const group = await adminClient.groups.findOne({ id: groupId }); - return { group, groups: group.subGroups! }; - } else { - return { - groups: _.differenceBy(allGroups, existingUserGroups, "id"), - }; - } - }, - async ({ group: selectedGroup, groups }) => { - if (selectedGroup) { - setNavigation([...navigation, selectedGroup]); - } + if (groupId) { + const group = await adminClient.groups.findOne({ id: groupId }); + return { group, groups: group.subGroups! }; + } else { + return { + groups: _.differenceBy(allGroups, existingUserGroups, "id"), + }; + } + }, + async ({ group: selectedGroup, groups }) => { + if (selectedGroup) { + setNavigation([...navigation, selectedGroup]); + } - groups.forEach((group: Group) => { - group.checked = !!selectedRows.find((r) => r.id === group.id); - }); - setGroups(groups); - }, - errorHandler - ), - [groupId] - ); + groups.forEach((group: Group) => { + group.checked = !!selectedRows.find((r) => r.id === group.id); + }); + setGroups(groups); + }, + errorHandler + ), + [groupId] + ); + } + + else if (!id) { + useEffect(() => { + return asyncStateFetch( + () => { + // adminClient.groups.find(); + return Promise.resolve(adminClient.groups.find()); + }, + (groups) => { + console.log(groups); + console.log("potato", chips) + // setGroups(groups.filter((item) => item.name !== chips)); + setGroups([...groups.filter((row) => !chips.includes(row.name))]); + + // setupForm(realm); + }, + errorHandler + ); + }, []); +} return ( ; @@ -45,6 +51,10 @@ export const UserForm = ({ const watchUsernameInput = watch("username"); const [timestamp, setTimestamp] = useState(null); + const [chips, setChips] = useState<(string | undefined)[]>([]); + + const [list, setList] = useState(false); + const [open, setOpen] = useState(false); useEffect(() => { if (editMode) { @@ -56,7 +66,7 @@ export const UserForm = ({ handleError ); } - }, []); + }, [chips]); const setupForm = (user: UserRepresentation) => { reset(); @@ -90,6 +100,82 @@ export const UserForm = ({ setRequiredUserActionsDropdownOpen(false); }; + const deleteItem = (id: string) => { + const copyOfChips = chips; + console.log("care", id); + + setChips(copyOfChips.filter((item) => item !== id)); + + // const index = copyOfChips.indexOf(id); + // if (index !== -1) { + // copyOfChips.splice(index, 1); + // setChips(copyOfChips); + // } + }; + + const deleteCategory = () => { + setChips([]); + }; + + const addChips = async (groups: GroupRepresentation[]): Promise => { + const newGroups = groups; + + const newGroupNames: (string | undefined)[] = newGroups!.map( + (item) => item.name + ); + console.log(newGroupNames); + setChips([...chips!, ...newGroupNames]); + + console.log("newGroups", newGroups) + + newGroups.forEach(async (group) => { + // try { + await adminClient.users.addToGroup({ + id: id, + groupId: group.id!, + }); + // refresh(); + // addAlert(t("users:addedGroupMembership"), AlertVariant.success); + // } catch (error) { + // // addAlert( + // // t("users:addedGroupMembershipError", { error }), + // // AlertVariant.danger + // // ); + // } + }); + + console.log("beep beep", adminClient.users.listGroups()) + + }; + + console.log(watchUsernameInput) + + + const addGroups = async (groups: GroupRepresentation[]): Promise => { + const newGroups = groups; + + newGroups.forEach(async (group) => { + try { + await adminClient.users.addToGroup({ + id: id, + groupId: group.id!, + }); + setList(true); + // refresh(); + // addAlert(t("users:addedGroupMembership"), AlertVariant.success); + } catch (error) { + // addAlert( + // t("users:addedGroupMembershipError", { error }), + // AlertVariant.danger + // ); + } + }); + }; + + const toggleModal = () => { + setOpen(!open); + }; + return ( + {open && ( + setOpen(!open)} + onConfirm={editMode ? addGroups : addChips} + toggleDialog={() => toggleModal()} + chips={chips} + /> + )} {editMode ? ( <> + {!editMode && ( + + } + > + ( + <> + + + {chips.map((currentChip) => ( + deleteItem(currentChip!)} + > + {currentChip} + + ))} + + + + + )} + /> + + )} +