Skip to content

Commit

Permalink
fix: better convert fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Aug 23, 2023
1 parent e4c254a commit 2976bee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/components/Creatives/CreateCreativeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { useField } from "formik";
import { useAdvertiser } from "auth/hooks/queries/useAdvertiser";
import { LoadingButton } from "@mui/lab";
import { transformCreativeFragment } from "user/library";

export function CreateCreativeButton() {
const [, , isCreating] = useField<boolean>("isCreating");
Expand All @@ -21,7 +22,7 @@ export function CreateCreativeButton() {
newHelper.setTouched(false);
creativesHelper.setValue([
...(creativesMeta.value ?? []),
data.createCreative as Creative,
transformCreativeFragment(data.createCreative, advertiser.id),
]);
isCreating.setValue(false);
},
Expand All @@ -38,7 +39,11 @@ export function CreateCreativeButton() {
startIcon={<SaveIcon />}
onClick={(e) => {
e.preventDefault();
create({ variables: { input: { ...newMeta.value } } });
create({
variables: {
input: { ...newMeta.value, advertiserId: advertiser.id },
},
});
}}
disabled={
newMeta.value?.targetUrlValid !== undefined ||
Expand Down
10 changes: 8 additions & 2 deletions src/user/ads/NotificationAd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { Creative } from "user/views/adsManager/types";
import { NotificationPreview } from "components/Creatives/NotificationPreview";
import { CreateCreativeButton } from "components/Creatives/CreateCreativeButton";
import { EditCreativeButton } from "components/Creatives/EditCreativeButton";
import { useEffect } from "react";

export function NotificationAd() {
const [, newMeta] = useField<Creative>("newCreative");
const [, creative] = useField<Creative>("newCreative");
const [, , code] = useField<string>("newCreative.type.code");

useEffect(() => {
code.setValue("notification_all_v1");
}, []);

return (
<CardContainer header="New Ad">
Expand Down Expand Up @@ -42,7 +48,7 @@ export function NotificationAd() {

<Stack direction="row" justifyContent="space-between" mt={1}>
<div />
{newMeta.value.id ? <EditCreativeButton /> : <CreateCreativeButton />}
{creative.value.id ? <EditCreativeButton /> : <CreateCreativeButton />}
</Stack>
</CardContainer>
);
Expand Down
49 changes: 29 additions & 20 deletions src/user/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "user/views/adsManager/types";
import _ from "lodash";
import BigNumber from "bignumber.js";
import { CreativeFragment } from "graphql/creative.generated";

const TYPE_CODE_LOOKUP: Record<string, string> = {
notification_all_v1: "Push Notification",
Expand Down Expand Up @@ -101,13 +102,12 @@ export function transformCreative(
createInput.creativeId = creative.id;
}

createInput.creative = _.omit(creative, [
"createdAt",
"modifiedAt",
"id",
"creativeInstanceId",
"targetUrlValid",
]);
if (createInput.creative) {
createInput.creative = transformCreativeFragment(
createInput.creative,
createInput.creative.advertiserId,
);
}

return createInput;
}
Expand Down Expand Up @@ -179,26 +179,35 @@ function creativeList(
.map((ad) => {
const c = ad.creative;
return {
advertiserId,
...transformCreativeFragment(c, advertiserId),
creativeInstanceId: ad.id,
id: c.id,
name: c.name,
targetUrlValid: "",
state: c.state,
type: { code: c.type.code },
payloadNotification: c.payloadNotification
? {
title: c.payloadNotification.title,
body: c.payloadNotification.body,
targetUrl: c.payloadNotification.targetUrl,
}
: undefined,
};
}),
"id",
);
}

export function transformCreativeFragment(
c: CreativeFragment | Creative,
advertiserId: string,
) {
return {
advertiserId,
id: c.id,
name: c.name,
targetUrlValid: "",
state: c.state,
type: { code: c.type.code },
payloadNotification: c.payloadNotification
? {
title: c.payloadNotification.title,
body: c.payloadNotification.body,
targetUrl: c.payloadNotification.targetUrl,
}
: undefined,
};
}

export function transformEditForm(
form: CampaignForm,
id: string,
Expand Down

0 comments on commit 2976bee

Please sign in to comment.