-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add edit/create role form and page (#1508)
* Begin creating role form * Remove common_roles_view (replaced by common_role) * biome * Remove duplicate getRole * Load permissions and display in MultiSelect * Actually fetch roles in role admin page * Fix imports * Update router structure for roles * Add link to Create role page * SchemaType shortcut type * Describe null value of hierarchy level * Send create/edit request to backend * Add create page to router * Add margin to button row * Allow MultiSelect to take ref
- Loading branch information
Showing
20 changed files
with
339 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
frontend/src/PagesAdmin/RoleFormAdminPage/RoleFormAdminPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useRouteLoaderData } from 'react-router-dom'; | ||
import { AdminPageLayout } from '~/PagesAdmin/AdminPageLayout/AdminPageLayout'; | ||
import { RoleForm } from '~/PagesAdmin/RoleFormAdminPage/components'; | ||
import { KEY } from '~/i18n/constants'; | ||
import type { RoleLoader } from '~/router/loaders'; | ||
import { lowerCapitalize } from '~/utils'; | ||
|
||
export function RoleFormAdminPage() { | ||
const { t } = useTranslation(); | ||
const data = useRouteLoaderData('role') as RoleLoader | undefined; | ||
|
||
const title = lowerCapitalize(`${t(data?.role ? KEY.common_edit : KEY.common_create)} ${t(KEY.common_role)}`); | ||
return ( | ||
<AdminPageLayout title={title}> | ||
<RoleForm role={data?.role} /> | ||
</AdminPageLayout> | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.action_row { | ||
display: flex; | ||
justify-content: flex-end; | ||
margin: 1rem 0; | ||
} |
175 changes: 175 additions & 0 deletions
175
frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/RoleForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useMutation, useQuery } from '@tanstack/react-query'; | ||
import { useMemo } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { toast } from 'react-toastify'; | ||
import { z } from 'zod'; | ||
import { | ||
Alert, | ||
Button, | ||
Dropdown, | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
Input, | ||
} from '~/Components'; | ||
import type { DropdownOption } from '~/Components/Dropdown/Dropdown'; | ||
import { MultiSelect } from '~/Components/MultiSelect'; | ||
import { createRole, editRole, getPermissions } from '~/api'; | ||
import type { RoleDto } from '~/dto'; | ||
import { KEY } from '~/i18n/constants'; | ||
import { ROLE_CONTENT_TYPE, ROLE_NAME } from '~/schema/role'; | ||
import styles from './RoleForm.module.scss'; | ||
|
||
const schema = z.object({ | ||
name: ROLE_NAME, | ||
permissions: z.array(z.number()), | ||
content_type: ROLE_CONTENT_TYPE.nullish(), | ||
}); | ||
|
||
type SchemaType = z.infer<typeof schema>; | ||
|
||
type ContentTypeSchemaType = z.infer<typeof ROLE_CONTENT_TYPE>; | ||
|
||
type Props = { | ||
role?: RoleDto; | ||
}; | ||
|
||
export function RoleForm({ role }: Props) { | ||
const { t } = useTranslation(); | ||
|
||
const { | ||
data: allPermissions, | ||
isLoading, | ||
isError, | ||
} = useQuery({ | ||
queryKey: ['permissions'], | ||
queryFn: getPermissions, | ||
}); | ||
|
||
const edit = useMutation({ | ||
mutationFn: editRole, | ||
onSuccess: () => { | ||
toast.success(t(KEY.common_save_successful)); | ||
}, | ||
}); | ||
|
||
const create = useMutation({ | ||
mutationFn: createRole, | ||
onSuccess: () => { | ||
toast.success(t(KEY.common_creation_successful)); | ||
}, | ||
}); | ||
|
||
const isPending = edit.isPending || create.isPending; | ||
|
||
const permissionOptions = useMemo<DropdownOption<number>[]>(() => { | ||
if (!allPermissions) { | ||
return []; | ||
} | ||
return allPermissions.map((p) => ({ | ||
value: p.id, | ||
label: p.name, | ||
})); | ||
}, [allPermissions]); | ||
|
||
const selectedPermissions = useMemo<DropdownOption<number>[]>(() => { | ||
if (!allPermissions || !role) { | ||
return []; | ||
} | ||
const permissions = allPermissions.filter((p) => role.permissions.includes(p.id)); | ||
return permissions.map((p) => ({ | ||
value: p.id, | ||
label: p.name, | ||
})); | ||
}, [role, allPermissions]); | ||
|
||
const form = useForm<SchemaType>({ | ||
resolver: zodResolver(schema), | ||
defaultValues: { | ||
name: role?.name ?? '', | ||
permissions: role?.permissions ?? [], | ||
content_type: (role?.content_type ?? '') as ContentTypeSchemaType, | ||
}, | ||
}); | ||
|
||
function onSubmit(values: SchemaType) { | ||
console.log(values); | ||
if (role) { | ||
edit.mutate({ id: role.id, ...values }); | ||
} else { | ||
create.mutate(values); | ||
} | ||
} | ||
|
||
const contentTypeLabels: Record<ContentTypeSchemaType, string> = { | ||
'': t(KEY.common_any), | ||
organization: t(KEY.recruitment_organization), | ||
gang: t(KEY.common_gang), | ||
section: t(KEY.common_section), | ||
}; | ||
|
||
const contentTypeOptions: DropdownOption<ContentTypeSchemaType>[] = ROLE_CONTENT_TYPE.options.map((ct) => ({ | ||
value: ct, | ||
label: contentTypeLabels[ct], | ||
})); | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)}> | ||
<FormField | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{t(KEY.common_name)}</FormLabel> | ||
<FormControl> | ||
<Input type="text" disabled={isLoading || isPending} {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
name="content_type" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{t(KEY.role_content_type)}</FormLabel> | ||
<FormControl> | ||
<Dropdown options={contentTypeOptions} disabled={isLoading || isPending} {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
name="permissions" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{t(KEY.common_permissions)}</FormLabel> | ||
<FormControl> | ||
{isLoading ? ( | ||
<span>{t(KEY.common_loading)}...</span> | ||
) : isError ? ( | ||
<Alert message={t(KEY.role_edit_could_not_load_permissions)} type="error" /> | ||
) : ( | ||
<MultiSelect options={permissionOptions} selected={selectedPermissions} {...field} /> | ||
)} | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<div className={styles.action_row}> | ||
<Button type="submit" theme="green" disabled={isLoading || isPending}> | ||
{t(KEY.common_save)} | ||
</Button> | ||
</div> | ||
</form> | ||
</Form> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
frontend/src/PagesAdmin/RoleFormAdminPage/components/RoleForm/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { RoleForm } from './RoleForm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { RoleForm } from './RoleForm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { RoleFormAdminPage } from './RoleFormAdminPage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.