Skip to content

Commit

Permalink
fix: state switches
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Oct 12, 2023
1 parent cafd27c commit f2f1457
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/Creatives/CreativeStatusSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 @@ -38,7 +39,7 @@ export function CreativeStatusSwitch({ creative }: Props) {
const [relatedCampaigns, setRelatedCampaigns] = useState<RelatedCampaign[]>(
[],
);
const [creativeState, setCreativeState] = useState(input.state);
const [creativeState, setCreativeState] = useGetEntityState(input.state);
const [update, { loading: updateLoading }] = useUpdateCreativeMutation({
refetchQueries: [
refetchAdvertiserCreativesQuery({ advertiserId: advertiser.id }),
Expand Down
10 changes: 5 additions & 5 deletions src/components/Switch/OnOff.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
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 +22,7 @@ export function OnOff({
source,
isInline,
}: Props) {
const [checked, setChecked] = useState(state === "active");
const [entityState, setEntityState] = useGetEntityState(state);
const isAfterEnd = isPast(parseISO(end));
const enabled =
source === CampaignSource.SelfServe &&
Expand All @@ -33,7 +33,7 @@ export function OnOff({
isInline ? null : (
<Typography sx={{ textAlign: "center", p: 0 }}>-</Typography>
);
const tooltip = checked ? "Pause" : "Activate";
const tooltip = entityState === "paused" ? "Pause" : "Activate";

return (
<Tooltip
Expand All @@ -46,10 +46,10 @@ export function OnOff({
<Switch
onChange={(e) => {
const theState = e.target.checked ? "active" : "paused";
setChecked(e.target.checked);
setEntityState(theState);
onChange(theState);
}}
checked={checked}
checked={entityState === "active"}
disabled={loading}
/>
) : (
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/useGetEntityState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Dispatch, useEffect, useState } from "react";

export function useGetEntityState(
ephemeralState: string,
): [string, Dispatch<string>] {
const [state, setState] = useState(ephemeralState);

useEffect(() => {
setState(ephemeralState);
}, [ephemeralState]);

return [state, setState];
}

0 comments on commit f2f1457

Please sign in to comment.