@@ -14,13 +14,12 @@ import {
1414 CHANGE_USER_PROFILE_PAGE ,
1515 START_USER_INFO_UPDATE ,
1616 FINISH_USER_INFO_UPDATE ,
17- ERROR_USER_INFO_UPDATE
17+ ERROR_USER_INFO_UPDATE ,
1818} from '../constants/userManagement' ;
1919import { ENDPOINTS } from '../utils/URL' ;
2020import { UserStatus } from '../utils/enums' ;
2121import { getTimeEndDateEntriesByPeriod , getTimeStartDateEntriesByPeriod } from './timeEntries' ;
2222
23-
2423/**
2524 * fetching all user profiles
2625 */
@@ -50,32 +49,40 @@ export const updateUserStatus = (user, status, reactivationDate) => {
5049 userProfile . reactivationDate = reactivationDate ;
5150 const patchData = { status, reactivationDate } ;
5251 return async dispatch => {
53- if ( status === UserStatus . InActive ) {
54- try {
55- //Check for last week of work
56- const lastEnddate = await dispatch ( getTimeEndDateEntriesByPeriod ( user . _id , user . createdDate , userProfile . toDate ) ) ;
57- if ( lastEnddate !== "N/A" ) { //if work exists, set EndDate to that week
52+ // Optimistic update
53+ dispatch ( userProfileUpdateAction ( userProfile ) ) ;
54+
55+ try {
56+ if ( status === UserStatus . InActive ) {
57+ // Check for the last week of work
58+ const lastEnddate = await dispatch (
59+ getTimeEndDateEntriesByPeriod ( user . _id , user . createdDate , userProfile . toDate ) ,
60+ ) ;
61+ if ( lastEnddate !== 'N/A' ) {
62+ //if work exists, set EndDate to that week
5863 patchData . endDate = moment ( lastEnddate ) . format ( 'YYYY-MM-DDTHH:mm:ss' ) ;
5964 userProfile . endDate = moment ( lastEnddate ) . format ( 'YYYY-MM-DDTHH:mm:ss' ) ;
60- } else { //No work exists, set end date to start date
65+ } else {
66+ //No work exists, set end date to start date
6167 patchData . endDate = moment ( user . createdDate ) ;
6268 userProfile . endDate = moment ( user . createdDate ) ;
6369 }
64-
65- const updateProfilePromise = axios . patch ( ENDPOINTS . USER_PROFILE ( user . _id ) , patchData ) ;
66- updateProfilePromise . then ( res => {
67- dispatch ( userProfileUpdateAction ( userProfile ) ) ;
68- } ) ;
69- } catch ( error ) {
70- console . error ( "Error updating user status:" , error ) ;
70+ } else {
71+ // User is active
72+ patchData . endDate = undefined ;
73+ userProfile . endDate = undefined ;
7174 }
72- } else { //user is active
73- patchData . endDate = undefined ;
74- userProfile . endDate = undefined ;
75- const updateProfilePromise = axios . patch ( ENDPOINTS . USER_PROFILE ( user . _id ) , patchData ) ;
76- updateProfilePromise . then ( res => {
77- dispatch ( userProfileUpdateAction ( userProfile ) ) ;
78- } ) ;
75+
76+ // Perform the API call
77+ await axios . patch ( ENDPOINTS . USER_PROFILE ( user . _id ) , patchData ) ;
78+
79+ // Ensure the dispatched action is final (optional if optimistic update is sufficient)
80+ dispatch ( userProfileUpdateAction ( userProfile ) ) ;
81+ } catch ( error ) {
82+ console . error ( 'Error updating user status:' , error ) ;
83+
84+ // Rollback to previous state on error
85+ dispatch ( userProfileUpdateAction ( user ) ) ;
7986 }
8087 } ;
8188} ;
@@ -105,18 +112,19 @@ export const updateRehireableStatus = (user, isRehireable) => {
105112 */
106113export const toggleVisibility = ( user , isVisible ) => {
107114 const userProfile = { ...user } ;
108- userProfile . isVisible = isVisible
115+ userProfile . isVisible = isVisible ;
109116 const requestData = { isVisible } ;
110117
111- const toggleVisibilityPromise = axios . patch ( ENDPOINTS . TOGGLE_VISIBILITY ( user . _id ) , requestData )
118+ const toggleVisibilityPromise = axios . patch ( ENDPOINTS . TOGGLE_VISIBILITY ( user . _id ) , requestData ) ;
112119 return async dispatch => {
113- toggleVisibilityPromise . then ( res => {
114- dispatch ( userProfileUpdateAction ( userProfile ) ) ;
115- } ) . catch ( err => {
116- console . error ( "failed to toggle visibility: " , err ) ;
117- } ) ;
120+ toggleVisibilityPromise
121+ . then ( res => {
122+ dispatch ( userProfileUpdateAction ( userProfile ) ) ;
123+ } )
124+ . catch ( err => {
125+ console . error ( 'failed to toggle visibility: ' , err ) ;
126+ } ) ;
118127 } ;
119-
120128} ;
121129
122130/**
@@ -220,24 +228,29 @@ export const updateUserFinalDayStatus = (user, status, finalDayDate) => {
220228} ;
221229
222230export const updateUserFinalDayStatusIsSet = ( user , status , finalDayDate , isSet ) => {
223- const userProfile = { ...user } ;
224- userProfile . endDate = finalDayDate ;
225- userProfile . isActive = status === 'Active' ;
226- userProfile . isSet = isSet === 'FinalDay' ;
227- const patchData = { status, endDate : finalDayDate , isSet } ;
228- if ( finalDayDate === undefined ) {
229- patchData . endDate = undefined ;
230- userProfile . endDate = undefined ;
231- } else {
232- userProfile . endDate = moment ( finalDayDate ) . add ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) ;
233- patchData . endDate = moment ( finalDayDate ) . add ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) ;
234- }
235-
236- const updateProfilePromise = axios . patch ( ENDPOINTS . USER_PROFILE ( user . _id ) , patchData ) ;
237231 return async dispatch => {
238- updateProfilePromise . then ( res => {
239- dispatch ( userProfileUpdateAction ( userProfile ) ) ;
240- } ) ;
232+ try {
233+ // Prepare patch data
234+ const patchData = {
235+ status,
236+ endDate : finalDayDate ? moment ( finalDayDate ) . add ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) : undefined ,
237+ isSet,
238+ } ;
239+
240+ // Make the API request
241+ const response = await axios . patch ( ENDPOINTS . USER_PROFILE ( user . _id ) , patchData ) ;
242+
243+ // Update the Redux store with the response data
244+ const updatedUserProfile = {
245+ ...user ,
246+ ...response . data , // Use the updated data from the API
247+ } ;
248+
249+ dispatch ( userProfileUpdateAction ( updatedUserProfile ) ) ;
250+ } catch ( error ) {
251+ console . error ( 'Error updating user profile:' , error ) ;
252+ throw new Error ( 'Failed to update user profile.' ) ;
253+ }
241254 } ;
242255} ;
243256
@@ -265,7 +278,6 @@ export const getUserProfileBasicInfo = () => {
265278 } ;
266279} ;
267280
268-
269281/**
270282 * Set a flag that starts fetching user profile basic info
271283 */
@@ -297,21 +309,21 @@ export const userProfilesBasicInfoFetchErrorAction = payload => {
297309 } ;
298310} ;
299311
300- export const enableEditUserInfo = ( value ) => ( dispatch , getState ) => {
312+ export const enableEditUserInfo = value => ( dispatch , getState ) => {
301313 dispatch ( { type : ENABLE_USER_PROFILE_EDIT , payload : value } ) ;
302- }
314+ } ;
303315
304- export const disableEditUserInfo = ( value ) => ( dispatch , getState ) => {
316+ export const disableEditUserInfo = value => ( dispatch , getState ) => {
305317 dispatch ( { type : DISABLE_USER_PROFILE_EDIT , payload : value } ) ;
306- }
318+ } ;
307319
308- export const changePagination = ( value ) => ( dispatch , getState ) => {
320+ export const changePagination = value => ( dispatch , getState ) => {
309321 dispatch ( { type : CHANGE_USER_PROFILE_PAGE , payload : value } ) ;
310- }
322+ } ;
311323
312- export const updateUserInfomation = ( value ) => ( dispatch , getState ) => {
313- dispatch ( { type : START_USER_INFO_UPDATE , payload : value } )
314- }
324+ export const updateUserInfomation = value => ( dispatch , getState ) => {
325+ dispatch ( { type : START_USER_INFO_UPDATE , payload : value } ) ;
326+ } ;
315327
316328// export const updateUserInformation=(value)=>async(dispatch,getState)=>{
317329// try {
@@ -324,4 +336,3 @@ export const updateUserInfomation = (value) => (dispatch, getState) => {
324336// dispatch({type:ERROR_USER_INFO_UPDATE})
325337// }
326338// }
327-
0 commit comments