11import React from 'react'
22import { useParams } from 'react-router'
33
4- import { H1 } from '@lara/components'
4+ import { AdminCreateUserLayout , H1 , Paragraph } from '@lara/components'
55
66import Loader from '../components/loader'
77import TraineeRow from '../components/trainee-row'
8- import { useTraineePageDataQuery } from '../graphql'
8+ import { useCreateTraineeMutation , useTraineePageDataQuery } from '../graphql'
99import strings from '../locales/localization'
1010import { 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
1217const 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