Skip to content

Commit f09ab8a

Browse files
committed
fix: refresh profile context after enrolment mutations
Ensure profile UI state is updated after enrol/unenrol. Refs: KK-1510
1 parent 929109b commit f09ab8a

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/domain/event/enrol/EnrolPage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import useGetPathname from '../../../common/route/utils/useGetPathname';
2626
import { publicSvgIconPaths } from '../../../public_files';
2727
import Icon from '../../../common/components/icon/Icon';
2828
import graphqlClient from '../../api/client';
29+
import { useProfileContext } from '../../profile/hooks/useProfileContext';
2930

3031
function containsAlreadyJoinedError(
3132
errors: ReadonlyArray<GraphQLFormattedError>
@@ -52,6 +53,7 @@ const EnrolPage = () => {
5253
i18n: { language },
5354
} = useTranslation();
5455
const dispatch = useDispatch();
56+
const { refetchProfile } = useProfileContext();
5557
const params = useParams<{
5658
childId: string;
5759
eventId: string;
@@ -91,7 +93,8 @@ const EnrolPage = () => {
9193
childId,
9294
eventGroupId: data?.occurrence?.event?.eventGroup?.id,
9395
}),
94-
onCompleted: (data) => {
96+
awaitRefetchQueries: true,
97+
onCompleted: async (data) => {
9598
if (data?.enrolOccurrence?.enrolment?.child?.occurrences?.edges) {
9699
dispatch(
97100
saveChildEvents({
@@ -111,6 +114,8 @@ const EnrolPage = () => {
111114
);
112115
}
113116

117+
await refetchProfile();
118+
114119
goToOccurrence();
115120
},
116121
onError: (error) => {

src/domain/event/modal/UnenrolModal.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { saveChildEvents } from '../state/EventActions';
1717
import getEventOrEventGroupOccurrenceRefetchQueries from '../getEventOrEventGroupOccurrenceRefetchQueries';
1818
import AppConfig from '../../app/AppConfig';
1919
import graphqlClient from '../../api/client';
20+
import { useProfileContext } from '../../profile/hooks/useProfileContext';
2021

2122
interface UnenrolModalProps {
2223
isOpen: boolean;
@@ -37,6 +38,7 @@ const UnenrolModal = ({
3738
const { t } = useTranslation();
3839
const dispatch = useDispatch();
3940
const getPathname = useGetPathname();
41+
const { refetchProfile } = useProfileContext();
4042

4143
const [unenrolOccurrence] = useMutation<
4244
UnenrolOccurrenceMutation,
@@ -48,7 +50,7 @@ const UnenrolModal = ({
4850
eventGroupId,
4951
}),
5052
awaitRefetchQueries: true,
51-
onCompleted: (data) => {
53+
onCompleted: async (data) => {
5254
if (data.unenrolOccurrence?.child?.occurrences.edges) {
5355
dispatch(
5456
saveChildEvents({
@@ -57,6 +59,9 @@ const UnenrolModal = ({
5759
})
5860
);
5961
}
62+
63+
await refetchProfile();
64+
6065
navigate(getPathname(`/profile/child/${childId}`), { replace: true });
6166
},
6267
});

src/domain/profile/ProfileContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type ProfileContextType = {
1111
profile: ProfileType;
1212
clearProfile: () => void;
1313
updateProfile: UpdateProfileType;
14-
refetchProfile: () => void;
14+
refetchProfile: () => Promise<void>;
1515
isLoading: boolean;
1616
isFetchCalled: boolean;
1717
};
@@ -26,7 +26,7 @@ const defaultContext: ProfileContextType = {
2626
updateProfile: () => {
2727
throw new Error('Not implemented');
2828
},
29-
refetchProfile: () => {
29+
refetchProfile: async () => {
3030
throw new Error('Not implemented');
3131
},
3232
};

src/domain/profile/ProfileProvider.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ export default function ProfileProvider({
1818
setProfileToContext,
1919
});
2020

21-
const refetchProfile = React.useCallback(() => {
22-
refetch().then(({ data: refreshedData }) => {
23-
setProfileToContext(refreshedData?.myProfile || null);
24-
// eslint-disable-next-line no-console
25-
console.info('ProfileContext', 'profile refetched');
26-
});
21+
const refetchProfile = React.useCallback(async () => {
22+
const { data: refreshedData } = await refetch();
23+
setProfileToContext(refreshedData?.myProfile || null);
24+
// eslint-disable-next-line no-console
25+
console.info('ProfileContext', 'profile refetched');
2726
}, [refetch]);
2827

2928
const clearProfile = React.useCallback(() => {

0 commit comments

Comments
 (0)