Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(template campaigns): use number copies field component #1655

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 12 additions & 34 deletions src/components/CreateCampaignFromTemplateDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ import {
} from "@spoke/spoke-codegen";
import React, { useCallback, useMemo, useState } from "react";

import NumberCopiesField from "./NumberCopiesField";

export interface CreateCampaignFromTemplateDialogProps {
organizationId: string;
open: boolean;
onClose?: () => Promise<void> | void;
defaultTemplate?: TemplateCampaignFragment;
}

// eslint-disable-next-line max-len
export const CreateCampaignFromTemplateDialog: React.FC<CreateCampaignFromTemplateDialogProps> = (
props
) => {
const [
selectedTemplate,
setSelectedTemplate
] = useState<TemplateCampaignFragment | null>(props.defaultTemplate ?? null);
const [quantity, setQuantity] = useState<number | null>(1);
const [quantity, setQuantity] = useState<number>(1);
const { data, error } = useGetTemplateCampaignsQuery({
variables: { organizationId: props.organizationId }
});
Expand All @@ -42,32 +45,14 @@ export const CreateCampaignFromTemplateDialog: React.FC<CreateCampaignFromTempla
const templates =
data?.organization?.templateCampaigns?.edges?.map(({ node }) => node) ?? [];

const handleChangeTemplate = useCallback(
(
_event: React.ChangeEvent<unknown>,
value: TemplateCampaignFragment | null
) => {
setSelectedTemplate(value);
},
[setSelectedTemplate]
);
const handleChangeTemplate = (
_event: React.ChangeEvent<unknown>,
value: TemplateCampaignFragment | null
) => setSelectedTemplate(value);

const handleKeyDown: React.KeyboardEventHandler = useCallback((event) => {
const badKey = event.keyCode === 69 || event.keyCode === 101;
if (badKey) event.preventDefault();
}, []);

const handleChangeQuantity: React.ChangeEventHandler<
HTMLInputElement | HTMLTextAreaElement
> = useCallback(
(event) => {
const textValue = event.target.value.replace(/\D/g, "");
const intValue = parseInt(textValue, 10);
const finalValue = Number.isNaN(intValue) ? null : Math.max(1, intValue);
setQuantity(finalValue);
},
[setQuantity]
);
const handleChangeQuantity: React.ChangeEventHandler<HTMLInputElement> = (
event
) => setQuantity(event.target.valueAsNumber);

const canCreate = useMemo(
() => quantity !== null && selectedTemplate !== null && !working,
Expand Down Expand Up @@ -112,14 +97,7 @@ export const CreateCampaignFromTemplateDialog: React.FC<CreateCampaignFromTempla
)}
/>
<br />
<TextField
fullWidth
label="Quantity"
type="number"
value={quantity ?? ""}
onChange={handleChangeQuantity}
onKeyDown={handleKeyDown}
/>
<NumberCopiesField qty={quantity} onChange={handleChangeQuantity} />
</DialogContent>
<DialogActions>
<Button onClick={props.onClose}>Cancel</Button>
Expand Down