11import axios from 'axios' ;
2+ import { toast } from 'react-toastify' ;
23import * as actions from '../constants/weeklySummaries' ;
4+ import * as reportActions from '../constants/weeklySummariesReport' ;
35import { ENDPOINTS } from '~/utils/URL' ;
46import { getUserProfileActionCreator } from './userProfile' ;
57
@@ -36,7 +38,7 @@ export const fetchWeeklySummariesError = error => ({
3638 * @param {ObjectId } userId The user id.
3739 */
3840export const getWeeklySummaries = userId => {
39- const url = ENDPOINTS . USER_PROFILE ( userId ) ;
41+ const url = ENDPOINTS . USER_PROFILE_FIXED ( userId ) ;
4042 return async dispatch => {
4143 dispatch ( fetchWeeklySummariesBegin ( ) ) ;
4244 try {
@@ -112,3 +114,215 @@ export const updateWeeklySummaries = (userId, weeklySummariesData) => {
112114 }
113115 } ;
114116} ;
117+
118+ // ========== WEEKLY SUMMARIES REPORT (from your old file) ==========
119+
120+ /**
121+ * Action to set the 'loading' flag to true for reports.
122+ */
123+ export const fetchWeeklySummariesReportBegin = ( ) => ( {
124+ type : reportActions . FETCH_SUMMARIES_REPORT_BEGIN ,
125+ } ) ;
126+
127+ /**
128+ * This action is used to set the weekly summaries reports in store.
129+ *
130+ * @param {array } weeklySummariesData An array of all active users.
131+ */
132+ export const fetchWeeklySummariesReportSuccess = weeklySummariesData => ( {
133+ type : reportActions . FETCH_SUMMARIES_REPORT_SUCCESS ,
134+ payload : { weeklySummariesData } ,
135+ } ) ;
136+
137+ /**
138+ * Handle the error case for reports.
139+ *
140+ * @param {Object } error The error object.
141+ */
142+ export const fetchWeeklySummariesReportError = error => ( {
143+ type : reportActions . FETCH_SUMMARIES_REPORT_ERROR ,
144+ payload : { error } ,
145+ } ) ;
146+
147+ /**
148+ * Update one summary report
149+ *
150+ * @param {Object } updatedField the updated field object, dynamic
151+ */
152+ export const updateSummaryReport = ( { _id, updatedField } ) => ( {
153+ type : reportActions . UPDATE_SUMMARY_REPORT ,
154+ payload : { _id, updatedField } ,
155+ } ) ;
156+
157+ // write server-truth user object into the store
158+ export const updateSummaryReportFromServerAction = ( user ) => ( {
159+ type : reportActions . UPDATE_SUMMARY_REPORT ,
160+ payload : { _id : user . _id , updatedField : user } ,
161+ } ) ;
162+ /**
163+ * Gets all active users' summaries + a few other selected fields from the userProfile that
164+ * might be useful for the weekly summary report.
165+ */
166+ export const getWeeklySummariesReport = ( weekIndex = null ) => {
167+ return async dispatch => {
168+ dispatch ( fetchWeeklySummariesReportBegin ( ) ) ;
169+ try {
170+ // Use the APIEndpoint from ENDPOINTS
171+ let url = ENDPOINTS . WEEKLY_SUMMARIES_REPORT ( ) ;
172+
173+ const timestamp = `ts=${ Date . now ( ) } ` ;
174+ const separator = url . includes ( '?' ) ? '&' : '?' ;
175+
176+ if ( weekIndex === null ) {
177+ url = `${ url } ${ separator } ${ timestamp } ` ;
178+ } else {
179+ const separator = url . includes ( '?' ) ? '&' : '?' ;
180+ url = `${ url } ${ separator } week=${ weekIndex } &${ timestamp } ` ;
181+ }
182+
183+ const response = await axios . get ( url , {
184+ headers : {
185+ 'Cache-Control' : 'no-cache' ,
186+ Pragma : 'no-cache' ,
187+ Expires : '0' ,
188+ } ,
189+ } ) ;
190+ // Adding this debug log to check if filterColors are coming back
191+ // eslint-disable-next-line no-console
192+ console . log ( 'API Response:' , response . data ) ;
193+ // eslint-disable-next-line no-console
194+ console . log ( 'FilterColors in response:' , response . data . map ( user => ( {
195+ id : user . _id ,
196+ name : user . firstName + ' ' + user . lastName ,
197+ filterColor : user . filterColor
198+ } ) ) ) ;
199+ dispatch ( fetchWeeklySummariesReportSuccess ( response . data ) ) ;
200+ return { status : response . status , data : response . data } ;
201+ } catch ( error ) {
202+ dispatch ( fetchWeeklySummariesReportError ( error ) ) ;
203+ return error . response ? error . response . status : 500 ;
204+ }
205+ } ;
206+ } ;
207+
208+ // working
209+ // export const updateOneSummaryReport = (userId, fullUserPayload) => {
210+ // // const url = ENDPOINTS.USER_PROFILE(userId);
211+ // // return async dispatch => {
212+ // // const { data: userProfile } = await axios.get(url);
213+ // // const payload = { ...userProfile, ...updatedField };
214+ // // // eslint-disable-next-line no-console
215+ // // console.log('🛰 PUT payload being sent:', payload);
216+ // // const res = await axios.put(url, {
217+ // // ...userProfile,
218+ // // ...updatedField,
219+ // // });
220+
221+ // // // 🔹 Step 4 debug: log backend response explicitly
222+ // // // eslint-disable-next-line no-console
223+ // // console.log('🔍 Backend returned after PUT:', res.data);
224+ // // // eslint-disable-next-line no-console
225+ // // console.log('✅ PUT response:', res.data);
226+
227+ // // if (res.status === 200) {
228+ // // dispatch(updateSummaryReport({ _id: userId, updatedField }));
229+ // // // eslint-disable-next-line no-console
230+ // // console.log('🟢 Redux state updated with:', updatedField);
231+ // // return res;
232+ // // }
233+
234+ // // throw new Error(`An error occurred while attempting to save the changes to the profile.`);
235+ // // };
236+ // const url = ENDPOINTS.USER_PROFILE(userId);
237+ // return async dispatch => {
238+ // // try {
239+ // // // Optional: fetch current user profile if needed
240+ // // // const { data: userProfile } = await axios.get(url);
241+ // // const state = getState();
242+ // // const allUsers = Array.isArray(state.weeklySummariesReport?.summaries)
243+ // // ? state.weeklySummariesReport.summaries
244+ // // : [];
245+ // // const currentUser = allUsers.find(u => u._id === userId);
246+
247+ // // if (!currentUser) throw new Error('User not found in state');
248+
249+ // // // Merge updatedField (like filterColor) into the full user object
250+ // // const payload = { ...currentUser, ...updatedField };
251+
252+ // // // Send the PUT and get server response (server should return the saved user)
253+ // // // const res = await axios.put(url, {
254+ // // // ...updatedField, // send only updated fields (you already normalized on client)
255+ // // // });
256+ // // const res = await axios.put(url, payload);
257+ // // // Log for debugging
258+ // // // eslint-disable-next-line no-console
259+ // // console.log('✅ PUT response (updateOneSummaryReport):', res.data);
260+
261+ // // if (res.status === 200) {
262+ // // // Dispatch the server-truth into the store.
263+ // // // IMPORTANT: payload should be the full user object (or at least fields the UI needs)
264+ // // dispatch(updateSummaryReport({ _id: userId, updatedField: res.data }));
265+
266+ // // // Return the server response so callers can use res.data
267+ // // return res;
268+ // // }
269+
270+ // // throw new Error('Failed to save profile');
271+ // // } catch (err) {
272+ // // // rethrow so caller's try/catch can handle revert
273+ // // throw err;
274+ // // }
275+ // //above was good byt not working code
276+ // const res = await axios.put(url, fullUserPayload);
277+ // if (res.status === 200) {
278+ // dispatch(updateSummaryReport({ _id: userId, updatedField: res.data }));
279+ // return res;
280+ // }
281+ // throw new Error('Failed to save profile');
282+ // };
283+ // };
284+ export const updateOneSummaryReport = ( userId , payload ) => {
285+ const url = ENDPOINTS . USER_PROFILE ( userId ) ;
286+ return async dispatch => {
287+ const res = await axios . put ( url , payload ) ;
288+ if ( res . status === 200 ) {
289+ dispatch ( updateSummaryReport ( { _id : userId , updatedField : res . data } ) ) ;
290+ return res ;
291+ }
292+ throw new Error ( 'Failed to save profile' ) ;
293+ } ;
294+ } ;
295+
296+ /**
297+ * Toggle the user's bio status (posted, requested, default).
298+ */
299+ export const toggleUserBio = ( userId , bioPosted ) => {
300+ const url = ENDPOINTS . TOGGLE_BIO_STATUS ( userId ) ;
301+ return async dispatch => {
302+ try {
303+ const res = await axios . patch ( url , { bioPosted } ) ;
304+
305+ if ( res . status === 200 ) {
306+ const updatedField = { bioPosted } ;
307+
308+ // Dispatch an action to update the store
309+ dispatch ( updateSummaryReport ( { _id : userId , updatedField } ) ) ;
310+
311+ toast . success ( `Bio status updated to "${ bioPosted } "` ) ;
312+ }
313+
314+ return res ;
315+ } catch ( error ) {
316+ toast . error ( 'An error occurred while updating bio status.' ) ;
317+ throw error ;
318+ }
319+ } ;
320+ } ;
321+
322+ /**
323+ * Optimistically update filterColor for all users in selected team codes.
324+ */
325+ export const updateBulkFilterColors = ( { color, teamCodes, newState } ) => ( {
326+ type : reportActions . UPDATE_BULK_FILTER_COLORS ,
327+ payload : { color, teamCodes, newState } ,
328+ } ) ;
0 commit comments