Skip to content

Commit

Permalink
bug fixed DeleteItemBody type error
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFrancis12 committed Jul 19, 2024
1 parent 89772f4 commit a63e88e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import { useState } from "react";
import toast from "react-hot-toast";
import { faCheckCircle, faDotCircle, faSpinner, faTrash, faXmarkCircle, IconDefinition } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faCheckCircle, faCircle, faSpinner,
faTrash, faXmarkCircle, IconDefinition
} from "@fortawesome/free-solid-svg-icons";
import { useReportView } from "../../ReportViewContext";
import ActionMenuBodyWrapper from "../ActionMenuBodyWrapper";
import Button from "@/components/Button";
Expand All @@ -17,7 +21,6 @@ import {
EItemName, TAffiliateNetwork, TCampaign, TLandingPage,
TOffer, TPrimaryItemName, TSavedFlow, TTrafficSource
} from "@/lib/types";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

type TDeletionStatus = "idle" | "pending" | "success" | "failed";

Expand All @@ -34,23 +37,21 @@ export default function DeleteItemBody({ actionMenu, setActionMenu }: {
const { primaryItemName, ids } = actionMenu;
const { primaryData } = useReportView();

const [disabled, setDisabled] = useState<boolean>(false);
const [deleting, setDeleting] = useState<boolean>(false);

const key = itemNameToKeyOfPrimaryData(primaryItemName);
const [deletionItems, setDeletionItems] = useState<TDeletionItem[]>(
ids
.map(id => getPrimaryItemById(primaryData, key, id))
.filter(item => !!item)
.map(data => ({
data,
status: "idle",
error: "",
}))
ids.reduce((delItems: TDeletionItem[], id) => {
const data = getPrimaryItemById(primaryData, key, id);
return data
? [...delItems, { data, status: "idle", error: "" }]
: delItems;
}, [])
);

async function handleDelete() {
if (disabled) return;
setDisabled(true);
if (deleting) return;
setDeleting(true);

let deletedIds: number[] = [];
let errCount = 0;
Expand All @@ -70,7 +71,7 @@ export default function DeleteItemBody({ actionMenu, setActionMenu }: {
}

setDeletionItems(prev => prev.filter((delItem => !deletedIds.includes(delItem.data.id))));
setDisabled(false);
setDeleting(false);

if (deletedIds.length > 0) toast.success(`Deleted ${deletedIds.length} item(s)`);

Expand All @@ -90,8 +91,8 @@ export default function DeleteItemBody({ actionMenu, setActionMenu }: {
))}
<Button
text="Yes"
icon={disabled ? faSpinner : faTrash}
disabled={disabled}
icon={deleting ? faSpinner : faTrash}
disabled={deleting}
onClick={handleDelete}
/>
</div>
Expand All @@ -102,12 +103,12 @@ export default function DeleteItemBody({ actionMenu, setActionMenu }: {
type TDeleteItemFunc = (id: number, pathname?: string) => Promise<Object>;

const deleteItemMap: Record<TPrimaryItemName, TDeleteItemFunc> = {
[EItemName.AFFILIATE_NETWORK]: deleteAffiliateNetworkAction as TDeleteItemFunc,
[EItemName.CAMPAIGN]: deleteCampaignAction as TDeleteItemFunc,
[EItemName.FLOW]: deleteFlowAction as TDeleteItemFunc,
[EItemName.LANDING_PAGE]: deleteLandingPageAction as TDeleteItemFunc,
[EItemName.OFFER]: deleteOfferAction as TDeleteItemFunc,
[EItemName.TRAFFIC_SOURCE]: deleteTrafficSourceAction as TDeleteItemFunc,
[EItemName.AFFILIATE_NETWORK]: deleteAffiliateNetworkAction,
[EItemName.CAMPAIGN]: deleteCampaignAction,
[EItemName.FLOW]: deleteFlowAction,
[EItemName.LANDING_PAGE]: deleteLandingPageAction,
[EItemName.OFFER]: deleteOfferAction,
[EItemName.TRAFFIC_SOURCE]: deleteTrafficSourceAction,
};

function statusToIcon(status: TDeletionStatus): IconDefinition {
Expand All @@ -119,6 +120,6 @@ function statusToIcon(status: TDeletionStatus): IconDefinition {
case "failed":
return faXmarkCircle;
default:
return faDotCircle;
return faCircle;
}
}
4 changes: 3 additions & 1 deletion src/app/dashboard/ReportView/LowerControlPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export default function LowerControlPanel({ view, onNewReport, reportItemName, r
setActionMenu({
type: "delete item",
primaryItemName,
ids: selectedRows.map(row => row.id).filter(id => typeof id === "number"),
ids: selectedRows.reduce((ids: number[], { id }) => {
return typeof id === "number" ? [...ids, id] : ids;
}, [])
});
}

Expand Down

0 comments on commit a63e88e

Please sign in to comment.