Skip to content

Commit 5374e0d

Browse files
resolving merge conflicts
2 parents 0e97ab2 + 74492fc commit 5374e0d

57 files changed

Lines changed: 4215 additions & 1695 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/actions/bmdashboard/equipmentActions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export const updateMultipleEquipmentLogs = (projectId, bulkArr) => dispatch => {
8888
)
8989
.then(res => {
9090
dispatch(setEquipments(res.data));
91-
toast.success('Equipment logs updated successfully!');
9291
return res.data;
9392
})
9493
.catch(err => {

src/actions/bmdashboard/invTypeActions.js

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { toast } from 'react-toastify';
23
import 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
};

src/actions/bmdashboard/invUnitActions.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { toast } from 'react-toastify';
23

34
import { ENDPOINTS } from '~/utils/URL';
45
import {
@@ -45,21 +46,38 @@ export const fetchInvUnits = () => {
4546
dispatch(setInvUnits(res.data));
4647
})
4748
.catch(err => {
48-
dispatch(setErrors(err));
49+
// Don't show toast for internal refresh calls
4950
});
5051
};
5152
};
5253

5354
export const postBuildingInventoryUnit = payload => {
5455
return async dispatch => {
55-
axios
56-
.post(ENDPOINTS.BM_INVENTORY_UNITS, payload)
57-
.then(res => {
58-
dispatch(setPostInvUnitResult(res.data));
59-
})
60-
.catch(err => {
61-
dispatch(setErrors(err));
62-
});
56+
const toastId = `add-unit-${Date.now()}`;
57+
try {
58+
await axios.post(ENDPOINTS.BM_INVENTORY_UNITS, payload);
59+
// Refresh the data after successful addition
60+
dispatch(fetchInvUnits());
61+
toast.success('Unit added successfully!', { toastId });
62+
} catch (err) {
63+
const errorMessage = err.response?.data?.error || err.response?.data?.message || 'Failed to add unit. Please try again.';
64+
toast.error(errorMessage, { toastId: `add-unit-error-${Date.now()}` });
65+
}
66+
};
67+
};
68+
69+
export const deleteInvUnit = (unitName) => {
70+
return async dispatch => {
71+
const toastId = `delete-unit-${Date.now()}`;
72+
try {
73+
await axios.delete(ENDPOINTS.BM_INVENTORY_UNITS, { data: { unit: unitName } });
74+
// Refresh the data after successful deletion
75+
dispatch(fetchInvUnits());
76+
toast.success('Unit deleted successfully!', { toastId });
77+
} catch (err) {
78+
const errorMessage = err.response?.data?.error || err.response?.data?.message || 'Failed to delete unit. Please try again.';
79+
toast.error(errorMessage, { toastId: `delete-unit-error-${Date.now()}` });
80+
}
6381
};
6482
};
6583

0 commit comments

Comments
 (0)