From a82cadfafc3273a3aab47598bd83d3b0fa417190 Mon Sep 17 00:00:00 2001 From: Ian Krieger Date: Fri, 13 Oct 2023 09:14:03 -0400 Subject: [PATCH] fix: no need for useEffect --- src/components/Creatives/CreativeStatusSwitch.tsx | 11 +++-------- src/components/Switch/OnOff.tsx | 7 ++----- src/hooks/useGetEntityState.tsx | 13 ------------- 3 files changed, 5 insertions(+), 26 deletions(-) delete mode 100644 src/hooks/useGetEntityState.tsx diff --git a/src/components/Creatives/CreativeStatusSwitch.tsx b/src/components/Creatives/CreativeStatusSwitch.tsx index f441579a..9a2c593f 100644 --- a/src/components/Creatives/CreativeStatusSwitch.tsx +++ b/src/components/Creatives/CreativeStatusSwitch.tsx @@ -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; @@ -39,7 +38,7 @@ export function CreativeStatusSwitch({ creative }: Props) { const [relatedCampaigns, setRelatedCampaigns] = useState( [], ); - const [creativeState, setCreativeState] = useGetEntityState(input.state); + const [creativeState, setCreativeState] = useState(input.state); const [update, { loading: updateLoading }] = useUpdateCreativeMutation({ refetchQueries: [ refetchAdvertiserCreativesQuery({ advertiserId: advertiser.id }), @@ -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 }, @@ -87,13 +83,13 @@ export function CreativeStatusSwitch({ creative }: Props) { }, }); }} - checked={creativeState === "active"} + checked={creative.state === "active"} disabled={loading || updateLoading} /> 0}> Are you sure you want to{" "} - {creativeState === "active" ? "activate" : "pause"}{" "} + {creative.state === "active" ? "activate" : "pause"}{" "} {`"${input.name}"`} @@ -116,7 +112,6 @@ export function CreativeStatusSwitch({ creative }: Props) { variant="outlined" onClick={() => { setRelatedCampaigns([]); - setCreativeState(creative.state); }} disabled={updateLoading} > diff --git a/src/components/Switch/OnOff.tsx b/src/components/Switch/OnOff.tsx index 6591af7e..01024a48 100644 --- a/src/components/Switch/OnOff.tsx +++ b/src/components/Switch/OnOff.tsx @@ -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; @@ -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 && @@ -33,7 +31,7 @@ export function OnOff({ isInline ? null : ( - ); - const tooltip = entityState === "paused" ? "Pause" : "Activate"; + const tooltip = state === "paused" ? "Pause" : "Activate"; return ( { const theState = e.target.checked ? "active" : "paused"; - setEntityState(theState); onChange(theState); }} - checked={entityState === "active"} + checked={state === "active"} disabled={loading} /> ) : ( diff --git a/src/hooks/useGetEntityState.tsx b/src/hooks/useGetEntityState.tsx deleted file mode 100644 index 6ab2919f..00000000 --- a/src/hooks/useGetEntityState.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import { Dispatch, useEffect, useState } from "react"; - -export function useGetEntityState( - ephemeralState: string, -): [string, Dispatch] { - const [state, setState] = useState(ephemeralState); - - useEffect(() => { - setState(ephemeralState); - }, [ephemeralState]); - - return [state, setState]; -}