Skip to content

Commit

Permalink
feat: allow for price-per-advertiser
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 26, 2023
1 parent a084df2 commit 17a430f
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 151 deletions.
93 changes: 93 additions & 0 deletions src/graphql/advertiser.generated.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/graphql/advertiser.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ fragment AdvertiserImage on AdvertiserImage {
createdAt
}

fragment AdvertiserPrice on AdvertiserPrice {
price
billingType
format
}

query advertiserImages($id: String!) {
advertiser(id: $id) {
images {
Expand All @@ -73,6 +79,14 @@ query advertiserImages($id: String!) {
}
}

query advertiserPrices($id: String!) {
advertiser(id: $id) {
prices {
...AdvertiserPrice
}
}
}

mutation uploadAdvertiserImage($input: CreateAdvertiserImageInput!) {
createAdvertiserImage(createImageInput: $input) {
name
Expand Down
22 changes: 18 additions & 4 deletions src/graphql/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/user/hooks/useAdvertiserWithPrices.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useAdvertiser } from "auth/hooks/queries/useAdvertiser";
import {
AdvertiserPriceFragment,
useAdvertiserPricesQuery,
} from "graphql/advertiser.generated";
import { useState } from "react";
import { IAdvertiser } from "auth/context/auth.interface";
import _ from "lodash";

export type AdvertiserWithPrices = IAdvertiser & {
prices: AdvertiserPriceFragment[];
};
export function useAdvertiserWithPrices() {
const { advertiser } = useAdvertiser();
const [data, setData] = useState<AdvertiserWithPrices>({
...advertiser,
prices: [],
});
const [error, setError] = useState<string>();

const { loading } = useAdvertiserPricesQuery({
variables: { id: advertiser.id },
onCompleted(data) {
const prices = data.advertiser?.prices ?? [];
if (_.isEmpty(prices)) {
setError("Unable to create a new campaign");
return;
}

setData({
...advertiser,
prices,
});
},
onError(error) {
setError(error.message);
},
});

return { data, loading, error };
}
19 changes: 15 additions & 4 deletions src/user/views/adsManager/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CampaignFormat, CreativeInput, PaymentType } from "graphql/types";
import { defaultEndDate, defaultStartDate } from "form/DateFieldHelpers";
import { MIN_PER_CAMPAIGN } from "validation/CampaignSchema";
import { IAdvertiser } from "auth/context/auth.interface";
import { AdvertiserWithPrices } from "user/hooks/useAdvertiserWithPrices";

export type Billing = "cpm" | "cpc" | "cpv";

Expand Down Expand Up @@ -105,7 +105,14 @@ export const initialAdSet: AdSetForm = {
creatives: [],
};

export const initialCampaign = (advertiser: IAdvertiser): CampaignForm => {
export const initialCampaign = (
advertiser: AdvertiserWithPrices,
): CampaignForm => {
const format = CampaignFormat.PushNotification;
const billingType = "cpm";
const price = advertiser.prices.find(
(p) => p.billingType === billingType.toUpperCase() && p.format === format,
);
return {
isCreating: false,
advertiserId: advertiser.id,
Expand All @@ -116,15 +123,19 @@ export const initialCampaign = (advertiser: IAdvertiser): CampaignForm => {
dailyBudget: MIN_PER_CAMPAIGN,
geoTargets: [],
newCreative: initialCreative,
billingType: "cpm",
billingType,
currency: "USD",
<<<<<<< HEAD
price: "6",
=======
price: price?.price ?? "6",
>>>>>>> 9018ac3 (feat: allow for price-per-advertiser)
adSets: [
{
...initialAdSet,
},
],
format: CampaignFormat.PushNotification,
format,
name: "",
state: "draft",
type: "paid",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ import {
} from "graphql/campaign.generated";
import { useHistory, useParams } from "react-router-dom";
import { BaseForm } from "./components/BaseForm";
import { useAdvertiser } from "auth/hooks/queries/useAdvertiser";
import { useCreatePaymentSession } from "checkout/hooks/useCreatePaymentSession";
import { ErrorDetail } from "components/Error/ErrorDetail";
import { refetchAdvertiserCampaignsQuery } from "graphql/advertiser.generated";
import { useContext } from "react";
import { FilterContext } from "state/context";
import { useAdvertiserWithPrices } from "user/hooks/useAdvertiserWithPrices";

interface Params {
campaignId: string;
}

export function EditCampaign() {
const { fromDate } = useContext(FilterContext);
const { advertiser } = useAdvertiser();
const history = useHistory();
const params = useParams<Params>();
const { createPaymentSession, loading } = useCreatePaymentSession();
const {
data,
loading: priceLoading,
error: priceError,
} = useAdvertiserWithPrices();

const {
data: initialData,
Expand Down Expand Up @@ -53,27 +57,33 @@ export function EditCampaign() {
refetchQueries: [
{
...refetchAdvertiserCampaignsQuery({
id: advertiser.id,
id: data.id,
filter: { from: fromDate },
}),
},
],
});

if (error) {
if (error || priceError) {
return (
<ErrorDetail
error={error}
error={error ?? priceError}
additionalDetails="Campaign does not exist, or cannot be edited. Please try again later."
/>
);
}

if (!initialData || !initialData.campaign || qLoading || loading) {
if (
!initialData ||
!initialData.campaign ||
qLoading ||
loading ||
priceLoading
) {
return <LinearProgress />;
}

const initialValues = editCampaignValues(initialData.campaign, advertiser.id);
const initialValues = editCampaignValues(initialData.campaign, data.id);
return (
<Container maxWidth="xl">
<Formik
Expand All @@ -84,7 +94,7 @@ export function EditCampaign() {
await mutation({ variables: { input } });
setSubmitting(false);
}}
validationSchema={CampaignSchema}
validationSchema={CampaignSchema(data.prices)}
>
<BaseForm hasPaymentIntent={hasPaymentIntent} />
</Formik>
Expand Down
Loading

0 comments on commit 17a430f

Please sign in to comment.