Skip to content

Commit

Permalink
fix: no need for useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Oct 13, 2023
1 parent f2f1457 commit a82cadf
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
11 changes: 3 additions & 8 deletions src/components/Creatives/CreativeStatusSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { useState } from "react";
import _ from "lodash";
import { validCreativeFields } from "user/library";
import { isReviewableState } from "util/displayState";
import { useGetEntityState } from "hooks/useGetEntityState";

interface Props {
creative: CreativeFragment;
Expand All @@ -39,7 +38,7 @@ export function CreativeStatusSwitch({ creative }: Props) {
const [relatedCampaigns, setRelatedCampaigns] = useState<RelatedCampaign[]>(
[],
);
const [creativeState, setCreativeState] = useGetEntityState(input.state);
const [creativeState, setCreativeState] = useState(input.state);
const [update, { loading: updateLoading }] = useUpdateCreativeMutation({
refetchQueries: [
refetchAdvertiserCreativesQuery({ advertiserId: advertiser.id }),
Expand All @@ -51,9 +50,6 @@ export function CreativeStatusSwitch({ creative }: Props) {
onCompleted() {
setRelatedCampaigns([]);
},
onError() {
setCreativeState(input.state);
},
});
const [campaigns, { loading }] = useCampaignsForCreativeLazyQuery({
variables: { creativeId: creative.id, advertiserId: advertiser.id },
Expand Down Expand Up @@ -87,13 +83,13 @@ export function CreativeStatusSwitch({ creative }: Props) {
},
});
}}
checked={creativeState === "active"}
checked={creative.state === "active"}
disabled={loading || updateLoading}
/>
<Dialog open={relatedCampaigns.length > 0}>
<DialogTitle>
Are you sure you want to{" "}
{creativeState === "active" ? "activate" : "pause"}{" "}
{creative.state === "active" ? "activate" : "pause"}{" "}
{`"${input.name}"`}
</DialogTitle>
<DialogContent>
Expand All @@ -116,7 +112,6 @@ export function CreativeStatusSwitch({ creative }: Props) {
variant="outlined"
onClick={() => {
setRelatedCampaigns([]);
setCreativeState(creative.state);
}}
disabled={updateLoading}
>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Switch/OnOff.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isPast, parseISO } from "date-fns";
import { Switch, Tooltip, Typography } from "@mui/material";
import { CampaignSource } from "graphql/types";
import { useGetEntityState } from "hooks/useGetEntityState";

interface Props {
onChange: (s: string) => void;
Expand All @@ -22,7 +21,6 @@ export function OnOff({
source,
isInline,
}: Props) {
const [entityState, setEntityState] = useGetEntityState(state);
const isAfterEnd = isPast(parseISO(end));
const enabled =
source === CampaignSource.SelfServe &&
Expand All @@ -33,7 +31,7 @@ export function OnOff({
isInline ? null : (
<Typography sx={{ textAlign: "center", p: 0 }}>-</Typography>
);
const tooltip = entityState === "paused" ? "Pause" : "Activate";
const tooltip = state === "paused" ? "Pause" : "Activate";

return (
<Tooltip
Expand All @@ -46,10 +44,9 @@ export function OnOff({
<Switch
onChange={(e) => {
const theState = e.target.checked ? "active" : "paused";
setEntityState(theState);
onChange(theState);
}}
checked={entityState === "active"}
checked={state === "active"}
disabled={loading}
/>
) : (
Expand Down
13 changes: 0 additions & 13 deletions src/hooks/useGetEntityState.tsx

This file was deleted.

0 comments on commit a82cadf

Please sign in to comment.