Fix status page deletion network requests and implement optimistic UI update pattern

This commit is contained in:
mohadeseh safari committed 2025-06-24 15:07:37 -04:00
1 parent a29282801a
commit e371596d15
3 files changed
+37 -11

No files matched your search

@@ -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 [isLoading, setIsLoading] = useState(false);
const { t } = useTranslation();
/**
* Delete a status page with optimistic UI update
* @returns {Promise<boolean>} - Success status
*/
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 {
setIsLoading(true);
await networkService.deleteStatusPage({ url });
fetchStatusPage?.();
createToast({ body: t("statusPage.deleteSuccess", "Status page deleted successfully") });
return true;
} catch (error) {
createToast({
body: error.message,
body: t("statusPage.deleteFailed", "Failed to delete status page"),
});
return false;
} finally {