-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1070 from joshunrau/update-group
- Loading branch information
Showing
9 changed files
with
5,277 additions
and
3,157 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
26 changes: 26 additions & 0 deletions
26
apps/web/src/features/admin/components/UpdateUserForm.stories.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,26 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { UpdateUserForm } from './UpdateUserForm'; | ||
|
||
type Story = StoryObj<typeof UpdateUserForm>; | ||
|
||
export default { component: UpdateUserForm } as Meta<typeof UpdateUserForm>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
data: { | ||
disableDelete: false, | ||
groupOptions: {}, | ||
initialValues: { | ||
additionalPermissions: [ | ||
{ | ||
action: 'create', | ||
subject: 'User' | ||
} | ||
] | ||
} | ||
}, | ||
onDelete: () => alert('Delete!'), | ||
onSubmit: (data) => alert(JSON.stringify({ data })) | ||
} | ||
}; |
179 changes: 179 additions & 0 deletions
179
apps/web/src/features/admin/components/UpdateUserForm.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,179 @@ | ||
import { isAllUndefined } from '@douglasneuroinformatics/libjs'; | ||
import { Button, Form } from '@douglasneuroinformatics/libui/components'; | ||
import { useTranslation } from '@douglasneuroinformatics/libui/hooks'; | ||
import type { FormTypes } from '@opendatacapture/runtime-core'; | ||
import { $UserPermission } from '@opendatacapture/schemas/user'; | ||
import type { Promisable } from 'type-fest'; | ||
import { z } from 'zod'; | ||
|
||
const $UpdateUserFormData = z | ||
.object({ | ||
additionalPermissions: z.array($UserPermission.partial()).optional(), | ||
groupIds: z.set(z.string()) | ||
}) | ||
.transform((arg) => { | ||
const firstPermission = arg.additionalPermissions?.[0]; | ||
if (firstPermission && isAllUndefined(firstPermission)) { | ||
arg.additionalPermissions?.pop(); | ||
} | ||
return arg; | ||
}) | ||
.superRefine((arg, ctx) => { | ||
arg.additionalPermissions?.forEach((permission, i) => { | ||
Object.entries(permission).forEach(([key, val]) => { | ||
if ((val satisfies string) === undefined) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.invalid_type, | ||
expected: 'string', | ||
path: ['additionalPermissions', i, key], | ||
received: 'undefined' | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
type UpdateUserFormData = z.infer<typeof $UpdateUserFormData>; | ||
|
||
export type UpdateUserFormInputData = { | ||
disableDelete: boolean; | ||
groupOptions: { | ||
[id: string]: string; | ||
}; | ||
initialValues: FormTypes.PartialNullableData<UpdateUserFormData>; | ||
}; | ||
|
||
export const UpdateUserForm: React.FC<{ | ||
data: UpdateUserFormInputData; | ||
onDelete: () => void; | ||
onSubmit: (data: UpdateUserFormData) => Promisable<void>; | ||
}> = ({ data, onDelete, onSubmit }) => { | ||
const { disableDelete, groupOptions, initialValues } = data; | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Form | ||
additionalButtons={{ | ||
left: ( | ||
<Button className="w-full" disabled={disableDelete} type="button" variant="danger" onClick={onDelete}> | ||
{t('core.delete')} | ||
</Button> | ||
) | ||
}} | ||
content={[ | ||
{ | ||
description: t({ | ||
en: 'IMPORTANT: These permissions are not specific to any group. To manage granular permissions, please use the API.', | ||
fr: "IMPORTANT : Ces autorisations ne sont pas spécifiques à un groupe. Pour gérer des autorisations granulaires, veuillez utiliser l'API." | ||
}), | ||
fields: { | ||
additionalPermissions: { | ||
fieldset: { | ||
action: { | ||
kind: 'string', | ||
label: t({ | ||
en: 'Action', | ||
fr: 'Action' | ||
}), | ||
options: { | ||
create: t({ | ||
en: 'Create', | ||
fr: 'Créer' | ||
}), | ||
delete: t({ | ||
en: 'Delete', | ||
fr: 'Effacer' | ||
}), | ||
manage: t({ | ||
en: 'Manage (All)', | ||
fr: 'Gérer (Tout)' | ||
}), | ||
read: t({ | ||
en: 'Read', | ||
fr: 'Lire' | ||
}), | ||
update: t({ | ||
en: 'Update', | ||
fr: 'Mettre à jour' | ||
}) | ||
}, | ||
variant: 'select' | ||
}, | ||
subject: { | ||
kind: 'string', | ||
label: t({ | ||
en: 'Resource', | ||
fr: 'Resource' | ||
}), | ||
options: { | ||
all: t({ | ||
en: 'All', | ||
fr: 'Tous' | ||
}), | ||
Assignment: t({ | ||
en: 'Assignment', | ||
fr: 'Devoir' | ||
}), | ||
Group: t({ | ||
en: 'Group', | ||
fr: 'Groupe' | ||
}), | ||
Instrument: t({ | ||
en: 'Instrument', | ||
fr: 'Instrument' | ||
}), | ||
InstrumentRecord: t({ | ||
en: 'Instrument Record', | ||
fr: "Enregistrement de l'instrument" | ||
}), | ||
Session: t({ | ||
en: 'Session', | ||
fr: 'Session' | ||
}), | ||
Subject: t({ | ||
en: 'Subject', | ||
fr: 'Client' | ||
}), | ||
User: t({ | ||
en: 'User', | ||
fr: 'Utilisateur' | ||
}) | ||
}, | ||
variant: 'select' | ||
} | ||
}, | ||
kind: 'record-array', | ||
label: t({ | ||
en: 'Permission', | ||
fr: 'Autorisations supplémentaires' | ||
}) | ||
} | ||
}, | ||
title: t({ | ||
en: 'Authorization', | ||
fr: 'Autorisation' | ||
}) | ||
}, | ||
{ | ||
fields: { | ||
groupIds: { | ||
kind: 'set', | ||
label: 'Group IDs', | ||
options: groupOptions, | ||
variant: 'listbox' | ||
} | ||
}, | ||
title: t({ | ||
en: 'Groups', | ||
fr: 'Groupes' | ||
}) | ||
} | ||
]} | ||
initialValues={initialValues} | ||
key={JSON.stringify(initialValues)} | ||
submitBtnLabel={t('core.save')} | ||
validationSchema={$UpdateUserFormData} | ||
onSubmit={onSubmit} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.