@@ -32,7 +32,7 @@ export const setErrors = payload => {
3232} ;
3333
3434export const fetchEquipmentById = equipmentId => {
35- const url = ENDPOINTS . BM_EQUIPMENT_BY_ID ( equipmentId ) ;
35+ const url = ` ${ ENDPOINTS . BM_EQUIPMENT_BY_ID ( equipmentId ) } ` ;
3636 return async dispatch => {
3737 axios
3838 . get ( url )
@@ -83,12 +83,12 @@ export const purchaseEquipment = async body => {
8383export const updateMultipleEquipmentLogs = ( projectId , bulkArr ) => dispatch => {
8484 axios
8585 . put (
86- `${ ENDPOINTS . BM_EQUIPMENT_LOGS } ?project=${ projectId } ` ,
86+ `${ ENDPOINTS . BM_EQUIPMENT_LOGS } ?project=${ projectId } ` ,
8787 bulkArr
8888 )
8989 . then ( res => {
90- dispatch ( setEquipments ( res . data ) ) ;
91- toast . success ( 'Equipment logs updated successfully!' ) ;
90+ dispatch ( setEquipments ( res . data ) ) ;
91+ toast . success ( 'Equipment logs updated successfully!' ) ;
9292 return res . data ;
9393 } )
9494 . catch ( err => {
@@ -98,6 +98,71 @@ export const updateMultipleEquipmentLogs = (projectId, bulkArr) => dispatch => {
9898 } ) ;
9999}
100100
101+ export const updateEquipment = ( equipmentId , updateData ) => async ( dispatch , getState ) => {
102+ const url = `${ ENDPOINTS . BM_EQUIPMENT_STATUS_UPDATE ( equipmentId ) } ` ;
103+
104+ try {
105+ const state = getState ( ) ;
106+ let currentUserId = state ?. auth ?. user ?. userid ;
107+
108+ if ( ! currentUserId ) {
109+ currentUserId = state ?. auth ?. user ?. _id ||
110+ state ?. auth ?. user ?. id ||
111+ state ?. auth ?. _id ||
112+ state ?. auth ?. id ;
113+ }
114+
115+ if ( ! currentUserId ) {
116+ const storedUserId = localStorage . getItem ( 'userId' ) ;
117+ if ( storedUserId ) {
118+ currentUserId = storedUserId ;
119+ }
120+ }
121+
122+ if ( ! currentUserId ) {
123+ const errorMsg = 'User not authenticated. Please log in.' ;
124+ toast . error ( errorMsg ) ;
125+ throw new Error ( errorMsg ) ;
126+ }
127+
128+ const statusUpdateData = {
129+ condition : updateData . condition ,
130+ lastUsedBy : updateData . lastUsedBy ,
131+ lastUsedFor : updateData . lastUsedFor ,
132+ replacementRequired : updateData . replacementRequired ,
133+ description : updateData . description || '' ,
134+ notes : updateData . notes || '' ,
135+ createdBy : currentUserId ,
136+ } ;
137+
138+ const res = await axios . put ( url , statusUpdateData ) ;
139+
140+ dispatch ( setEquipment ( res . data ) ) ;
141+ toast . success ( 'Equipment status updated successfully!' ) ;
142+ dispatch ( fetchEquipmentById ( equipmentId ) ) ;
143+
144+ return res . data ;
145+ } catch ( err ) {
146+
147+ let errorMessage = 'Failed to update equipment status.' ;
148+
149+ if ( err . response ) {
150+ errorMessage = err . response . data ?. error ||
151+ err . response . data ?. message ||
152+ err . response . statusText ||
153+ errorMessage ;
154+ dispatch ( setErrors ( err . response . data ) ) ;
155+ } else if ( err . request ) {
156+ errorMessage = 'No response from server. Please check your connection.' ;
157+ } else {
158+ errorMessage = err . message ;
159+ }
160+
161+ toast . error ( errorMessage ) ;
162+ throw err ;
163+ }
164+ } ;
165+
101166export const updateEquipmentById = ( equipmentId , updatedFields ) => async dispatch => {
102167 dispatch ( { type : UPDATE_EQUIPMENT_START } ) ;
103168 try {
0 commit comments