Skip to content

Commit

Permalink
[Chore] Resolve undefined errors for failed api calls (#3168)
Browse files Browse the repository at this point in the history
  • Loading branch information
finnar-bin authored Jan 27, 2025
1 parent c80aa67 commit 3086ee7
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 173 deletions.
10 changes: 6 additions & 4 deletions src/apps/code-editor/src/store/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ export function updateFileCode(ZUID, status, code) {

export function fetchFile(fileZUID, fileType, options = { forceSync: false }) {
return (dispatch) => {
return request(`${CONFIG.API_INSTANCE}/web/${fileType}/${fileZUID}`).then(
(res) => {
return request(`${CONFIG.API_INSTANCE}/web/${fileType}/${fileZUID}`)
.then((res) => {
if (res.status === 200) {
dispatch({
type: "FETCH_FILE_SUCCESS",
Expand Down Expand Up @@ -355,8 +355,10 @@ export function fetchFile(fileZUID, fileType, options = { forceSync: false }) {
}

return res;
}
);
})
.catch((err) => {
console.error("fetchFile failed:", err);
});
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/apps/code-editor/src/store/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export function fetchHeaders() {
}
return res;
},
}).catch((err) => {
console.error("fetchHeaders failed:", err);
});
};
}
Expand Down
124 changes: 67 additions & 57 deletions src/apps/content-editor/src/store/navContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,65 +76,75 @@ export function fetchNav() {
return null;
})
.then((granularRoles) => {
return request(`${CONFIG.API_INSTANCE}/env/nav`).then((res) => {
if (res.status === 200) {
// enrich nav with stored closed/hidden status
// FIXME: this should be scoped to the instance
const closed = localStorage.getItem("zesty:navContent:closed");
const closedArr = closed ? JSON.parse(closed) : [];
const closedZUIDS = closedArr.map((node) => node.ZUID);

// FIXME: this should be scoped to the instance
const hidden = localStorage.getItem("zesty:navContent:hidden");
const hiddenArr = hidden ? JSON.parse(hidden) : [];
const hiddenZUIDS = hiddenArr.map((node) => node.ZUID);

const hasContentItemGranularRole = granularRoles?.some((zuid) =>
zuid?.startsWith("7-")
);
// Only filter content items by granular roles if there is at least one granular role that target content items
const filteredByRole = res.data.filter((el) => {
if (granularRoles && hasContentItemGranularRole) {
return granularRoles.find((zuid) => zuid === el.ZUID);
} else {
return el;
}
});

filteredByRole.forEach((node) => {
if (closedZUIDS.includes(node.ZUID)) {
node.closed = true;
}
if (hiddenZUIDS.includes(node.ZUID)) {
node.hidden = true;
}

// Set path
if (node.type === "item") {
node.path = `/content/${node.contentModelZUID}/${node.ZUID}`;
} else if (node.type === "external" || node.type === "internal") {
node.path = `/content/link/${node.ZUID}`;
} else {
node.path = `/content/${node.ZUID}`;
return request(`${CONFIG.API_INSTANCE}/env/nav`)
.then((res) => {
if (res.status === 200) {
// enrich nav with stored closed/hidden status
// FIXME: this should be scoped to the instance
const closed = localStorage.getItem("zesty:navContent:closed");
const closedArr = closed ? JSON.parse(closed) : [];
const closedZUIDS = closedArr.map((node) => node.ZUID);

// FIXME: this should be scoped to the instance
const hidden = localStorage.getItem("zesty:navContent:hidden");
const hiddenArr = hidden ? JSON.parse(hidden) : [];
const hiddenZUIDS = hiddenArr.map((node) => node.ZUID);

const hasContentItemGranularRole = granularRoles?.some((zuid) =>
zuid?.startsWith("7-")
);
// Only filter content items by granular roles if there is at least one granular role that target content items
const filteredByRole = res.data.filter((el) => {
if (granularRoles && hasContentItemGranularRole) {
return granularRoles.find((zuid) => zuid === el.ZUID);
} else {
return el;
}
});

filteredByRole.forEach((node) => {
if (closedZUIDS.includes(node.ZUID)) {
node.closed = true;
}
if (hiddenZUIDS.includes(node.ZUID)) {
node.hidden = true;
}

// Set path
if (node.type === "item") {
node.path = `/content/${node.contentModelZUID}/${node.ZUID}`;
} else if (
node.type === "external" ||
node.type === "internal"
) {
node.path = `/content/link/${node.ZUID}`;
} else {
node.path = `/content/${node.ZUID}`;
}
});

dispatch({
type: "FETCH_CONTENT_NAV_SUCCESS",
raw: filteredByRole,
});
} else {
dispatch(
notify({
message: `Failed to fetch nav`,
kind: "warn",
})
);
if (res.error) {
throw new Error(res.error);
}
});

dispatch({
type: "FETCH_CONTENT_NAV_SUCCESS",
raw: filteredByRole,
});
} else {
dispatch(
notify({
message: `Failed to fetch nav`,
kind: "warn",
})
);
if (res.error) {
throw new Error(res.error);
}
}
});
})
.catch((err) => {
console.error("fetchNav /nav failed:", err);
});
})
.catch((err) => {
console.error("fetchNav /roles failed:", err);
});
};
}
Expand Down
164 changes: 95 additions & 69 deletions src/shell/store/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ export function fetchItem(modelZUID, itemZUID) {
}
return res;
},
}).catch((err) => {
console.error("Failed to fetch item:", err);
return err;
});
};
}
Expand Down Expand Up @@ -359,6 +362,8 @@ export function fetchItems(modelZUID, options = {}) {

return res;
},
}).catch((err) => {
console.error("fetchItems failed:", err);
});
};
}
Expand Down Expand Up @@ -529,37 +534,42 @@ export function saveItem({
web: item.web,
},
}
).then(async (res) => {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch(
instanceApi.util.invalidateTags([{ type: "ItemVersions", itemZUID }])
);
dispatch({
type: "UNMARK_ITEMS_DIRTY",
items: [itemZUID],
});
)
.then(async (res) => {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch(
instanceApi.util.invalidateTags([{ type: "ItemVersions", itemZUID }])
);
dispatch({
type: "UNMARK_ITEMS_DIRTY",
items: [itemZUID],
});

if (res.status === 200) {
await dispatch(fetchItem(item.meta.contentModelZUID, itemZUID));
if (model?.type === "block") {
/*
if (res.status === 200) {
await dispatch(fetchItem(item.meta.contentModelZUID, itemZUID));
if (model?.type === "block") {
/*
Not awaiting this because capturing the screenshot is not critical to the save operation
and we don't want to hold up the user
*/
dispatch(
cloudFunctionsApi.endpoints.createScreenshot.initiate(
`${CONFIG.URL_PREVIEW_PROTOCOL}${itemBlockPreviewUrl}`
)
).then(() =>
dispatch(instanceApi.util.invalidateTags(["ContentItems"]))
);
dispatch(
cloudFunctionsApi.endpoints.createScreenshot.initiate(
`${CONFIG.URL_PREVIEW_PROTOCOL}${itemBlockPreviewUrl}`
)
).then(() =>
dispatch(instanceApi.util.invalidateTags(["ContentItems"]))
);
}
}
}

zesty.trigger("PREVIEW_REFRESH");
zesty.trigger("PREVIEW_REFRESH");

return res;
});
return res;
})
.catch((err) => {
console.error("Failed to save item:", err);
return err;
});
};
}

