-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add existing creative modals to campaign create
- Loading branch information
1 parent
f367d62
commit 72c5d56
Showing
7 changed files
with
247 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
Autocomplete, | ||
Box, | ||
Button, | ||
Checkbox, | ||
TextField, | ||
Typography, | ||
} from "@mui/material"; | ||
import { CreativeFragment } from "graphql/creative.generated"; | ||
import { uiTextForCreativeTypeCode } from "user/library"; | ||
import CheckBoxIcon from "@mui/icons-material/CheckBox"; | ||
import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank"; | ||
import moment from "moment"; | ||
import { useFormikContext } from "formik"; | ||
import { CampaignForm } from "user/views/adsManager/types"; | ||
import _ from "lodash"; | ||
import { useState } from "react"; | ||
|
||
interface CreativeAutocompleteProps { | ||
label: string; | ||
options: readonly CreativeFragment[]; | ||
alreadyAssociatedCreativeIds: string[]; | ||
onSetValue: () => void; | ||
} | ||
|
||
export function CreativeAutocomplete(params: CreativeAutocompleteProps) { | ||
const { setFieldValue } = useFormikContext<CampaignForm>(); | ||
const label = params.label; | ||
const [alreadyAdded, setAlreadyAdded] = useState<string[]>( | ||
params.alreadyAssociatedCreativeIds, | ||
); | ||
|
||
return ( | ||
<Box display="flex" flexDirection="column"> | ||
<Autocomplete | ||
fullWidth | ||
multiple | ||
color="secondary" | ||
autoComplete | ||
disableCloseOnSelect | ||
options={params.options} | ||
onChange={async (_ev, value) => { | ||
const mapped = value.map((c) => c.id); | ||
setAlreadyAdded(mapped); | ||
await setFieldValue("creatives", _.uniq(mapped)); | ||
}} | ||
value={params.options.filter((o) => alreadyAdded.includes(o.id))} | ||
renderInput={(params) => ( | ||
<TextField | ||
variant="outlined" | ||
margin="normal" | ||
label={label} | ||
{...params} | ||
/> | ||
)} | ||
renderOption={(props, option, { selected }) => { | ||
return ( | ||
<li {...props}> | ||
<Checkbox | ||
icon={<CheckBoxOutlineBlankIcon fontSize="small" />} | ||
checkedIcon={<CheckBoxIcon fontSize="small" />} | ||
style={{ marginRight: 8 }} | ||
checked={ | ||
selected || | ||
params.alreadyAssociatedCreativeIds.includes(option?.id) | ||
} | ||
/> | ||
{option.name} | ||
<Typography variant="caption" marginLeft={1} color="GrayText"> | ||
created {moment(option.createdAt).fromNow()} | ||
</Typography> | ||
</li> | ||
); | ||
}} | ||
getOptionLabel={(opt) => opt?.name ?? ""} | ||
getOptionDisabled={(opt) => | ||
params.alreadyAssociatedCreativeIds.includes(opt?.id) | ||
} | ||
groupBy={(opt) => uiTextForCreativeTypeCode(opt.type)} | ||
/> | ||
<Button | ||
size="large" | ||
variant="contained" | ||
sx={{ maxWidth: "165px", alignSelf: "end" }} | ||
onClick={async (e) => { | ||
e.preventDefault(); | ||
params.onSetValue(); | ||
}} | ||
> | ||
Close | ||
</Button> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Typography, Divider } from "@mui/material"; | ||
import { useFormikContext } from "formik"; | ||
import { CampaignFormat } from "graphql/types"; | ||
import _ from "lodash"; | ||
import { | ||
CreativeFragment, | ||
useAdvertiserCreativesQuery, | ||
} from "graphql/creative.generated"; | ||
import { isCreativeTypeApplicableToCampaignFormat } from "user/library"; | ||
import { useAdvertiser } from "auth/hooks/queries/useAdvertiser"; | ||
import { CampaignForm } from "user/views/adsManager/types"; | ||
import { CreativeAutocomplete } from "components/Creatives/CreativeAutocomplete"; | ||
import { CardContainer } from "components/Card/CardContainer"; | ||
|
||
function filterCreativesBasedOnCampaignFormat( | ||
creatives: CreativeFragment[], | ||
campaignFormat: CampaignFormat | null, | ||
): CreativeFragment[] { | ||
if (!campaignFormat) return creatives; | ||
|
||
return creatives.filter((c) => | ||
isCreativeTypeApplicableToCampaignFormat(c.type, campaignFormat), | ||
); | ||
} | ||
|
||
export function AdsNewAd(props: { onAddCreative: () => void }) { | ||
const { values } = useFormikContext<CampaignForm>(); | ||
const { advertiser } = useAdvertiser(); | ||
const { data } = useAdvertiserCreativesQuery({ | ||
variables: { advertiserId: advertiser.id }, | ||
}); | ||
|
||
const allCreativesForAdvertiser = data?.advertiser?.creatives ?? []; | ||
const associatedCreatives = values.creatives ?? []; | ||
const creativeOptionList = _.orderBy( | ||
filterCreativesBasedOnCampaignFormat( | ||
allCreativesForAdvertiser, | ||
values.format, | ||
), | ||
["type.code", "createdAt"], | ||
["asc", "desc"], | ||
) as CreativeFragment[]; | ||
|
||
return ( | ||
<CardContainer header="Existing creative"> | ||
<Typography variant="h1">Add an existing creative</Typography> | ||
|
||
<Divider sx={{ mt: 2, mb: 2 }} /> | ||
|
||
<Typography variant="subtitle2"> | ||
Creatives are modular building blocks that can be paired with ad sets to | ||
build ads. | ||
</Typography> | ||
|
||
<CreativeAutocomplete | ||
label="Creative" | ||
options={creativeOptionList} | ||
alreadyAssociatedCreativeIds={associatedCreatives} | ||
onSetValue={() => { | ||
props.onAddCreative(); | ||
}} | ||
/> | ||
</CardContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters