|
| 1 | +import Button from "@material-ui/core/Button"; |
| 2 | +import Dialog from "@material-ui/core/Dialog"; |
| 3 | +import DialogActions from "@material-ui/core/DialogActions"; |
| 4 | +import DialogContent from "@material-ui/core/DialogContent"; |
| 5 | +import DialogContentText from "@material-ui/core/DialogContentText"; |
| 6 | +import DialogTitle from "@material-ui/core/DialogTitle"; |
| 7 | +import TextField from "@material-ui/core/TextField"; |
| 8 | +import Autocomplete from "@material-ui/lab/Autocomplete"; |
| 9 | +import { |
| 10 | + GetAdminCampaignsDocument, |
| 11 | + TemplateCampaignFragment, |
| 12 | + useCreateCampaignFromTemplateMutation, |
| 13 | + useGetTemplateCampaignsQuery |
| 14 | +} from "@spoke/spoke-codegen"; |
| 15 | +import React, { useCallback, useMemo, useState } from "react"; |
| 16 | + |
| 17 | +export interface CreateCampaignFromTemplateDialogProps { |
| 18 | + organizationId: string; |
| 19 | + open: boolean; |
| 20 | + onClose?: () => Promise<void> | void; |
| 21 | +} |
| 22 | + |
| 23 | +export const CreateCampaignFromTemplateDialog: React.FC<CreateCampaignFromTemplateDialogProps> = ( |
| 24 | + props |
| 25 | +) => { |
| 26 | + const [ |
| 27 | + selectedTemplate, |
| 28 | + setSelectedTemplate |
| 29 | + ] = useState<TemplateCampaignFragment | null>(null); |
| 30 | + const [quantity, setQuantity] = useState<number | null>(1); |
| 31 | + const { data, error } = useGetTemplateCampaignsQuery({ |
| 32 | + variables: { organizationId: props.organizationId } |
| 33 | + }); |
| 34 | + const [ |
| 35 | + createFromTemplate, |
| 36 | + { loading: working } |
| 37 | + ] = useCreateCampaignFromTemplateMutation({ |
| 38 | + refetchQueries: [GetAdminCampaignsDocument] |
| 39 | + }); |
| 40 | + |
| 41 | + const templates = |
| 42 | + data?.organization?.templateCampaigns?.edges?.map(({ node }) => node) ?? []; |
| 43 | + |
| 44 | + const handleChangeTemplate = useCallback( |
| 45 | + ( |
| 46 | + _event: React.ChangeEvent<unknown>, |
| 47 | + value: TemplateCampaignFragment | null |
| 48 | + ) => { |
| 49 | + setSelectedTemplate(value); |
| 50 | + }, |
| 51 | + [setSelectedTemplate] |
| 52 | + ); |
| 53 | + |
| 54 | + const handleKeyDown: React.KeyboardEventHandler = useCallback((event) => { |
| 55 | + const badKey = event.keyCode === 69 || event.keyCode === 101; |
| 56 | + if (badKey) event.preventDefault(); |
| 57 | + }, []); |
| 58 | + |
| 59 | + const handleChangeQuantity: React.ChangeEventHandler< |
| 60 | + HTMLInputElement | HTMLTextAreaElement |
| 61 | + > = useCallback( |
| 62 | + (event) => { |
| 63 | + const textValue = event.target.value.replace(/\D/g, ""); |
| 64 | + const intValue = parseInt(textValue, 10); |
| 65 | + const finalValue = Number.isNaN(intValue) ? null : Math.max(1, intValue); |
| 66 | + setQuantity(finalValue); |
| 67 | + }, |
| 68 | + [setQuantity] |
| 69 | + ); |
| 70 | + |
| 71 | + const canCreate = useMemo( |
| 72 | + () => quantity !== null && selectedTemplate !== null && !working, |
| 73 | + [quantity, selectedTemplate, working] |
| 74 | + ); |
| 75 | + |
| 76 | + const handleClickCreate = useCallback(async () => { |
| 77 | + if (!(quantity !== null && selectedTemplate !== null && !working)) return; |
| 78 | + |
| 79 | + await createFromTemplate({ |
| 80 | + variables: { templateId: selectedTemplate.id, quantity } |
| 81 | + }); |
| 82 | + props.onClose?.(); |
| 83 | + }, [quantity, selectedTemplate, createFromTemplate, props.onClose]); |
| 84 | + |
| 85 | + return ( |
| 86 | + <Dialog |
| 87 | + onClose={props.onClose} |
| 88 | + aria-labelledby="create-from-template-dialog-title" |
| 89 | + open={props.open} |
| 90 | + > |
| 91 | + <DialogTitle id="create-from-template-dialog-title"> |
| 92 | + Create Campaign from Template |
| 93 | + </DialogTitle> |
| 94 | + <DialogContent> |
| 95 | + <DialogContentText> |
| 96 | + Select a campaign template to create from |
| 97 | + </DialogContentText> |
| 98 | + {error && ( |
| 99 | + <DialogContentText> |
| 100 | + Error fetching templates: {error.message} |
| 101 | + </DialogContentText> |
| 102 | + )} |
| 103 | + <Autocomplete |
| 104 | + options={templates} |
| 105 | + getOptionLabel={(template) => template.title} |
| 106 | + value={selectedTemplate} |
| 107 | + onChange={handleChangeTemplate} |
| 108 | + style={{ width: 300 }} |
| 109 | + renderInput={(params) => ( |
| 110 | + <TextField {...params} label="Template campaign name" /> |
| 111 | + )} |
| 112 | + /> |
| 113 | + <br /> |
| 114 | + <TextField |
| 115 | + fullWidth |
| 116 | + label="Quantity" |
| 117 | + type="number" |
| 118 | + value={quantity ?? ""} |
| 119 | + onChange={handleChangeQuantity} |
| 120 | + onKeyDown={handleKeyDown} |
| 121 | + /> |
| 122 | + </DialogContent> |
| 123 | + <DialogActions> |
| 124 | + <Button onClick={props.onClose}>Cancel</Button> |
| 125 | + <Button |
| 126 | + color="primary" |
| 127 | + disabled={!canCreate} |
| 128 | + onClick={handleClickCreate} |
| 129 | + > |
| 130 | + Create |
| 131 | + </Button> |
| 132 | + </DialogActions> |
| 133 | + </Dialog> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export default CreateCampaignFromTemplateDialog; |
0 commit comments