Expand Down Expand Up @@ -695,28 +705,33 @@ export function createItem({ modelZUID, itemZUID, skipPathPartValidation }) {
web: item.web,
meta: item.meta,
},
}).then(async (res) => {
if (!res.error) {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch({
type: "REMOVE_ITEM",
itemZUID,
});
})
.then(async (res) => {
if (!res.error) {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch({
type: "REMOVE_ITEM",
itemZUID,
});

if (model?.type === "block") {
const newItem = await dispatch(
fetchItem(item.meta.contentModelZUID, res?.data?.ZUID)
);
await dispatch(
saveItem({
itemZUID: res?.data?.ZUID,
itemOverride: newItem?.data,
})
);
if (model?.type === "block") {
const newItem = await dispatch(
fetchItem(item.meta.contentModelZUID, res?.data?.ZUID)
);
await dispatch(
saveItem({
itemZUID: res?.data?.ZUID,
itemOverride: newItem?.data,
})
);
}
}
}
return res;
});
return res;
})
.catch((err) => {
console.error("Failed to create item:", err);
return err;
});
};
}

Expand All @@ -727,29 +742,34 @@ export function deleteItem(modelZUID, itemZUID) {
{
method: "DELETE",
}
).then((res) => {
if (res.status >= 400) {
dispatch(
notify({
message: `Failure deleting item: ${res.statusText}`,
kind: "error",
})
);
} else {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch({
type: "REMOVE_ITEM",
itemZUID,
});
dispatch(
notify({
message: `Successfully deleted item`,
kind: "save",
})
);
}
return res;
});
)
.then((res) => {
if (res.status >= 400) {
dispatch(
notify({
message: `Failure deleting item: ${res.statusText}`,
kind: "error",
})
);
} else {
dispatch(instanceApi.util.invalidateTags(["ContentNav"]));
dispatch({
type: "REMOVE_ITEM",
itemZUID,
});
dispatch(
notify({
message: `Successfully deleted item`,
kind: "save",
})
);
}
return res;
})
.catch((err) => {
console.error("Failed to delete item:", err);
return err;
});
};
}

Expand Down Expand Up @@ -932,7 +952,9 @@ export function checkLock(itemZUID) {
{
credentials: "omit",
}
);
).catch((err) => {
console.error("checkLock failed:", err);
});
};
}

Expand All @@ -943,7 +965,9 @@ export function unlock(itemZUID) {
{
credentials: "omit",
}
);
).catch((err) => {
console.error("unlock failed:", err);
});
};
}

Expand All @@ -962,6 +986,8 @@ export function lock(itemZUID) {
userZUID: user.ZUID,
path: itemZUID,
},
}).catch((err) => {
console.error("unlock failed:", err);
});
}
};
Expand Down
Loading

0 comments on commit 3086ee7

Please sign in to comment.