Skip to content

Commit

Permalink
inventory item crud (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
romanetar committed Dec 20, 2024
1 parent b1e30a0 commit a917db5
Show file tree
Hide file tree
Showing 11 changed files with 851 additions and 164 deletions.
236 changes: 205 additions & 31 deletions src/actions/inventory-item-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ export const RECEIVE_INVENTORY_ITEMS = "RECEIVE_INVENTORY_ITEMS";
export const REQUEST_INVENTORY_ITEMS = "REQUEST_INVENTORY_ITEMS";
export const RESET_INVENTORY_ITEM_FORM = "RESET_INVENTORY_ITEM_FORM";
export const UPDATE_INVENTORY_ITEM = "UPDATE_INVENTORY_ITEM";
export const ADD_INVENTORY_ITEM_IMAGE = "ADD_INVENTORY_ITEM_IMAGE";
export const ADD_INVENTORY_ITEM_META_FIELD_TYPE =
"ADD_INVENTORY_ITEM_META_FIELD_TYPE";
export const INVENTORY_ITEM_META_FIELD_SAVED =
"INVENTORY_ITEM_META_FIELD_SAVED";
export const INVENTORY_ITEM_META_FIELD_DELETED =
"INVENTORY_ITEM_META_FIELD_DELETED";
export const INVENTORY_ITEM_IMAGE_SAVED = "INVENTORY_ITEM_IMAGE_SAVED";
export const INVENTORY_ITEM_IMAGE_DELETED = "INVENTORY_ITEM_IMAGE_DELETED";
export const META_FIELD_VALUE_LOCALLY_ADDED = "META_FIELD_VALUE_LOCALLY_ADDED";
export const META_FIELD_VALUE_LOCALLY_UPDATED =
"META_FIELD_VALUE_LOCALLY_UPDATED";
export const META_FIELD_VALUE_LOCALLY_REMOVED =
"META_FIELD_VALUE_LOCALLY_REMOVED";

export const getInventoryItems =
(
Expand All @@ -61,7 +75,7 @@ export const getInventoryItems =

if (term) {
const escapedTerm = escapeFilterValue(term);
filter.push(`name=@${escapedTerm}`);
filter.push(`name=@${escapedTerm},code=@${escapedTerm}`);
}

const params = {
Expand All @@ -77,8 +91,8 @@ export const getInventoryItems =

// order
if (order != null && orderDir != null) {
const orderDirSign = orderDir === 1 ? "+" : "-";
params.order = `${orderDirSign}${order}`;
const orderDirSign = orderDir === 1 ? "" : "-";
params.ordering = `${orderDirSign}${order}`;
}

return getRequest(
Expand Down Expand Up @@ -138,55 +152,215 @@ export const resetInventoryItemForm = () => (dispatch) => {

export const saveInventoryItem = (entity) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
dispatch(startLoading());

const params = {
access_token: accessToken
access_token: accessToken,
expand: "images,meta_fields,meta_fields.values"
};

dispatch(startLoading());

const normalizedEntity = normalizeEntity(entity);

if (entity.id) {
putRequest(
return putRequest(
createAction(UPDATE_INVENTORY_ITEM),
createAction(INVENTORY_ITEM_UPDATED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${entity.id}/`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch).then(() => {
dispatch(
showSuccessMessage(
T.translate("edit_inventory_item.inventory_item_saved")
)
);
});
} else {
const success_message = {
title: T.translate("general.done"),
html: T.translate("edit_inventory_item.inventory_item_created"),
type: "success"
};
const promises = [];

postRequest(
createAction(ADD_INVENTORY_ITEM),
createAction(INVENTORY_ITEM_ADDED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch).then(() => {
dispatch(
showMessage(success_message, () => {
history.push("/app/sponsors-inventory");
if (normalizedEntity.images.length > 0) {
promises.push(saveInventoryItemImages(normalizedEntity)(dispatch));
}

if (normalizedEntity.meta_fields.length > 0) {
promises.push(
saveInventoryItemMetaFieldTypes(normalizedEntity)(dispatch)
);
}

Promise.all(promises)
.then(() => {
dispatch(
showSuccessMessage(
T.translate("edit_inventory_item.inventory_item_saved")
)
);
})
);
.finally(() => {
dispatch(stopLoading());
});
});
}
const success_message = {
title: T.translate("general.done"),
html: T.translate("edit_inventory_item.inventory_item_created"),
type: "success"
};

return postRequest(
createAction(ADD_INVENTORY_ITEM),
createAction(INVENTORY_ITEM_ADDED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/`,
normalizedEntity,
authErrorHandler,
entity
)(params)(dispatch).then(() => {
const promises = [];

if (normalizedEntity.images.length > 0) {
promises.push(saveInventoryItemImages(normalizedEntity)(dispatch));
}

if (normalizedEntity.meta_fields.length > 0) {
promises.push(
saveInventoryItemMetaFieldTypes(normalizedEntity)(dispatch)
);
}

Promise.all(promises)
.then(() => {
dispatch(
showMessage(success_message, () => {
history.push("/app/sponsors-inventory");
})
);
})
.finally(() => {
dispatch(stopLoading());
});
});
};

const normalizeEntity = (entity) => {
const normalizedEntity = { ...entity };

normalizedEntity.meta_fields = normalizedEntity.meta_fields.map(
(metaField) => ({
...metaField,
is_required: !!metaField.is_required
})
);

return normalizedEntity;
};

/* ************************************ META FIELD TYPES ************************************ */

const saveInventoryItemMetaFieldTypes = (inventoryItem) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
const params = { access_token: accessToken };

const promises = inventoryItem.meta_fields.map((metaFieldType) => {
if (metaFieldType.id) {
return putRequest(
null,
createAction(INVENTORY_ITEM_META_FIELD_SAVED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItem.id}/meta-field-types/${metaFieldType.id}/`,
metaFieldType,
authErrorHandler,
metaFieldType
)(params)(dispatch);
}
return postRequest(
null,
createAction(INVENTORY_ITEM_META_FIELD_SAVED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItem.id}/meta-field-types/`,
metaFieldType,
authErrorHandler,
metaFieldType
)(params)(dispatch);
});

return Promise.all(promises);
};

export const deleteInventoryItemMetaFieldType =
(inventoryItemId, metaFieldId) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();

dispatch(startLoading());

const params = {
access_token: accessToken
};

return deleteRequest(
null,
createAction(INVENTORY_ITEM_META_FIELD_DELETED)({ metaFieldId }),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItemId}/meta-field-types/${metaFieldId}/`,
null,
authErrorHandler
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};

/* ************************************ META FIELD VALUES ************************************ */

export const addMetaFieldValue = (parentId, value) => (dispatch) => {
console.log("addMetaFieldValue", parentId, value);
dispatch(createAction(META_FIELD_VALUE_LOCALLY_ADDED)({ parentId, value }));
};

export const updateMetaFieldValue = (parentId, value) => (dispatch) => {
dispatch(createAction(META_FIELD_VALUE_LOCALLY_UPDATED)({ parentId, value }));
};

export const deleteMetaFieldValue = (parentId, value) => (dispatch) => {
dispatch(createAction(META_FIELD_VALUE_LOCALLY_REMOVED)({ parentId, value }));
};

/* ************************************ IMAGES ************************************ */

const saveInventoryItemImages = (inventoryItem) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();
const params = { access_token: accessToken };

const promises = inventoryItem.images.map((image) => {
if (image.id) {
return putRequest(
null,
createAction(INVENTORY_ITEM_IMAGE_SAVED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItem.id}/images/${image.id}/`,
image,
authErrorHandler,
image
)(params)(dispatch);
}
return postRequest(
null,
createAction(INVENTORY_ITEM_IMAGE_SAVED),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItem.id}/images/`,
image,
authErrorHandler,
image
)(params)(dispatch);
});

return Promise.all(promises);
};

export const deleteInventoryItemImage =
(inventoryItemId, imageId) => async (dispatch) => {
const accessToken = await getAccessTokenSafely();

dispatch(startLoading());

const params = {
access_token: accessToken
};

return deleteRequest(
null,
createAction(INVENTORY_ITEM_IMAGE_DELETED)({ imageId }),
`${window.INVENTORY_API_BASE_URL}/api/v1/inventory-items/${inventoryItemId}/images/${imageId}/`,
null,
authErrorHandler
)(params)(dispatch).then(() => {
dispatch(stopLoading());
});
};
Loading

0 comments on commit a917db5

Please sign in to comment.