diff --git a/src/actions/__tests__/weeklySummaryEmailBCCAction.js.test.js b/src/actions/__tests__/weeklySummaryEmailBCCAction.js.test.js new file mode 100644 index 0000000000..41662d9bcf --- /dev/null +++ b/src/actions/__tests__/weeklySummaryEmailBCCAction.js.test.js @@ -0,0 +1,67 @@ +import axios from 'axios'; +import configureMockStore from 'redux-mock-store'; +import thunk from 'redux-thunk'; + +import * as types from '../../constants/WeeklySummaryEmailBccConstants'; +import { updateWeeklySummaryEmailAssignment } from '../weeklySummaryEmailBCCAction'; + +vi.mock('axios'); + +const mockStore = configureMockStore([thunk]); + +describe('updateWeeklySummaryEmailAssignment action creator', () => { + it('dispatches updated assignment when API returns wrapped assignment payload', async () => { + const store = mockStore({}); + const updatedAssignment = { + _id: 'assignment-id', + email: 'updated@example.com', + assignedTo: { _id: 'user-id', firstName: 'Updated', lastName: 'User' }, + }; + + axios.put.mockResolvedValue({ + status: 200, + data: { assignment: updatedAssignment }, + }); + + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); + + expect(store.getActions()).toContainEqual({ + type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT, + payload: updatedAssignment, + }); + }); + + it('dispatches updated assignment when API returns assignment directly', async () => { + const store = mockStore({}); + const updatedAssignment = { + _id: 'assignment-id', + email: 'updated@example.com', + }; + + axios.put.mockResolvedValue({ + status: 200, + data: updatedAssignment, + }); + + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); + + expect(store.getActions()).toContainEqual({ + type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT, + payload: updatedAssignment, + }); + }); + + it('dispatches error action when update fails', async () => { + const store = mockStore({}); + const error = new Error('Network Error'); + + axios.put.mockRejectedValue(error); + + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); + + expect(store.getActions()).toContainEqual({ + type: types.WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR, + payload: error, + }); + }); +}); diff --git a/src/actions/weeklySummaryEmailBCCAction.js b/src/actions/weeklySummaryEmailBCCAction.js index d8d0a05751..7c9b814f3c 100644 --- a/src/actions/weeklySummaryEmailBCCAction.js +++ b/src/actions/weeklySummaryEmailBCCAction.js @@ -24,6 +24,11 @@ const weeklySummaryEmailBccError = error => ({ payload: error, }); +const updateWeeklySummaryEmailBcc = assignment => ({ + type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT, + payload: assignment, +}); + // fetch all assignments export const getAllWeeklySummaryEmailAssignments = () => { const url = ENDPOINTS.WEEKLY_SUMMARY_EMAIL_BCC(); @@ -79,7 +84,12 @@ export const updateWeeklySummaryEmailAssignment = (id, email) => async dispatch try { const response = await axios.put(ENDPOINTS.UPDATE_WEEKLY_SUMMARY_EMAIL_BCC(id), { email }); if (response.status === 200) { - dispatch(getAllWeeklySummaryEmailAssignments()); + const updated = response?.data?.assignment || response?.data || { _id: id, email }; + if (updated && updated._id) { + dispatch(updateWeeklySummaryEmailBcc(updated)); + } else { + dispatch(getAllWeeklySummaryEmailAssignments()); + } } else { dispatch(weeklySummaryEmailBccError(response.data)); } @@ -87,3 +97,4 @@ export const updateWeeklySummaryEmailAssignment = (id, email) => async dispatch dispatch(weeklySummaryEmailBccError(error)); } }; + diff --git a/src/components/VolunteerweeklysummaryBBC/WeeklySummaryEmailAssignmentPopUp.jsx b/src/components/VolunteerweeklysummaryBBC/WeeklySummaryEmailAssignmentPopUp.jsx index 5598221d58..f4f4f684e5 100644 --- a/src/components/VolunteerweeklysummaryBBC/WeeklySummaryEmailAssignmentPopUp.jsx +++ b/src/components/VolunteerweeklysummaryBBC/WeeklySummaryEmailAssignmentPopUp.jsx @@ -89,9 +89,9 @@ const WeeklySummaryEmailAssignmentPopUp = React.memo(props => { setEditingEmail(email); }; - const handleEditSave = id => { + const handleEditSave = async id => { if (editingEmail && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(editingEmail)) { - dispatch(updateWeeklySummaryEmailAssignment(id, editingEmail)); + await dispatch(updateWeeklySummaryEmailAssignment(id, editingEmail)); setEditingId(null); setEditingEmail(''); } diff --git a/src/constants/WeeklySummaryEmailBccConstants.js b/src/constants/WeeklySummaryEmailBccConstants.js index 4e1d98f298..8148a583d7 100644 --- a/src/constants/WeeklySummaryEmailBccConstants.js +++ b/src/constants/WeeklySummaryEmailBccConstants.js @@ -2,3 +2,4 @@ export const GET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENTS = 'GET_WEEKLY_SUMMARY_EMAIL_AS export const SET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'SET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT'; export const DELETE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'DELETE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT'; export const WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR = 'WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR'; +export const UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT'; diff --git a/src/reducers/WeeklySummaryEmailAssignment.js b/src/reducers/WeeklySummaryEmailAssignment.js index 0a19850cb0..e0fc360bf8 100644 --- a/src/reducers/WeeklySummaryEmailAssignment.js +++ b/src/reducers/WeeklySummaryEmailAssignment.js @@ -25,6 +25,14 @@ const WeeklySummaryEmailAssignment = (state = initialState, action = {}) => { emailAssignment: state.emailAssignment.filter(ele => ele._id !== action.payload), error: null, }; + case types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT: + return { + ...state, + emailAssignment: state.emailAssignment.map(item => + item._id === action.payload._id ? { ...item, ...action.payload } : item, + ), + error: null, + }; case types.WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR: return { ...state,