Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/actions/bmdashboard/invTypeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import GET_MATERIAL_TYPES, {
POST_BUILDING_TOOL_INVENTORY_TYPE,
POST_ERROR_BUILDING_TOOL_INVENTORY_TYPE,
RESET_POST_BUILDING_TOOL_INVENTORY_TYPE,
POST_BUILDING_REUSABLE_INVENTORY_TYPE,
POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE,
GET_INV_BY_TYPE,
GET_TOOL_TYPES,
GET_CONSUMABLE_TYPES,
Expand Down Expand Up @@ -131,6 +133,20 @@ export const setPostBuildingToolTypeResult = payload => {
};
};

export const setPostBuildingReusableTypeResult = payload => {
return {
type: POST_BUILDING_REUSABLE_INVENTORY_TYPE,
payload,
};
};

export const setPostErrorBuildingReusableTypeResult = payload => {
return {
type: POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE,
payload,
};
};

export const setPostErrorBuildingInventoryTypeResult = payload => {
return {
type: POST_ERROR_BUILDING_MATERIAL_INVENTORY_TYPE,
Expand Down Expand Up @@ -195,10 +211,11 @@ export const fetchToolTypes = () => {
export const fetchInvTypeByType = type => {
const url = ENDPOINTS.BM_INVTYPE_TYPE(type);
return async dispatch => {
axios
return axios
.get(url)
.then(res => {
dispatch(setInvTypesByType({ type, data: res.data }));
return res;
})
.catch(err => {
dispatch(setErrors(err));
Expand All @@ -208,51 +225,76 @@ export const fetchInvTypeByType = type => {

export const postBuildingConsumableType = payload => {
return async dispatch => {
axios
return axios
.post(ENDPOINTS.BM_CONSUMABLES, payload)
.then(res => {
dispatch(setPostBuildingConsumableTypeResult(res.data));
return res;
})
.catch(err => {
dispatch(
setPostErrorBuildingConsumableTypeResult(
JSON.stringify(err.response.data) || 'Sorry! Some error occurred!',
),
);
throw err;
});
};
};

export const postBuildingToolType = payload => {
return async dispatch => {
axios
return axios
.post(ENDPOINTS.BM_TOOLS, payload)
.then(res => {
dispatch(setPostBuildingToolTypeResult(res.data));
return res;
})
.catch(err => {
dispatch(
setPostErrorBuildingToolTypeResult(
JSON.stringify(err.response.data) || 'Sorry! Some error occurred!',
),
);
throw err;
});
};
};

export const postBuildingReusableType = payload => {
return async dispatch => {
return axios
.post(ENDPOINTS.BM_REUSABLE_TYPE, payload)
.then(res => {
dispatch(setPostBuildingReusableTypeResult(res.data));
return res;
})
.catch(err => {
dispatch(
setPostErrorBuildingReusableTypeResult(
JSON.stringify(err.response?.data) || 'Sorry! Some error occurred!',
),
);
throw err;
});
};
};

export const postBuildingInventoryType = payload => {
return async dispatch => {
axios
return axios
.post(ENDPOINTS.BM_MATERIAL_TYPE, payload)
.then(res => {
dispatch(setPostBuildingInventoryTypeResult(res.data));
return res;
})
.catch(err => {
dispatch(
setPostErrorBuildingInventoryTypeResult(
JSON.stringify(err.response.data) || 'Sorry! Some error occurred!',
),
);
throw err;
});
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import axios from 'axios';
import {
postBuildingInventoryType,
postBuildingToolType,
postBuildingReusableType,
postBuildingConsumableType,
fetchInvTypeByType,
} from '~/actions/bmdashboard/invTypeActions';
Expand Down Expand Up @@ -227,15 +228,22 @@ function AddEditInvTypeFullModal({ isOpen, toggle, category, mode = 'add', itemT
fuel: formData.fuel,
};
response = await axios.put(ENDPOINTS.BM_INVTYPE_BY_ID(itemType._id), payload);
} else if (category === 'Tools' || category === 'Reusables') {
} else if (category === 'Tools') {
const payload = {
...formData,
category: category === 'Tools' ? 'Tool' : 'Reusable',
category: 'Tool',
images: imageURL[0] || itemType?.images || '',
totalPriceWithShipping,
};
// Try tool-specific endpoint first, fallback to generic
response = await axios.put(ENDPOINTS.BM_TOOL_BY_ID(itemType._id), payload);
} else if (category === 'Reusables') {
const payload = {
...formData,
category: 'Reusable',
images: imageURL[0] || itemType?.images || '',
totalPriceWithShipping,
};
response = await axios.put(ENDPOINTS.BM_INVTYPE_BY_ID(itemType._id), payload);
} else {
// Materials, Consumables
const payload = {
Expand All @@ -249,7 +257,7 @@ function AddEditInvTypeFullModal({ isOpen, toggle, category, mode = 'add', itemT

if (response.status === 200) {
toast.success(`${getCategoryLabel()} type updated successfully!`);
dispatch(fetchInvTypeByType(category));
await dispatch(fetchInvTypeByType(category));
toggle();
}
} catch (err) {
Expand Down Expand Up @@ -282,17 +290,29 @@ function AddEditInvTypeFullModal({ isOpen, toggle, category, mode = 'add', itemT
} else {
toast.error(`Error: ${result.status} ${result.statusText}`);
}
} else if (category === 'Tools' || category === 'Reusables') {
} else if (category === 'Tools') {
const payload = {
...formData,
category: 'Tool',
images: imageURL[0] || '',
totalPriceWithShipping,
createdBy: userId,
};
await dispatch(postBuildingToolType(payload));
toast.success('Tool type added successfully!');
await dispatch(fetchInvTypeByType(category));
toggle();
} else if (category === 'Reusables') {
const payload = {
...formData,
category: category === 'Tools' ? 'Tool' : 'Reusable',
category: 'Reusable',
images: imageURL[0] || '',
totalPriceWithShipping,
createdBy: userId,
};
dispatch(postBuildingToolType(payload));
toast.success(`${getCategoryLabel()} type added successfully!`);
dispatch(fetchInvTypeByType(category));
await dispatch(postBuildingReusableType(payload));
toast.success('Reusable type added successfully!');
await dispatch(fetchInvTypeByType(category));
toggle();
} else if (category === 'Consumables') {
const payload = {
Expand All @@ -302,22 +322,21 @@ function AddEditInvTypeFullModal({ isOpen, toggle, category, mode = 'add', itemT
totalPriceWithShipping,
createdBy: userId,
};
dispatch(postBuildingConsumableType(payload));
await dispatch(postBuildingConsumableType(payload));
toast.success('Consumable type added successfully!');
dispatch(fetchInvTypeByType(category));
await dispatch(fetchInvTypeByType(category));
toggle();
} else {
// Materials
const payload = {
...formData,
category: 'Material',
images: imageURL[0] || '',
totalPriceWithShipping,
createdBy: userId,
};
dispatch(postBuildingInventoryType(payload));
await dispatch(postBuildingInventoryType(payload));
toast.success('Material type added successfully!');
dispatch(fetchInvTypeByType(category));
await dispatch(fetchInvTypeByType(category));
toggle();
}
} catch {
Expand Down
5 changes: 5 additions & 0 deletions src/constants/bmdashboard/inventoryTypeConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export const RESET_POST_BUILDING_CONSUMABLE_INVENTORY_TYPE =
export const POST_BUILDING_TOOL_INVENTORY_TYPE = 'POST_BUILDING_TOOL_INVENTORY_TYPE';
export const POST_ERROR_BUILDING_TOOL_INVENTORY_TYPE = 'POST_ERROR_BUILDING_TOOL_INVENTORY_TYPE';
export const RESET_POST_BUILDING_TOOL_INVENTORY_TYPE = 'RESET_POST_BUILDING_TOOL_INVENTORY_TYPE';
export const POST_BUILDING_REUSABLE_INVENTORY_TYPE = 'POST_BUILDING_REUSABLE_INVENTORY_TYPE';
export const POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE =
'POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE';
export const RESET_POST_BUILDING_REUSABLE_INVENTORY_TYPE =
'RESET_POST_BUILDING_REUSABLE_INVENTORY_TYPE';
export const GET_INV_BY_TYPE = 'GET_INV_BY_TYPE';

// Update and Delete constants for inventory types
Expand Down
30 changes: 30 additions & 0 deletions src/reducers/bmdashboard/inventoryTypeReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
POST_BUILDING_TOOL_INVENTORY_TYPE,
POST_ERROR_BUILDING_TOOL_INVENTORY_TYPE,
RESET_POST_BUILDING_TOOL_INVENTORY_TYPE,
POST_BUILDING_REUSABLE_INVENTORY_TYPE,
POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE,
RESET_POST_BUILDING_REUSABLE_INVENTORY_TYPE,
GET_INV_BY_TYPE,
GET_TOOL_TYPES,
GET_CONSUMABLE_TYPES,
Expand Down Expand Up @@ -147,6 +150,33 @@
error: null,
},
};
case POST_BUILDING_REUSABLE_INVENTORY_TYPE:
return {
...state,
postedResult: {
result: action.payload,
success: true,
error: false,
},
};

Check warning on line 161 in src/reducers/bmdashboard/inventoryTypeReducer.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This case's code block is the same as the block for the case on line 52.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZ0vgekLq2Czwd1gszXY&open=AZ0vgekLq2Czwd1gszXY&pullRequest=5074
case POST_ERROR_BUILDING_REUSABLE_INVENTORY_TYPE:
return {
...state,
postedResult: {
result: action.payload,
success: false,
error: true,
},
};

Check warning on line 170 in src/reducers/bmdashboard/inventoryTypeReducer.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This case's code block is the same as the block for the case on line 90.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZ0vgekLq2Czwd1gszXZ&open=AZ0vgekLq2Czwd1gszXZ&pullRequest=5074
case RESET_POST_BUILDING_REUSABLE_INVENTORY_TYPE:
return {
...state,
postedResult: {
result: null,
success: null,
error: null,
},
};

Check warning on line 179 in src/reducers/bmdashboard/inventoryTypeReducer.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This case's code block is the same as the block for the case on line 99.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZ0vgekLq2Czwd1gszXa&open=AZ0vgekLq2Czwd1gszXa&pullRequest=5074
case GET_INV_BY_TYPE: {
state.invTypeList[action.payload.type] = [...action.payload.data];
return { ...state };
Expand Down
1 change: 1 addition & 0 deletions src/utils/URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export const ENDPOINTS = {
BM_CONSUMABLES_PURCHASE: `${APIEndpoint}/bm/consumables/purchase`,
BM_UPDATE_CONSUMABLE_STATUS: `${APIEndpoint}/bm/updateConsumableStatus`,
BM_REUSABLE_TYPES: `${APIEndpoint}/bm/invtypes/reusables`,
BM_REUSABLE_TYPE: `${APIEndpoint}/bm/invtypes/reusable`,
BM_REUSABLES: `${APIEndpoint}/bm/reusables`,
BM_PURCHASE_REUSABLES: `${APIEndpoint}/bm/reusables/purchase`,
BM_EQUIPMENT_TYPES: `${APIEndpoint}/bm/invtypes/equipments`,
Expand Down
Loading