|
| 1 | +import type { NextPage } from 'next' |
| 2 | +import type { Languages } from '@helpwave/common/hooks/useLanguage' |
| 3 | +import { useTranslation, type PropsForTranslation } from '@helpwave/common/hooks/useTranslation' |
| 4 | +import { Page } from '@/components/layout/Page' |
| 5 | +import titleWrapper from '@/utils/titleWrapper' |
| 6 | +import { Section } from '@/components/layout/Section' |
| 7 | +import { LoadingAndErrorComponent } from '@helpwave/common/components/LoadingAndErrorComponent' |
| 8 | +import { tw } from '@twind/core' |
| 9 | +import type { UserRole, UserRoleTranslation, UserSeat } from '@/api/dataclasses/user_seat' |
| 10 | +import { userRoles } from '@/api/dataclasses/user_seat' |
| 11 | +import { defaultUserRoleTranslation } from '@/api/dataclasses/user_seat' |
| 12 | +import { useUserSeatsQuery, useUserSeatUpdateMutation } from '@/api/mutations/user_seat_mutations' |
| 13 | +import { Table } from '@helpwave/common/components/Table' |
| 14 | +import { Select } from '@helpwave/common/components/user-input/Select' |
| 15 | +import { Checkbox } from '@helpwave/common/components/user-input/Checkbox' |
| 16 | + |
| 17 | +type TeamTranslation = { |
| 18 | + team: string, |
| 19 | + teamDescription: string, |
| 20 | + role : string, |
| 21 | + enabled: string, |
| 22 | +} & UserRoleTranslation |
| 23 | + |
| 24 | +const defaultTeamTranslations: Record<Languages, TeamTranslation> = { |
| 25 | + en: { |
| 26 | + ...defaultUserRoleTranslation.en, |
| 27 | + team: 'Team', |
| 28 | + teamDescription: 'Here you can change and add members to your team.', |
| 29 | + role: 'Role', |
| 30 | + enabled: 'Enabled', |
| 31 | + }, |
| 32 | + de: { |
| 33 | + ...defaultUserRoleTranslation.de, |
| 34 | + team: 'Team', |
| 35 | + teamDescription: 'Hier kannst du Mitglieder deines Teams verwalten.', |
| 36 | + role: 'Rolle', |
| 37 | + enabled: 'Aktiv', |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +type TeamServerSideProps = { |
| 42 | + jsonFeed: unknown, |
| 43 | +} |
| 44 | + |
| 45 | +const Team: NextPage<PropsForTranslation<TeamTranslation, TeamServerSideProps>> = ({ overwriteTranslation }) => { |
| 46 | + const translation = useTranslation(defaultTeamTranslations, overwriteTranslation) |
| 47 | + const { data, isError, isLoading } = useUserSeatsQuery() |
| 48 | + const userSeatUpdate = useUserSeatUpdateMutation() |
| 49 | + |
| 50 | + const idMapping = (value: UserSeat): string => value.customerUUID + value.email |
| 51 | + |
| 52 | + // TODO do input validation |
| 53 | + return ( |
| 54 | + <Page pageTitle={titleWrapper(translation.team)} mainContainerClassName={tw('min-h-[auto] pb-6')}> |
| 55 | + <Section titleText={translation.team}> |
| 56 | + <LoadingAndErrorComponent isLoading={isLoading} hasError={isError} minimumLoadingDuration={200}> |
| 57 | + {!!data && ( |
| 58 | + <div className={tw('flex flex-col gap-y-1')}> |
| 59 | + <span>{translation.teamDescription}</span> |
| 60 | + <Table |
| 61 | + data={data} |
| 62 | + identifierMapping={idMapping} |
| 63 | + header={[ |
| 64 | + (<span key="user">{translation.user}</span>), |
| 65 | + (<span key="role">{translation.role}</span>), |
| 66 | + (<span key="enabled">{translation.enabled}</span>) |
| 67 | + ]} |
| 68 | + rowMappingToCells={dataObject => [ |
| 69 | + ( |
| 70 | + <div key={idMapping(dataObject) + '-name'} className={tw('flex flex-col')}> |
| 71 | + <span |
| 72 | + className={tw('text-lg font-semibold')}>{`${dataObject.firstName} ${dataObject.lastName}`}</span> |
| 73 | + <span className={tw('text-gray-400')}>{dataObject.email}</span> |
| 74 | + </div> |
| 75 | + ), |
| 76 | + ( |
| 77 | + <Select<UserRole> |
| 78 | + key={idMapping(dataObject) + '-role'} |
| 79 | + value={dataObject.role} |
| 80 | + options={userRoles.map(role => ({ value: role, label: translation[role] }))} |
| 81 | + onChange={role => userSeatUpdate.mutate({ ...dataObject, role })} |
| 82 | + /> |
| 83 | + ), |
| 84 | + ( |
| 85 | + <Checkbox |
| 86 | + key={idMapping(dataObject) + '-enabled'} |
| 87 | + checked={dataObject.enabled} |
| 88 | + onChange={enabled => userSeatUpdate.mutate({ ...dataObject, enabled })} |
| 89 | + /> |
| 90 | + ) |
| 91 | + ]} |
| 92 | + /> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + </LoadingAndErrorComponent> |
| 96 | + </Section> |
| 97 | + </Page> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +export default Team |
0 commit comments