feat: usergroups bugs

This commit is contained in:
swve 2024-06-13 22:42:30 +01:00
parent ccabc12b3a
commit 866111aa4c
3 changed files with 61 additions and 61 deletions

View file

@ -17,9 +17,10 @@ function OrgInviteCodeGenerate(props: OrgInviteCodeGenerateProps) {
const session = useLHSession() as any const session = useLHSession() as any
const access_token = session?.data?.tokens?.access_token; const access_token = session?.data?.tokens?.access_token;
const [usergroup_id, setUsergroup_id] = React.useState(0); const [usergroup_id, setUsergroup_id] = React.useState(0);
const { data: usergroups } = useSWR( const { data: usergroups } = useSWR(
org ? `${getAPIUrl()}usergroups/org/${org.id}` : null, org ? `${getAPIUrl()}usergroups/org/${org.id}` : null,
swrFetcher (url) => swrFetcher(url, access_token)
) )
async function createInviteWithUserGroup() { async function createInviteWithUserGroup() {

View file

@ -4,6 +4,7 @@ import FormLayout, {
Flex, Flex,
FormField, FormField,
FormLabel, FormLabel,
FormLabelAndMessage,
FormMessage, FormMessage,
Input, Input,
} from '@components/StyledElements/Form/Form' } from '@components/StyledElements/Form/Form'
@ -15,37 +16,37 @@ import { createUserGroup } from '@services/usergroups/usergroups'
import { mutate } from 'swr' import { mutate } from 'swr'
import { getAPIUrl } from '@services/config/config' import { getAPIUrl } from '@services/config/config'
import { useLHSession } from '@components/Contexts/LHSessionContext' import { useLHSession } from '@components/Contexts/LHSessionContext'
import { useFormik } from 'formik'
type AddUserGroupProps = { type AddUserGroupProps = {
setCreateUserGroupModal: any setCreateUserGroupModal: any
} }
const validate = (values: any) => {
const errors: any = {}
if (!values.name) {
errors.name = 'Name is Required'
}
return errors
}
function AddUserGroup(props: AddUserGroupProps) { function AddUserGroup(props: AddUserGroupProps) {
const org = useOrg() as any; const org = useOrg() as any;
const session = useLHSession() as any const session = useLHSession() as any
const access_token = session?.data?.tokens?.access_token; const access_token = session?.data?.tokens?.access_token;
const [userGroupName, setUserGroupName] = React.useState('')
const [userGroupDescription, setUserGroupDescription] = React.useState('')
const [isSubmitting, setIsSubmitting] = React.useState(false) const [isSubmitting, setIsSubmitting] = React.useState(false)
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => { const formik = useFormik({
setUserGroupName(event.target.value) initialValues: {
} name: '',
description: '',
const handleDescriptionChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setUserGroupDescription(event.target.value)
}
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
setIsSubmitting(true)
const obj = {
name: userGroupName,
description: userGroupDescription,
org_id: org.id org_id: org.id
} },
const res = await createUserGroup(obj, access_token) validate,
onSubmit: async (values) => {
setIsSubmitting(true)
const res = await createUserGroup(values, access_token)
if (res.status == 200) { if (res.status == 200) {
setIsSubmitting(false) setIsSubmitting(false)
mutate(`${getAPIUrl()}usergroups/org/${org.id}`) mutate(`${getAPIUrl()}usergroups/org/${org.id}`)
@ -54,47 +55,45 @@ function AddUserGroup(props: AddUserGroupProps) {
} else { } else {
setIsSubmitting(false) setIsSubmitting(false)
} }
} },
})
return ( return (
<FormLayout onSubmit={handleSubmit}> <FormLayout onSubmit={formik.handleSubmit}>
<FormField name="name"> <FormField name="name">
<Flex css={{ alignItems: 'baseline', justifyContent: 'space-between' }}> <FormLabelAndMessage
<FormLabel>Name</FormLabel> label="Name"
<FormMessage match="valueMissing"> message={formik.errors.name}
Please provide a ug name />
</FormMessage>
</Flex>
<Form.Control asChild> <Form.Control asChild>
<Input onChange={handleNameChange} type="text" required /> <Input
onChange={formik.handleChange}
value={formik.values.name}
type="name"
required
/>
</Form.Control> </Form.Control>
</FormField> </FormField>
<FormField name="description"> <FormField name="description">
<Flex css={{ alignItems: 'baseline', justifyContent: 'space-between' }}> <FormLabelAndMessage
<FormLabel>Description</FormLabel> label="Description"
<FormMessage match="valueMissing"> message={formik.errors.description}
Please provide a ug description />
</FormMessage>
</Flex>
<Form.Control asChild> <Form.Control asChild>
<Input onChange={handleDescriptionChange} type="text" required /> <Input
onChange={formik.handleChange}
value={formik.values.description}
type="description"
/>
</Form.Control> </Form.Control>
</FormField> </FormField>
<Flex css={{ marginTop: 25, justifyContent: 'flex-end' }}> <div className="flex py-4">
<Form.Submit asChild> <Form.Submit asChild>
<ButtonBlack type="submit" css={{ marginTop: 10 }}> <button className="w-full bg-black text-white font-bold text-center p-2 rounded-md shadow-md hover:cursor-pointer">
{isSubmitting ? ( {isSubmitting ? 'Loading...' : 'Create a UserGroup'}
<BarLoader </button>
cssOverride={{ borderRadius: 60 }}
width={60}
color="#ffffff"
/>
) : (
'Create UserGroup'
)}
</ButtonBlack>
</Form.Submit> </Form.Submit>
</Flex> </div>
</FormLayout> </FormLayout>
) )
} }

View file

@ -19,11 +19,11 @@ function ManageUsers(props: ManageUsersProps) {
const access_token = session?.data?.tokens?.access_token; const access_token = session?.data?.tokens?.access_token;
const { data: OrgUsers } = useSWR( const { data: OrgUsers } = useSWR(
org ? `${getAPIUrl()}orgs/${org.id}/users` : null, org ? `${getAPIUrl()}orgs/${org.id}/users` : null,
swrFetcher (url) => swrFetcher(url, access_token)
) )
const { data: UGusers } = useSWR( const { data: UGusers } = useSWR(
org ? `${getAPIUrl()}usergroups/${props.usergroup_id}/users` : null, org ? `${getAPIUrl()}usergroups/${props.usergroup_id}/users` : null,
swrFetcher (url) => swrFetcher(url, access_token)
) )
const isUserPartOfGroup = (user_id: any) => { const isUserPartOfGroup = (user_id: any) => {
@ -34,7 +34,7 @@ function ManageUsers(props: ManageUsersProps) {
} }
const handleLinkUser = async (user_id: any) => { const handleLinkUser = async (user_id: any) => {
const res = await linkUserToUserGroup(props.usergroup_id, user_id,access_token) const res = await linkUserToUserGroup(props.usergroup_id, user_id, access_token)
if (res.status === 200) { if (res.status === 200) {
toast.success('User linked successfully') toast.success('User linked successfully')
mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`) mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`)
@ -44,7 +44,7 @@ function ManageUsers(props: ManageUsersProps) {
} }
const handleUnlinkUser = async (user_id: any) => { const handleUnlinkUser = async (user_id: any) => {
const res = await unLinkUserToUserGroup(props.usergroup_id, user_id,access_token) const res = await unLinkUserToUserGroup(props.usergroup_id, user_id, access_token)
if (res.status === 200) { if (res.status === 200) {
toast.success('User unlinked successfully') toast.success('User unlinked successfully')
mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`) mutate(`${getAPIUrl()}usergroups/${props.usergroup_id}/users`)