Skip to content

Commit 39f9a64

Browse files
Merge pull request #309 from lara-learning/feat/trainer-can-make-new-users
feat: Allow Trainers to add new Trainees
2 parents dbe5e39 + 3180138 commit 39f9a64

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

packages/backend/src/permissions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ export const permissions = shield<unknown, Context>(
8787
claimTrainee: and(authenticated, trainer),
8888
unclaimTrainee: and(authenticated, trainer),
8989

90+
// Trainer and Admin mutations
91+
createTrainee: and(authenticated, or(admin, trainer)),
92+
9093
//Admin mutations
91-
createTrainee: and(authenticated, admin),
9294
updateTrainee: and(authenticated, admin),
9395
createTrainer: and(authenticated, admin),
9496
updateTrainer: and(authenticated, admin),

packages/frontend/src/graphql/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ export type SuggestionsDataQuery = { __typename?: 'Query', suggestions: Array<{
10651065
export type TraineePageDataQueryVariables = Exact<{ [key: string]: never; }>;
10661066

10671067

1068-
export type TraineePageDataQuery = { __typename?: 'Query', trainees: Array<{ __typename?: 'Trainee', id: string, firstName: string, lastName: string, course?: string | undefined, startDate?: string | undefined, trainer?: { __typename?: 'Trainer', id: string, firstName: string, lastName: string } | undefined, company: { __typename?: 'Company', id: string, name: string } }>, currentUser?: { __typename?: 'Admin', id: string } | { __typename?: 'Trainee', id: string } | { __typename?: 'Trainer', id: string } | undefined };
1068+
export type TraineePageDataQuery = { __typename?: 'Query', trainees: Array<{ __typename?: 'Trainee', id: string, firstName: string, lastName: string, course?: string | undefined, startDate?: string | undefined, trainer?: { __typename?: 'Trainer', id: string, firstName: string, lastName: string } | undefined, company: { __typename?: 'Company', id: string, name: string } }>, currentUser?: { __typename?: 'Admin', id: string } | { __typename?: 'Trainee', id: string } | { __typename?: 'Trainer', id: string } | undefined, companies?: Array<{ __typename?: 'Company', id: string, name: string }> | undefined };
10691069

10701070
export type TraineeSettingsDataQueryVariables = Exact<{ [key: string]: never; }>;
10711071

@@ -2604,6 +2604,10 @@ export const TraineePageDataDocument = gql`
26042604
currentUser {
26052605
id
26062606
}
2607+
companies {
2608+
id
2609+
name
2610+
}
26072611
}
26082612
`;
26092613
export function useTraineePageDataQuery(baseOptions?: Apollo.QueryHookOptions<TraineePageDataQuery, TraineePageDataQueryVariables>) {

packages/frontend/src/graphql/queries/trainee-page-data.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ query TraineePageData {
1818
currentUser {
1919
id
2020
}
21+
companies {
22+
id
23+
name
24+
}
2125
}

packages/frontend/src/pages/trainee-page.tsx

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
import React from 'react'
22
import { useParams } from 'react-router'
33

4-
import { H1 } from '@lara/components'
4+
import { AdminCreateUserLayout, H1, Paragraph } from '@lara/components'
55

66
import Loader from '../components/loader'
77
import TraineeRow from '../components/trainee-row'
8-
import { useTraineePageDataQuery } from '../graphql'
8+
import { useCreateTraineeMutation, useTraineePageDataQuery } from '../graphql'
99
import strings from '../locales/localization'
1010
import { Template } from '../templates/template'
11+
import { Fab } from '../components/fab'
12+
import Modal from '../components/modal'
13+
import { EditTraineeFormData, TraineeForm } from '../components/trainee-form'
14+
import { useToastContext } from '../hooks/use-toast-context'
15+
import { GraphQLError } from 'graphql'
1116

1217
const TraineePage: React.FunctionComponent = () => {
1318
const { trainee } = useParams()
1419
const { loading, data } = useTraineePageDataQuery()
20+
const [mutate] = useCreateTraineeMutation()
21+
22+
const { addToast } = useToastContext()
23+
24+
const [showModal, setShowModal] = React.useState(false)
1525

1626
const isActive = (id: string): boolean => {
1727
return id === trainee
1828
}
1929

30+
const createTrainee = async (data: EditTraineeFormData) => {
31+
await mutate({
32+
variables: { input: data },
33+
updateQueries: {
34+
TraineePageData: (prevData, { mutationResult }) => {
35+
return {
36+
...prevData,
37+
trainees: [...prevData.trainees, mutationResult.data?.createTrainee],
38+
}
39+
},
40+
},
41+
})
42+
.then(() => {
43+
addToast({
44+
icon: 'PersonNew',
45+
title: strings.createTrainee.title,
46+
text: strings.formatString(strings.createTrainee.success, `${data?.firstName} ${data?.lastName}`).toString(),
47+
type: 'success',
48+
})
49+
50+
setShowModal(false)
51+
})
52+
.catch((exception: GraphQLError) => {
53+
addToast({
54+
title: strings.errors.error,
55+
text: exception.message,
56+
type: 'error',
57+
})
58+
})
59+
}
60+
2061
return (
2162
<Template type="Main">
2263
<H1>{strings.navigation.trainees}</H1>
@@ -26,6 +67,30 @@ const TraineePage: React.FunctionComponent = () => {
2667
data?.trainees.map((trainee, index) => (
2768
<TraineeRow trainee={trainee} trainerId={data.currentUser?.id} key={index} active={isActive(trainee.id)} />
2869
))}
70+
71+
<Fab icon="Plus" large onClick={() => setShowModal(true)} />
72+
73+
<Modal large show={showModal} handleClose={() => setShowModal(false)} customClose>
74+
<AdminCreateUserLayout
75+
headline={<H1 noMargin>{strings.createTrainee.title}</H1>}
76+
description={
77+
<Paragraph fontSize="copy" color="darkFont">
78+
{strings.createTrainee.description}
79+
</Paragraph>
80+
}
81+
>
82+
{!loading && data?.companies ? (
83+
<TraineeForm
84+
blurSubmit={false}
85+
companies={data.companies}
86+
submit={createTrainee}
87+
cancel={() => setShowModal(false)}
88+
/>
89+
) : (
90+
<Loader />
91+
)}
92+
</AdminCreateUserLayout>
93+
</Modal>
2994
</Template>
3095
)
3196
}

0 commit comments

Comments
 (0)