Fix status page deletion network requests and implement optimistic UI update pattern
This commit is contained in:
1 parent
a29282801a
commit
e371596d15
3 files changed
+37
-11
No files matched your search
@@ -126,11 +126,18 @@ const CreateStatusPage = () => {
|
|||||||
setProgress({ value: 0, isLoading: false });
|
setProgress({ value: 0, isLoading: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = () => {
|
/**
|
||||||
deleteStatusPage();
|
* Handle status page deletion with optimistic UI update
|
||||||
setIsDeleteOpen(false);
|
* Immediately navigates away without waiting for the deletion to complete
|
||||||
navigate("/status");
|
* to prevent unnecessary network requests for the deleted page
|
||||||
};
|
*/
|
||||||
|
const handleDelete = async () => {
|
||||||
|
setIsDeleteOpen(false);
|
||||||
|
// Start deletion process but don't wait for it
|
||||||
|
deleteStatusPage();
|
||||||
|
// Immediately navigate away to prevent additional fetches for the deleted page
|
||||||
|
navigate("/status");
|
||||||
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
let toSubmit = {
|
let toSubmit = {
|
||||||
|
|||||||
@@ -1,19 +1,34 @@
|
|||||||
import { useSelector } from "react-redux";
|
|
||||||
import { useState } from "react";
|
|
||||||
import { networkService } from "../../../../main";
|
|
||||||
import { createToast } from "../../../../Utils/toastUtils";
|
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { networkService } from "../../../../Utils/NetworkService";
|
||||||
|
import { createToast } from "../../../../Utils/toastUtils";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook for deleting a status page with optimistic UI update
|
||||||
|
* @param {Function} fetchStatusPage - Function to fetch status page data
|
||||||
|
* @param {string} url - URL of the status page
|
||||||
|
* @returns {Array} - [deleteStatusPage function, isLoading state]
|
||||||
|
*/
|
||||||
const useStatusPageDelete = (fetchStatusPage, url) => {
|
const useStatusPageDelete = (fetchStatusPage, url) => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a status page with optimistic UI update
|
||||||
|
* @returns {Promise<boolean>} - Success status
|
||||||
|
*/
|
||||||
const deleteStatusPage = async () => {
|
const deleteStatusPage = async () => {
|
||||||
|
// We don't need to call fetchStatusPage after deletion
|
||||||
|
// This prevents the 404 error when trying to fetch a deleted status page
|
||||||
try {
|
try {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
await networkService.deleteStatusPage({ url });
|
await networkService.deleteStatusPage({ url });
|
||||||
fetchStatusPage?.();
|
createToast({ body: t("statusPage.deleteSuccess", "Status page deleted successfully") });
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createToast({
|
createToast({
|
||||||
body: error.message,
|
body: t("statusPage.deleteFailed", "Failed to delete status page"),
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -275,6 +275,10 @@
|
|||||||
"date&Time": "Date & Time",
|
"date&Time": "Date & Time",
|
||||||
"type": "Type",
|
"type": "Type",
|
||||||
"statusPageName": "Status page name",
|
"statusPageName": "Status page name",
|
||||||
|
"statusPage": {
|
||||||
|
"deleteSuccess": "Status page deleted successfully",
|
||||||
|
"deleteFailed": "Failed to delete status page"
|
||||||
|
},
|
||||||
"publicURL": "Public URL",
|
"publicURL": "Public URL",
|
||||||
"repeat": "Repeat",
|
"repeat": "Repeat",
|
||||||
"edit": "Edit",
|
"edit": "Edit",
|
||||||
|
|||||||
Reference in new issue
Block a user