import { useLHSession } from '@components/Contexts/LHSessionContext' import { useOrg } from '@components/Contexts/OrgContext' import { getAPIUrl } from '@services/config/config' import { linkUserToUserGroup, unLinkUserToUserGroup } from '@services/usergroups/usergroups' import { swrFetcher } from '@services/utils/ts/requests' import { Check, Plus, X } from 'lucide-react' import React from 'react' import toast from 'react-hot-toast' import useSWR, { mutate } from 'swr' type ManageUsersProps = { usergroup_id: any } function ManageUsers(props: ManageUsersProps) { const org = useOrg() as any const session = useLHSession() as any const access_token = session?.data?.tokens?.access_token; const { data: OrgUsers } = useSWR( org ? `${getAPIUrl()}orgs/${org.id}/users` : null, (url) => swrFetcher(url, access_token) ) const { data: UGusers } = useSWR( org ? `${getAPIUrl()}usergroups/${props.usergroup_id}/users` : null, (url) => swrFetcher(url, access_token) ) const isUserPartOfGroup = (user_id: any) => { if (UGusers) { return UGusers.some((user: any) => user.id === user_id) } return false } const handleLinkUser = async (user_id: any) => { const res = await linkUserToUserGroup(props.usergroup_id, user_id, access_token) if (res.status === 200) { toast.success('User linked successfully') mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`) } else { toast.error('Error ' + res.status + ': ' + res.data.detail) } } const handleUnlinkUser = async (user_id: any) => { const res = await unLinkUserToUserGroup(props.usergroup_id, user_id, access_token) if (res.status === 200) { toast.success('User unlinked successfully') mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`) } else { toast.error('Error ' + res.status + ': ' + res.data.detail) } } return (
<> {OrgUsers?.map((user: any) => ( ))}
User Linked Actions
{user.user.first_name + ' ' + user.user.last_name} @{user.user.username} {isUserPartOfGroup(user.user.id) ?
Linked
:
Not linked
}
) } export default ManageUsers