-
Notifications
You must be signed in to change notification settings - Fork 0
/
mes-rendezvous.tsx
195 lines (168 loc) · 6.5 KB
/
mes-rendezvous.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { withTransaction } from '@elastic/apm-rum-react'
import { DateTime } from 'luxon'
import { GetServerSideProps, GetServerSidePropsResult } from 'next'
import React, { useEffect, useState } from 'react'
import TableauRdv from 'components/rdv/TableauRdv'
import Button, { ButtonStyle } from 'components/ui/Button/Button'
import ButtonLink from 'components/ui/Button/ButtonLink'
import IconComponent, { IconName } from 'components/ui/IconComponent'
import { StructureConseiller } from 'interfaces/conseiller'
import { PageProps } from 'interfaces/pageProps'
import { RdvListItem, rdvToListItem } from 'interfaces/rdv'
import { QueryParam, QueryValue } from 'referentiel/queryParam'
import { RendezVousService } from 'services/rendez-vous.service'
import useMatomo from 'utils/analytics/useMatomo'
import { withMandatorySessionOrRedirect } from 'utils/auth/withMandatorySessionOrRedirect'
import { useConseiller } from 'utils/conseiller/conseillerContext'
import { useDependance } from 'utils/injectionDependances'
interface MesRendezvousProps extends PageProps {
creationSuccess?: boolean
modificationSuccess?: boolean
suppressionSuccess?: boolean
messageEnvoiGroupeSuccess?: boolean
pageTitle: string
}
function MesRendezvous({
creationSuccess,
modificationSuccess,
suppressionSuccess,
messageEnvoiGroupeSuccess,
}: MesRendezvousProps) {
const rendezVousService =
useDependance<RendezVousService>('rendezVousService')
const [conseiller] = useConseiller()
const AUJOURDHUI = DateTime.now().startOf('day')
const [index7JoursAffiches, setIndex7JoursAffiches] = useState<number>(0)
const [rdvs, setRdvs] = useState<RdvListItem[]>([])
let initialTracking = `Mes rendez-vous`
if (creationSuccess) initialTracking += ' - Creation rdv succès'
if (modificationSuccess) initialTracking += ' - Modification rdv succès'
if (suppressionSuccess) initialTracking += ' - Suppression rdv succès'
if (messageEnvoiGroupeSuccess) initialTracking += ' - Succès envoi message'
const [trackingTitle, setTrackingTitle] = useState<string>(initialTracking)
async function allerRdvs7JoursPrecedants() {
const index7JoursPrecedants = index7JoursAffiches - 1
await chargerRdvs7Jours(index7JoursPrecedants)
setIndex7JoursAffiches(index7JoursPrecedants)
setTrackingTitle(`${trackingTitle} passés`)
}
async function allerRdvs7JoursActuels() {
const index7JoursActuels = 0
await chargerRdvs7Jours(index7JoursActuels)
setIndex7JoursAffiches(index7JoursActuels)
setTrackingTitle(trackingTitle)
}
async function allerRdvs7JoursSuivants() {
const index7JoursSuivants = index7JoursAffiches + 1
await chargerRdvs7Jours(index7JoursSuivants)
setIndex7JoursAffiches(index7JoursSuivants)
setTrackingTitle(`${trackingTitle} futurs`)
}
async function chargerRdvs7Jours(index7Jours: number) {
const rdvs7Jours = await rendezVousService.getRendezVousConseiller(
conseiller!.id,
jourDeDebutDesRdvs(index7Jours),
jourDeFinDesRdvs(index7Jours)
)
if (rdvs7Jours) setRdvs(rdvs7Jours.map(rdvToListItem))
}
function jourDeDebutDesRdvs(index7Jours?: number): DateTime {
return AUJOURDHUI.plus({
day: 7 * (index7Jours ?? index7JoursAffiches),
})
}
function jourDeFinDesRdvs(index7Jours?: number): DateTime {
return jourDeDebutDesRdvs(index7Jours ?? index7JoursAffiches)
.plus({ day: 6 })
.endOf('day')
}
useMatomo(trackingTitle)
useEffect(() => {
if (conseiller) chargerRdvs7Jours(index7JoursAffiches)
}, [conseiller, index7JoursAffiches])
return (
<>
<ButtonLink href={'/mes-jeunes/edition-rdv'} className='mb-6 w-fit'>
Fixer un rendez-vous
</ButtonLink>
<div className='mb-12'>
<div className='flex justify-between items-center'>
<p className='text-base-medium'>Période :</p>
<Button
type='button'
style={ButtonStyle.SECONDARY}
onClick={allerRdvs7JoursActuels}
>
<span className='sr-only'>Aller à la</span> Semaine en cours
</Button>
</div>
<div className='flex items-center mt-1'>
<p className='text-m-bold text-primary mr-6'>
du {jourDeDebutDesRdvs().toFormat('dd/MM/yyyy')} au{' '}
{jourDeFinDesRdvs().toFormat('dd/MM/yyyy')}
</p>
<button
aria-label='Aller à la semaine précédente'
onClick={allerRdvs7JoursPrecedants}
>
<IconComponent
name={IconName.ChevronLeft}
className='w-6 h-6 fill-primary hover:fill-primary_darken'
focusable='false'
title='Aller à la semaine précédente'
/>
</button>
<button
aria-label='Aller à la semaine suivante'
onClick={allerRdvs7JoursSuivants}
>
<IconComponent
name={IconName.ChevronRight}
className='w-6 h-6 fill-primary ml-8 hover:fill-primary_darken'
focusable='false'
title='Aller à la semaine suivante'
/>
</button>
</div>
</div>
<TableauRdv
idConseiller={conseiller?.id ?? ''}
rdvs={rdvs ?? []}
withIntercalaires={true}
/>
</>
)
}
export const getServerSideProps: GetServerSideProps<
MesRendezvousProps
> = async (context): Promise<GetServerSidePropsResult<MesRendezvousProps>> => {
const sessionOrRedirect = await withMandatorySessionOrRedirect(context)
if (!sessionOrRedirect.validSession) {
return { redirect: sessionOrRedirect.redirect }
}
const {
session: { user },
} = sessionOrRedirect
if (user.structure === StructureConseiller.POLE_EMPLOI) {
return { notFound: true }
}
const props: MesRendezvousProps = {
pageTitle: 'Tableau de bord - Mes rendez-vous',
pageHeader: 'Mes rendez-vous',
}
if (context.query[QueryParam.creationRdv])
props.creationSuccess =
context.query[QueryParam.creationRdv] === QueryValue.succes
if (context.query[QueryParam.modificationRdv])
props.modificationSuccess =
context.query[QueryParam.modificationRdv] === QueryValue.succes
if (context.query[QueryParam.suppressionRdv])
props.suppressionSuccess =
context.query[QueryParam.suppressionRdv] === QueryValue.succes
if (context.query[QueryParam.envoiMessage]) {
props.messageEnvoiGroupeSuccess =
context.query[QueryParam.envoiMessage] === QueryValue.succes
}
return { props }
}
export default withTransaction(MesRendezvous.name, 'page')(MesRendezvous)