fix(dashboard): Show progress on tabs in create form (#9553)

**What**
- Shows progress on tabs in create promotion form. This is a bit of a band-aid fix, the promotion form and domain needs some proper cleanup, but this will work for now.
- Adds missing translation of "Method" field.

Resolves CC-113
This commit is contained in:
Kasper Fabricius Kristensen
2024-10-14 12:36:31 +02:00
committed by GitHub
parent 224e530453
commit 2a9fc0e514
3 changed files with 64 additions and 32 deletions

View File

@@ -1803,6 +1803,7 @@
"title": "Status" "title": "Status"
}, },
"method": { "method": {
"label": "Method",
"code": { "code": {
"title": "Promotion code", "title": "Promotion code",
"description": "Customers must enter this code at checkout" "description": "Customers must enter this code at checkout"

View File

@@ -7,6 +7,7 @@ import {
CurrencyInput, CurrencyInput,
Heading, Heading,
Input, Input,
ProgressStatus,
ProgressTabs, ProgressTabs,
RadioGroup, RadioGroup,
Text, Text,
@@ -60,9 +61,15 @@ const defaultValues = {
campaign: undefined, campaign: undefined,
} }
type TabState = Record<Tab, ProgressStatus>
export const CreatePromotionForm = () => { export const CreatePromotionForm = () => {
const [tab, setTab] = useState<Tab>(Tab.TYPE) const [tab, setTab] = useState<Tab>(Tab.TYPE)
const [detailsValidated, setDetailsValidated] = useState(false) const [tabState, setTabState] = useState<TabState>({
[Tab.TYPE]: "in-progress",
[Tab.PROMOTION]: "not-started",
[Tab.CAMPAIGN]: "not-started",
})
const { t } = useTranslation() const { t } = useTranslation()
const { handleSuccess } = useRouteModal() const { handleSuccess } = useRouteModal()
@@ -77,9 +84,9 @@ export const CreatePromotionForm = () => {
const handleSubmit = form.handleSubmit( const handleSubmit = form.handleSubmit(
async (data) => { async (data) => {
const { const {
campaign_choice, campaign_choice: _campaignChoice,
is_automatic, is_automatic,
template_id, template_id: _templateId,
application_method, application_method,
rules, rules,
...promotionData ...promotionData
@@ -151,7 +158,7 @@ export const CreatePromotionForm = () => {
) )
}, },
async (error) => { async (error) => {
const { campaign, ...rest } = error || {} const { campaign: _campaign, ...rest } = error || {}
const errorInPromotionTab = !!Object.keys(rest || {}).length const errorInPromotionTab = !!Object.keys(rest || {}).length
if (errorInPromotionTab) { if (errorInPromotionTab) {
@@ -160,39 +167,64 @@ export const CreatePromotionForm = () => {
} }
) )
const handleContinue = async () => { const handleTabChange = async (tab: Tab) => {
switch (tab) { switch (tab) {
case Tab.TYPE: case Tab.TYPE:
setTab(Tab.PROMOTION) setTabState((prev) => ({
...prev,
[Tab.TYPE]: "in-progress",
}))
setTab(tab)
break break
case Tab.PROMOTION: case Tab.PROMOTION:
setTabState((prev) => ({
...prev,
[Tab.TYPE]: "completed",
[Tab.PROMOTION]: "in-progress",
}))
setTab(tab)
break
case Tab.CAMPAIGN: {
const valid = await form.trigger() const valid = await form.trigger()
if (valid) { if (!valid) {
setTab(Tab.CAMPAIGN) // If the promotion tab is not valid, we want to set the tab state to in-progress
} else { // and set the tab to the promotion tab
// TODO: Set errors on the root level setTabState({
[Tab.TYPE]: "completed",
[Tab.PROMOTION]: "in-progress",
[Tab.CAMPAIGN]: "not-started",
})
setTab(Tab.PROMOTION)
break
} }
setTabState((prev) => ({
...prev,
[Tab.PROMOTION]: "completed",
[Tab.CAMPAIGN]: "in-progress",
}))
setTab(tab)
break break
case Tab.CAMPAIGN: }
break
} }
} }
const handleTabChange = (tab: Tab) => { const handleContinue = async () => {
switch (tab) { switch (tab) {
case Tab.TYPE: case Tab.TYPE:
setDetailsValidated(false) handleTabChange(Tab.PROMOTION)
setTab(tab)
break break
case Tab.PROMOTION: case Tab.PROMOTION: {
setDetailsValidated(false) const valid = await form.trigger()
setTab(tab)
if (valid) {
handleTabChange(Tab.CAMPAIGN)
}
break break
}
case Tab.CAMPAIGN: case Tab.CAMPAIGN:
setDetailsValidated(false)
setTab(tab)
break break
} }
} }
@@ -268,14 +300,6 @@ export const CreatePromotionForm = () => {
const { campaigns } = useCampaigns(campaignQuery) const { campaigns } = useCampaigns(campaignQuery)
const detailsProgress = useMemo(() => {
if (detailsValidated) {
return "completed"
}
return "not-started"
}, [detailsValidated])
const watchCampaignChoice = useWatch({ const watchCampaignChoice = useWatch({
control: form.control, control: form.control,
name: "campaign_choice", name: "campaign_choice",
@@ -339,7 +363,7 @@ export const CreatePromotionForm = () => {
<ProgressTabs.Trigger <ProgressTabs.Trigger
className="w-full" className="w-full"
value={Tab.TYPE} value={Tab.TYPE}
status={detailsProgress} status={tabState[Tab.TYPE]}
> >
{t("promotions.tabs.template")} {t("promotions.tabs.template")}
</ProgressTabs.Trigger> </ProgressTabs.Trigger>
@@ -347,11 +371,16 @@ export const CreatePromotionForm = () => {
<ProgressTabs.Trigger <ProgressTabs.Trigger
className="w-full" className="w-full"
value={Tab.PROMOTION} value={Tab.PROMOTION}
status={tabState[Tab.PROMOTION]}
> >
{t("promotions.tabs.details")} {t("promotions.tabs.details")}
</ProgressTabs.Trigger> </ProgressTabs.Trigger>
<ProgressTabs.Trigger className="w-full" value={Tab.CAMPAIGN}> <ProgressTabs.Trigger
className="w-full"
value={Tab.CAMPAIGN}
status={tabState[Tab.CAMPAIGN]}
>
{t("promotions.tabs.campaign")} {t("promotions.tabs.campaign")}
</ProgressTabs.Trigger> </ProgressTabs.Trigger>
</ProgressTabs.List> </ProgressTabs.List>
@@ -439,7 +468,9 @@ export const CreatePromotionForm = () => {
render={({ field }) => { render={({ field }) => {
return ( return (
<Form.Item> <Form.Item>
<Form.Label>Method</Form.Label> <Form.Label>
{t("promotions.form.method.label")}
</Form.Label>
<Form.Control> <Form.Control>
<RadioGroup <RadioGroup

View File

@@ -82,7 +82,7 @@ export const EditPromotionDetailsForm = ({
render={({ field }) => { render={({ field }) => {
return ( return (
<Form.Item> <Form.Item>
<Form.Label>Method</Form.Label> <Form.Label>{t("promotions.form.method.label")}</Form.Label>
<Form.Control> <Form.Control>
<RadioGroup <RadioGroup
className="flex-col gap-y-3" className="flex-col gap-y-3"