Skip to content

Commit

Permalink
add a new extraReducer to update the submissionsList when person is u…
Browse files Browse the repository at this point in the history
…pdated
  • Loading branch information
rebecarubio committed Dec 13, 2024
1 parent 007e246 commit c247300
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/features/surveys/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

import { personUpdated } from 'features/profile/store';
import { SurveyStats } from './rpc/getSurveyStats';
import {
ELEMENT_TYPE,
Expand Down Expand Up @@ -39,6 +40,33 @@ const initialState: SurveysStoreSlice = {
};

const surveysSlice = createSlice({
extraReducers: (builder) =>
builder.addCase(personUpdated, (state, action) => {
const person = action.payload;
const item = state.submissionList.items.find(
(item) => item?.data?.respondent?.id === person.id
);

if (item?.data?.respondent) {
const respondent = item.data.respondent;

if (person.email) {
respondent.email = person.email;
}
if (person.first_name) {
respondent.first_name = person.first_name;
}
if (person.last_name) {
respondent.last_name = person.last_name;
}
}
const submissionsUpdated = state.submissionList.items
.map((item) => item.data)
.filter((data): data is ZetkinSurveySubmission => data !== null);

addSubmissionToState(state, submissionsUpdated);
}),

initialState,
name: 'surveys',
reducers: {
Expand Down

0 comments on commit c247300

Please sign in to comment.