Skip to content

Commit f37aeac

Browse files
authored
feat: Age update adjustments (#973)
## What was done? - Update the UI logic so that screeners update the current aprox pupil age but we save the age at registration.
1 parent 94af6d3 commit f37aeac

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

src/Utility.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,22 @@ export const TIME_SLOTS = [
196196
`${fromFormatToMinutes('20:00')}-${fromFormatToMinutes('21:00')}`,
197197
];
198198

199+
export const getApproxCurrentAge = (createdAt: string, ageAtRegistration: number) => {
200+
const now = DateTime.now();
201+
const created = DateTime.fromISO(createdAt);
202+
const diffYears = Math.floor(now.diff(created, 'years').years);
203+
return Math.floor(ageAtRegistration + diffYears);
204+
};
205+
206+
export const getAgeAtRegistration = (createdAt: string, currentAge: number) => {
207+
const now = DateTime.now();
208+
const created = DateTime.fromISO(createdAt);
209+
210+
const diffYears = Math.floor(now.diff(created, 'years').years);
211+
212+
return Math.floor(currentAge - diffYears);
213+
};
214+
199215
const Utility = {
200216
createToken,
201217
toTimerString,

src/pages/screening/pupil/PersonalDetails.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ const PersonalDetails = ({ pupil, refresh, form, isUpdating, updatePupil }: Pers
7676
setDescriptionForMatch,
7777
weeklyAvailability,
7878
setWeeklyAvailability,
79-
age,
80-
setAge,
79+
currentAge,
80+
setCurrentAge,
8181
} = form;
8282

8383
const [mutationCreateLoginToken] = useMutation(CREATE_LOGIN_TOKEN_MUTATION);
8484
const [errors, setErrors] = useState<FormErrors>({});
8585

8686
useEffect(() => {
8787
let updatedErrors = {};
88-
if (!!age && age < MIN_AGE_PUPIL) {
88+
if (!!currentAge && currentAge < MIN_AGE_PUPIL) {
8989
updatedErrors = { ...updatedErrors, age: t('registration.steps.userAge.tooYoungError', { minAge: MIN_AGE_PUPIL }) };
9090
}
9191

9292
setErrors(updatedErrors);
93-
}, [languages, grade, subjects, age, t]);
93+
}, [languages, grade, subjects, currentAge, t]);
9494

9595
const impersonate = async () => {
9696
// We need to work around the popup blocker of modern browsers, as you can only
@@ -118,7 +118,6 @@ const PersonalDetails = ({ pupil, refresh, form, isUpdating, updatePupil }: Pers
118118
}
119119
};
120120

121-
console.log(errors);
122121
return (
123122
<>
124123
<div className="flex w-full justify-between mb-10">
@@ -169,8 +168,8 @@ const PersonalDetails = ({ pupil, refresh, form, isUpdating, updatePupil }: Pers
169168
<Label>Alter</Label>
170169
<Input
171170
className="w-full max-w-40"
172-
value={age || ''}
173-
onChangeText={(e) => setAge(Number(e.replace(/\D/g, '').substring(0, 2)))}
171+
value={currentAge || ''}
172+
onChangeText={(e) => setCurrentAge(Number(e.replace(/\D/g, '').substring(0, 2)))}
174173
errorMessage={errors.age}
175174
errorMessageClassName="hidden"
176175
min={MIN_AGE_PUPIL}

src/pages/screening/pupil/PupilDetail.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const PupilDetail = ({ pupil, refresh }: PupilDetailProps) => {
125125
<span className="font-bold">Registiert</span>: {formatDate(pupil.createdAt, DateTime.DATE_MED)}
126126
</Typography>
127127
<Typography>
128-
<span className="font-bold">Alter</span>: {getEstimatedAgeRange()}
128+
<span className="font-bold">Alter bei der Registrierung</span>: {!pupil.age ? 'Alter unbekannt' : pupil.age}
129129
</Typography>
130130
<Typography>
131131
<span className="font-bold">Aktiv</span>:{' '}

src/pages/screening/pupil/useUpdatePupil.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CheckedState } from '@/components/Checkbox';
22
import { gql } from '@/gql';
33
import { Gender_Enum as Gender, ExternalSchoolSearch } from '@/gql/graphql';
44
import { PupilForScreening } from '@/types';
5+
import { getAgeAtRegistration, getApproxCurrentAge } from '@/Utility';
56
import { useMutation } from '@apollo/client';
67
import { useState } from 'react';
78
import { useTranslation } from 'react-i18next';
@@ -31,7 +32,7 @@ export const useUpdatePupil = (pupil: PupilForScreening) => {
3132
const [weeklyAvailability, setWeeklyAvailability] = useState(pupil.calendarPreferences?.weeklyAvailability);
3233
const [canHaveMatches, setCanHaveMatches] = useState(pupil.isPupil);
3334
const [canParticipateInCourses, setCanParticipateInCourses] = useState(pupil.isParticipant);
34-
const [age, setAge] = useState(pupil.age);
35+
const [currentAge, setCurrentAge] = useState(pupil.age ? getApproxCurrentAge(pupil.createdAt, pupil.age) : undefined);
3536

3637
const updatePupil = async () => {
3738
try {
@@ -54,7 +55,7 @@ export const useUpdatePupil = (pupil: PupilForScreening) => {
5455
descriptionForMatch,
5556
descriptionForScreening,
5657
isPupil: canHaveMatches,
57-
age,
58+
age: pupil.age !== currentAge && currentAge ? getAgeAtRegistration(pupil.createdAt, currentAge) : pupil.age,
5859
isParticipant: canParticipateInCourses,
5960
calendarPreferences: weeklyAvailability
6061
? {
@@ -67,7 +68,7 @@ export const useUpdatePupil = (pupil: PupilForScreening) => {
6768
});
6869
toast.success(t('changesWereSaved'));
6970
} catch (error) {
70-
toast.success(t('error'));
71+
toast.error(t('error'));
7172
}
7273
};
7374

@@ -101,8 +102,8 @@ export const useUpdatePupil = (pupil: PupilForScreening) => {
101102
setCanHaveMatches,
102103
canParticipateInCourses,
103104
setCanParticipateInCourses,
104-
setAge,
105-
age,
105+
setCurrentAge,
106+
currentAge,
106107
},
107108
};
108109
};

0 commit comments

Comments
 (0)