11import axios from 'axios' ;
2+ import { toast } from 'react-toastify' ;
23import GET_MATERIAL_TYPES , {
34 POST_BUILDING_MATERIAL_INVENTORY_TYPE ,
45 POST_ERROR_BUILDING_MATERIAL_INVENTORY_TYPE ,
@@ -182,11 +183,9 @@ export const fetchToolTypes = () => {
182183 axios
183184 . get ( ENDPOINTS . BM_TOOL_TYPES )
184185 . then ( res => {
185- // console.log("tool types: ", res)
186186 dispatch ( setToolTypes ( res . data ) ) ;
187187 } )
188188 . catch ( err => {
189- // console.log("fetchToolTypes err: ", err)
190189 dispatch ( setErrors ( err ) ) ;
191190 } ) ;
192191 } ;
@@ -201,7 +200,7 @@ export const fetchInvTypeByType = type => {
201200 dispatch ( setInvTypesByType ( { type, data : res . data } ) ) ;
202201 } )
203202 . catch ( err => {
204- dispatch ( setErrors ( err ) ) ;
203+ console . error ( 'Failed to refresh data:' , err ) ;
205204 } ) ;
206205 } ;
207206} ;
@@ -275,14 +274,10 @@ export const postToolsLog = payload => {
275274 axios
276275 . post ( ENDPOINTS . BM_LOG_TOOLS , payload )
277276 . then ( res => {
278- // eslint-disable-next-line no-console
279- // eslint-disable-next-line no-use-before-define
280277 dispatch ( setToolsLogResult ( res . data ) ) ;
281278 } )
282279 . catch ( err => {
283280 dispatch (
284- // setPostErrorToolsLog(JSON.stringify(err.response.data) || 'Sorry! Some error occurred!'),
285- // eslint-disable-next-line no-use-before-define
286281 setPostErrorToolsLog ( err . response . data || 'Sorry! Some error occurred!' ) ,
287282 ) ;
288283 } ) ;
@@ -309,68 +304,94 @@ export const resetPostToolsLog = () => {
309304 } ;
310305} ;
311306
312- // ============ Generic Inventory Type CRUD Actions ============
313-
314- /**
315- * Add a new inventory type
316- * @param {Object } payload - { name, description, category }
317- * @param {string } category - The category (Materials, Consumables, Equipments, Reusables, Tools)
318- */
319- export const addInventoryType = ( payload , category ) => {
307+ export const deleteInvType = ( type , invtypeId ) => {
320308 return async dispatch => {
309+ const toastId = `delete-${ type } -${ Date . now ( ) } ` ;
321310 try {
322- const res = await axios . post ( ENDPOINTS . BM_INVTYPE_ROOT , { ...payload , category } ) ;
323- dispatch ( { type : ADD_INVENTORY_TYPE_SUCCESS , payload : res . data } ) ;
324- // Refresh the list for this category
325- dispatch ( fetchInvTypeByType ( category ) ) ;
326- return { success : true , data : res . data } ;
311+ await axios . delete ( `${ ENDPOINTS . BM_INVTYPE_TYPE ( type ) } /${ invtypeId } ` ) ;
312+ // Refresh the data after successful deletion
313+ dispatch ( fetchInvTypeByType ( type ) ) ;
314+ toast . success ( `${ type . slice ( 0 , - 1 ) } deleted successfully!` , { toastId } ) ;
327315 } catch ( err ) {
328- const errorMsg = err . response ?. data ?. message || err . response ?. data || 'Failed to add inventory type' ;
329- dispatch ( { type : ADD_INVENTORY_TYPE_ERROR , payload : errorMsg } ) ;
330- return { success : false , error : errorMsg } ;
316+ const errorMessage = err . response ?. data ?. error || err . response ?. data ?. message || 'Failed to delete item. Please try again.' ;
317+ toast . error ( errorMessage , { toastId : `delete-error-${ type } -${ Date . now ( ) } ` } ) ;
331318 }
332319 } ;
333320} ;
334321
335- /**
336- * Update an existing inventory type
337- * @param {string } typeId - The ID of the inventory type
338- * @param {Object } payload - { name, description }
339- * @param {string } category - The category to refresh after update
340- */
341- export const updateInventoryType = ( typeId , payload , category ) => {
322+ export const updateInvType = ( type , invtypeId , payload ) => {
342323 return async dispatch => {
324+ const toastId = `update-${ type } -${ Date . now ( ) } ` ;
343325 try {
344- const res = await axios . put ( ENDPOINTS . BM_INVTYPE_BY_ID ( typeId ) , payload ) ;
345- dispatch ( { type : UPDATE_INVENTORY_TYPE_SUCCESS , payload : res . data } ) ;
346- // Refresh the list for this category
347- dispatch ( fetchInvTypeByType ( category ) ) ;
348- return { success : true , data : res . data } ;
326+ await axios . put ( `${ ENDPOINTS . BM_INVTYPE_TYPE ( type ) } /${ invtypeId } ` , payload ) ;
327+ // Refresh the data after successful update
328+ dispatch ( fetchInvTypeByType ( type ) ) ;
329+ toast . success ( `${ type . slice ( 0 , - 1 ) } updated successfully!` , { toastId } ) ;
330+ } catch ( err ) {
331+ const errorMessage = err . response ?. data ?. error || err . response ?. data ?. message || 'Failed to update item. Please try again.' ;
332+ toast . error ( errorMessage , { toastId : `update-error-${ type } -${ Date . now ( ) } ` } ) ;
333+ }
334+ } ;
335+ } ;
336+
337+ export const addInvType = ( type , payload ) => {
338+ let endpoint ;
339+ switch ( type ) {
340+ case 'Materials' :
341+ endpoint = ENDPOINTS . BM_MATERIAL_TYPE ;
342+ break ;
343+ case 'Consumables' :
344+ endpoint = ENDPOINTS . BM_CONSUMABLES ;
345+ break ;
346+ case 'Tools' :
347+ endpoint = ENDPOINTS . BM_TOOLS ;
348+ break ;
349+ case 'Equipments' :
350+ endpoint = ENDPOINTS . BM_EQUIPMENT_INVTYPE ;
351+ break ;
352+ case 'Reusables' :
353+ endpoint = ENDPOINTS . BM_REUSABLES_INVTYPE ;
354+ break ;
355+ default :
356+ endpoint = ENDPOINTS . BM_MATERIAL_TYPE ;
357+ }
358+
359+ return async ( dispatch , getState ) => {
360+ const toastId = `add-${ type } -${ Date . now ( ) } ` ;
361+ const { auth } = getState ( ) ;
362+ const requestorId = auth . user ?. userid ;
363+
364+ const body = { ...payload , requestor : { requestorId } } ;
365+
366+ if ( type === 'Equipments' ) {
367+ body . desc = body . description ;
368+ body . fuel = body . fuel || 'Diesel' ;
369+ }
370+
371+ try {
372+ await axios . post ( endpoint , body ) ;
373+ dispatch ( fetchInvTypeByType ( type ) ) ;
374+ toast . success ( `${ type . slice ( 0 , - 1 ) } added successfully!` , { toastId } ) ;
349375 } catch ( err ) {
350- const errorMsg = err . response ?. data ?. message || err . response ?. data || 'Failed to update inventory type' ;
351- dispatch ( { type : UPDATE_INVENTORY_TYPE_ERROR , payload : errorMsg } ) ;
352- return { success : false , error : errorMsg } ;
376+ const errorMessage = err . response ?. data ?. error || err . response ?. data ?. message || 'Failed to add item. Please try again.' ;
377+ toast . error ( errorMessage , { toastId : `add-error-${ type } -${ Date . now ( ) } ` } ) ;
353378 }
354379 } ;
355380} ;
356381
357382/**
358- * Delete an inventory type
359- * @param {string } typeId - The ID of the inventory type
360- * @param {string } category - The category to refresh after delete
383+ * Delete an inventory type by ID and category.
384+ * Used by DeleteInvTypeModal (param order: typeId, category).
361385 */
362- export const deleteInventoryType = ( typeId , category ) => {
386+ export const deleteInventoryType = ( invtypeId , category ) => {
363387 return async dispatch => {
364388 try {
365- await axios . delete ( ENDPOINTS . BM_INVTYPE_BY_ID ( typeId ) ) ;
366- dispatch ( { type : DELETE_INVENTORY_TYPE_SUCCESS , payload : typeId } ) ;
367- // Refresh the list for this category
389+ await axios . delete ( `${ ENDPOINTS . BM_INVTYPE_TYPE ( category ) } /${ invtypeId } ` ) ;
368390 dispatch ( fetchInvTypeByType ( category ) ) ;
369391 return { success : true } ;
370392 } catch ( err ) {
371- const errorMsg = err . response ?. data ?. message || err . response ?. data || 'Failed to delete inventory type' ;
372- dispatch ( { type : DELETE_INVENTORY_TYPE_ERROR , payload : errorMsg } ) ;
373- return { success : false , error : errorMsg } ;
393+ const errorMessage = err . response ?. data ?. error || err . response ?. data ?. message || 'Failed to delete item.' ;
394+ return { success : false , error : errorMessage } ;
374395 }
375396 } ;
376397} ;
0 commit comments