Skip to content

Commit

Permalink
Add support for discount type (#4641)
Browse files Browse the repository at this point in the history
* Refactor rule model

* Add changeset

* Add discount type component

* Add type to initial form values

* Bump macaw

* Refactor Add button

* Update test

* Refactor DiscountRule isLoaded

* Add type support

* Remve useeffect

* Add changeset

* Extract messages

* Fix typo
  • Loading branch information
poulch committed Jan 30, 2024
1 parent a210c06 commit f98fd64
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 244 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-panthers-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": minor
---

Add discount type to form
10 changes: 10 additions & 0 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@
"context": "deprecated secret key toolbar label",
"string": "Use RS256 signature instead."
},
"0khVBN": {
"context": "discount type",
"string": "Catalog"
},
"0krqBj": {
"context": "page header",
"string": "Order no. {orderNumber} - Refund"
Expand Down Expand Up @@ -6231,6 +6235,9 @@
"context": "gift card history message",
"string": "Gift card was activated by {activatedBy}"
},
"fKrRhF": {
"string": "General information"
},
"fLCV/w": {
"string": "Add new value"
},
Expand Down Expand Up @@ -8988,6 +8995,9 @@
"context": "header, dialog",
"string": "Create New Warehouse"
},
"z/2AZY": {
"string": "Discount type"
},
"z0gGP+": {
"context": "number of attributes",
"string": "{number} Attributes"
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@material-ui/styles": "^4.11.4",
"@reach/auto-id": "^0.16.0",
"@saleor/macaw-ui": "npm:@saleor/macaw-ui@0.7.4",
"@saleor/macaw-ui-next": "npm:@saleor/macaw-ui@1.0.0-pre.18",
"@saleor/macaw-ui-next": "npm:@saleor/macaw-ui@1.0.0-pre.20",
"@saleor/sdk": "0.6.0",
"@sentry/react": "^7.83.0",
"@types/faker": "^5.1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DiscoutFormData } from "@dashboard/discounts/types";
export const initialFormValues: DiscoutFormData = {
name: "",
description: "",
type: "catalog",
dates: {
endDate: "",
endTime: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getValidationSchema = (intl: IntlShape) => {
name: z
.string()
.min(1, intl.formatMessage(validationMessages.nameRequired)),
type: z.string().optional(),
description: z.string().optional(),
dates: z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useIntl } from "react-intl";
import { DiscountCreateForm } from "../DiscountCreateForm";
import { DiscountDatesWithController } from "../DiscountDates";
import { DiscountDescription } from "../DiscountDescription";
import { DiscountName } from "../DiscountName";
import { DiscountGeneralInfo } from "../DiscountGeneralInfo";
import { DiscountRules, DiscountRulesErrors } from "../DiscountRules";

export interface DiscountCreatePageProps {
Expand Down Expand Up @@ -54,9 +54,10 @@ export const DiscountCreatePage = ({
<DiscountCreateForm onSubmit={onSubmit}>
{({ rules, onDeleteRule, onRuleSubmit, submitHandler }) => (
<>
<DiscountName
<DiscountGeneralInfo
error={getCommonFormFieldErrorMessage(formErrors.name, intl)}
disabled={disabled}
typeDisabled={true}
/>

<DiscountDescription disabled={disabled} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const DiscountDetailsForm = ({
const methods = useForm<DiscoutFormData>({
mode: "onBlur",
values: {
type: "catalog", // TODO: type: data?.type ?? PromotionTypeEnum.CATALOGUE,
dates: {
startDate: splitDateTime(data?.startDate ?? "").date,
startTime: splitDateTime(data?.startDate ?? "").time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useIntl } from "react-intl";
import { DiscountDatesWithController } from "../DiscountDates";
import { DiscountDescription } from "../DiscountDescription";
import { DiscountDetailsForm } from "../DiscountDetailsForm";
import { DiscountName } from "../DiscountName";
import { DiscountGeneralInfo } from "../DiscountGeneralInfo";
import { DiscountRules } from "../DiscountRules";
import { DiscountSavebar } from "../DiscountSavebar";

Expand Down Expand Up @@ -86,9 +86,10 @@ export const DiscountDetailsPage = ({
>
{({ rulesErrors, rules, onDeleteRule, onRuleSubmit, onSubmit }) => (
<>
<DiscountName
<DiscountGeneralInfo
error={getCommonFormFieldErrorMessage(formErrors.name, intl)}
disabled={disabled}
typeDisabled={true}
/>

<DiscountDescription disabled={disabled} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { DashboardCard } from "@dashboard/components/Card";
import { DiscoutFormData } from "@dashboard/discounts/types";
import { Box, Input, Select } from "@saleor/macaw-ui-next";
import React, { useMemo } from "react";
import { useController, useFormContext } from "react-hook-form";
import { FormattedMessage, useIntl } from "react-intl";

interface DiscountNameProps {
disabled?: boolean;
typeDisabled?: boolean;
error: string | undefined;
}

export const DiscountGeneralInfo = ({
disabled,
typeDisabled,
error,
}: DiscountNameProps) => {
const intl = useIntl();
const { formState } = useFormContext<DiscoutFormData>();
const { field: nameField } = useController<DiscoutFormData, "name">({
name: "name",
});
const { field: typeField } = useController<DiscoutFormData, "type">({
name: "type",
});

const discountTypes = useMemo(
() => [
{
label: intl.formatMessage({
defaultMessage: "Catalog",
id: "0khVBN",
description: "discount type",
}),
value: "catalog",
},
// Uncomment when API will support catalog discounts
// {
// label: intl.formatMessage({
// defaultMessage: "Order",
// id: "onUvP+",
// description: "discount type",
// }),
// value: "order",
// },
],
[intl],
);

return (
<DashboardCard>
<DashboardCard.Title>
<FormattedMessage defaultMessage="General information" id="fKrRhF" />
</DashboardCard.Title>
<DashboardCard.Content>
<Box display="grid" gap={3}>
<Input
{...nameField}
error={!!error || !!formState.errors?.name}
helperText={error || formState.errors?.name?.message}
label={intl.formatMessage({
defaultMessage: "Discount name",
id: "lJXkFS",
description: "discount name",
})}
disabled={disabled || nameField.disabled}
/>

<Box __width={250}>
<Select
{...typeField}
size="medium"
options={discountTypes}
label={intl.formatMessage({
defaultMessage: "Discount type",
id: "z/2AZY",
})}
disabled={typeDisabled || nameField.disabled}
/>
</Box>
</Box>
</DashboardCard.Content>
</DashboardCard>
);
};
1 change: 1 addition & 0 deletions src/discounts/components/DiscountGeneralInfo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./DiscountGeneralInfo";
40 changes: 0 additions & 40 deletions src/discounts/components/DiscountName/DiscountName.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/discounts/components/DiscountName/index.ts

This file was deleted.

Loading

0 comments on commit f98fd64

Please sign in to comment.