Skip to content

Commit

Permalink
feat(graphql): replace update creative mutations (#1278)
Browse files Browse the repository at this point in the history
Replace old update mutations with ones that make it far more clear
exactly what you are updating when editing a creative
  • Loading branch information
IanKrieger authored Jul 29, 2024
1 parent 74afb5b commit 60dba08
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 124 deletions.
23 changes: 12 additions & 11 deletions src/components/Creatives/CreativeStatusSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ import {
AdvertiserCreativesDocument,
CampaignsForCreativeDocument,
CreativeFragment,
UpdateCreativeDocument,
} from "@/graphql-client/graphql";
import { useLazyQuery, useMutation } from "@apollo/client";
import { graphql } from "@/graphql-client/index";

interface Props {
creative: CreativeFragment;
}

const Creative_State_Update = graphql(`
mutation AdsManagerUpdateCreativeState($id: String!, $state: String!) {
adsManagerUpdateCreativeState(id: $id, state: $state) {
id
}
}
`);

type RelatedCampaign = { id: string; name: string; state: string };
export function CreativeStatusSwitch({ creative }: Props) {
const { advertiser } = useAdvertiser();
Expand All @@ -41,20 +49,13 @@ export function CreativeStatusSwitch({ creative }: Props) {
);
const [creativeState, setCreativeState] = useState(input.state);
const [update, { loading: updateLoading }] = useMutation(
UpdateCreativeDocument,
Creative_State_Update,
{
refetchQueries: [
{
query: AdvertiserCreativesDocument,
variables: { advertiserId: advertiser.id },
},
{
query: CampaignsForCreativeDocument,
variables: {
creativeId: creative.id,
advertiserId: advertiser.id,
},
},
],
onCompleted() {
setRelatedCampaigns([]);
Expand Down Expand Up @@ -86,7 +87,7 @@ export function CreativeStatusSwitch({ creative }: Props) {
update({
variables: {
id: creative.id,
input: { ...input, state: theState },
state: theState,
},
});
}
Expand Down Expand Up @@ -137,7 +138,7 @@ export function CreativeStatusSwitch({ creative }: Props) {
update({
variables: {
id: creative.id,
input: { ...input, state: creativeState },
state: creativeState,
},
});
}}
Expand Down
22 changes: 19 additions & 3 deletions src/components/Creatives/hooks/useSubmitCreative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback } from "react";
import {
AdvertiserCreativesDocument,
CreateCreativeDocument,
UpdateCreativeDocument,
} from "@/graphql-client/graphql";
import { useAdvertiser } from "@/auth/hooks/queries/useAdvertiser";
import { useHistory } from "react-router-dom";
Expand All @@ -11,6 +10,17 @@ import _ from "lodash";
import { useTrackMatomoEvent } from "@/hooks/useTrackWithMatomo";
import { useMutation } from "@apollo/client";
import { CreativeInputWithType } from "@/user/views/adsManager/types";
import { graphql } from "@/graphql-client/index";

const Update_Creative_Payload = graphql(`
mutation AdsManagerUpdateCreativePayload(
$input: AdsManagerUpdateCreativeInput!
) {
adsManagerUpdateCreativePayload(adsManagerUpdateCreativeInput: $input) {
id
}
}
`);

export function useSubmitCreative(props: { id: string }) {
const { trackMatomoEvent } = useTrackMatomoEvent();
Expand Down Expand Up @@ -39,7 +49,7 @@ export function useSubmitCreative(props: { id: string }) {
});

const [updateCreative, { error: updateError, loading: updateLoading }] =
useMutation(UpdateCreativeDocument, {
useMutation(Update_Creative_Payload, {
refetchQueries,
onCompleted,
onError,
Expand All @@ -65,7 +75,13 @@ export function useSubmitCreative(props: { id: string }) {
});
} else {
await updateCreative({
variables: { input: input, id: props.id },
variables: {
input: {
id: props.id,
payloadNotification: input.payloadNotification,
payloadInlineContent: input.payloadInlineContent,
},
},
});
}
} finally {
Expand Down
Loading

0 comments on commit 60dba08

Please sign in to comment.