Add saving and retreiving currency from localstorage in giftcard create dialog (#1415)
This commit is contained in:
parent
51e204076f
commit
90717b7ed0
6 changed files with 125 additions and 90 deletions
|
@ -27,10 +27,7 @@ const GiftCardCreateDialog: React.FC<DialogActionHandlersProps> = ({
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const notify = useNotifier();
|
const notify = useNotifier();
|
||||||
|
|
||||||
const {
|
const { loading: loadingChannelCurrencies } = useChannelCurrencies({});
|
||||||
data: channelCurrenciesData,
|
|
||||||
loading: loadingChannelCurrencies
|
|
||||||
} = useChannelCurrencies({});
|
|
||||||
|
|
||||||
const [cardCode, setCardCode] = useState(null);
|
const [cardCode, setCardCode] = useState(null);
|
||||||
|
|
||||||
|
@ -116,7 +113,6 @@ const GiftCardCreateDialog: React.FC<DialogActionHandlersProps> = ({
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<GiftCardCreateDialogForm
|
<GiftCardCreateDialogForm
|
||||||
channelCurrencies={channelCurrenciesData?.shop?.channelCurrencies}
|
|
||||||
opts={createGiftCardOpts}
|
opts={createGiftCardOpts}
|
||||||
onClose={handleClose}
|
onClose={handleClose}
|
||||||
apiErrors={createGiftCardOpts?.data?.giftCardCreate?.errors}
|
apiErrors={createGiftCardOpts?.data?.giftCardCreate?.errors}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import VerticalSpacer from "@saleor/apps/components/VerticalSpacer";
|
||||||
import DialogButtons from "@saleor/components/ActionDialog/DialogButtons";
|
import DialogButtons from "@saleor/components/ActionDialog/DialogButtons";
|
||||||
import CardSpacer from "@saleor/components/CardSpacer";
|
import CardSpacer from "@saleor/components/CardSpacer";
|
||||||
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
||||||
import TextWithSelectField from "@saleor/components/TextWithSelectField";
|
|
||||||
import { GiftCardError } from "@saleor/fragments/types/GiftCardError";
|
import { GiftCardError } from "@saleor/fragments/types/GiftCardError";
|
||||||
import GiftCardTagInput from "@saleor/giftCards/components/GiftCardTagInput";
|
import GiftCardTagInput from "@saleor/giftCards/components/GiftCardTagInput";
|
||||||
import useForm from "@saleor/hooks/useForm";
|
import useForm from "@saleor/hooks/useForm";
|
||||||
|
@ -20,23 +19,22 @@ import {
|
||||||
TimePeriodTypeEnum
|
TimePeriodTypeEnum
|
||||||
} from "@saleor/types/globalTypes";
|
} from "@saleor/types/globalTypes";
|
||||||
import { getFormErrors } from "@saleor/utils/errors";
|
import { getFormErrors } from "@saleor/utils/errors";
|
||||||
import { mapSingleValueNodeToChoice } from "@saleor/utils/maps";
|
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
|
|
||||||
import GiftCardSendToCustomer from "../components/GiftCardSendToCustomer/GiftCardSendToCustomer";
|
import GiftCardSendToCustomer from "../components/GiftCardSendToCustomer/GiftCardSendToCustomer";
|
||||||
import { useGiftCardSettingsQuery } from "../GiftCardSettings/queries";
|
import { useGiftCardSettingsQuery } from "../GiftCardSettings/queries";
|
||||||
import { getGiftCardErrorMessage } from "../GiftCardUpdate/messages";
|
import GiftCardCreateDialogMoneyInput from "./GiftCardCreateDialogMoneyInput";
|
||||||
import GiftCardCreateExpirySelect from "./GiftCardCreateExpirySelect";
|
import GiftCardCreateExpirySelect from "./GiftCardCreateExpirySelect";
|
||||||
import { giftCardCreateDialogMessages as messages } from "./messages";
|
import { giftCardCreateDialogMessages as messages } from "./messages";
|
||||||
import { useGiftCardCreateDialogFormStyles as useStyles } from "./styles";
|
import { useGiftCardCreateDialogFormStyles as useStyles } from "./styles";
|
||||||
import {
|
import {
|
||||||
GiftCardCommonFormData,
|
GiftCardCreateFormCommonProps,
|
||||||
GiftCardCreateFormCustomer,
|
GiftCardCreateFormCustomer,
|
||||||
GiftCardExpiryType
|
GiftCardExpiryType
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
export interface GiftCardCreateFormData extends GiftCardCommonFormData {
|
export interface GiftCardCreateFormData {
|
||||||
note: string;
|
note: string;
|
||||||
sendToCustomerSelected: boolean;
|
sendToCustomerSelected: boolean;
|
||||||
selectedCustomer?: GiftCardCreateFormCustomer;
|
selectedCustomer?: GiftCardCreateFormCustomer;
|
||||||
|
@ -46,6 +44,10 @@ export interface GiftCardCreateFormData extends GiftCardCommonFormData {
|
||||||
expiryPeriodType: TimePeriodTypeEnum;
|
expiryPeriodType: TimePeriodTypeEnum;
|
||||||
expiryPeriodAmount: number;
|
expiryPeriodAmount: number;
|
||||||
requiresActivation: boolean;
|
requiresActivation: boolean;
|
||||||
|
tag: string;
|
||||||
|
balanceAmount: number;
|
||||||
|
balanceCurrency: string;
|
||||||
|
expiryDate: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialCustomer = { email: "", name: "" };
|
const initialCustomer = { email: "", name: "" };
|
||||||
|
@ -69,21 +71,17 @@ interface GiftCardCreateDialogFormProps {
|
||||||
apiErrors: GiftCardError[];
|
apiErrors: GiftCardError[];
|
||||||
onSubmit: (data: GiftCardCreateFormData) => void;
|
onSubmit: (data: GiftCardCreateFormData) => void;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
channelCurrencies: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
||||||
onSubmit,
|
onSubmit,
|
||||||
opts,
|
opts,
|
||||||
onClose,
|
onClose,
|
||||||
apiErrors,
|
apiErrors
|
||||||
channelCurrencies
|
|
||||||
}) => {
|
}) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const classes = useStyles({});
|
const classes = useStyles({});
|
||||||
|
|
||||||
const initialCurrency = channelCurrencies[0];
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: settingsData,
|
data: settingsData,
|
||||||
loading: loadingSettings
|
loading: loadingSettings
|
||||||
|
@ -114,11 +112,11 @@ const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const { submit, change, data } = useForm(
|
const { submit, change, data, set } = useForm(
|
||||||
{
|
{
|
||||||
...initialData,
|
...initialData,
|
||||||
...getInitialExpirySettingsData(),
|
...getInitialExpirySettingsData(),
|
||||||
balanceCurrency: initialCurrency,
|
balanceCurrency: "",
|
||||||
channelSlug: ""
|
channelSlug: ""
|
||||||
},
|
},
|
||||||
handleSubmit
|
handleSubmit
|
||||||
|
@ -134,11 +132,8 @@ const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
||||||
sendToCustomerSelected,
|
sendToCustomerSelected,
|
||||||
channelSlug,
|
channelSlug,
|
||||||
balanceAmount,
|
balanceAmount,
|
||||||
balanceCurrency,
|
|
||||||
expirySelected,
|
expirySelected,
|
||||||
expiryType,
|
expiryType,
|
||||||
expiryPeriodAmount,
|
|
||||||
expiryPeriodType,
|
|
||||||
expiryDate,
|
expiryDate,
|
||||||
requiresActivation
|
requiresActivation
|
||||||
} = data;
|
} = data;
|
||||||
|
@ -155,28 +150,16 @@ const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const commonFormProps: GiftCardCreateFormCommonProps = {
|
||||||
|
data,
|
||||||
|
errors: formErrors,
|
||||||
|
change
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<TextWithSelectField
|
<GiftCardCreateDialogMoneyInput {...commonFormProps} set={set} />
|
||||||
isError={!!formErrors?.balance}
|
|
||||||
helperText={getGiftCardErrorMessage(formErrors?.balance, intl)}
|
|
||||||
change={change}
|
|
||||||
choices={mapSingleValueNodeToChoice(channelCurrencies)}
|
|
||||||
containerClassName={classes.balanceContainer}
|
|
||||||
textFieldProps={{
|
|
||||||
type: "float",
|
|
||||||
label: intl.formatMessage(messages.amountLabel),
|
|
||||||
name: "balanceAmount",
|
|
||||||
value: balanceAmount,
|
|
||||||
minValue: 0
|
|
||||||
}}
|
|
||||||
selectFieldProps={{
|
|
||||||
name: "balanceCurrency",
|
|
||||||
value: balanceCurrency || initialCurrency,
|
|
||||||
className: classes.currencySelectField
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<CardSpacer />
|
<CardSpacer />
|
||||||
<GiftCardTagInput
|
<GiftCardTagInput
|
||||||
error={formErrors?.tag}
|
error={formErrors?.tag}
|
||||||
|
@ -195,15 +178,7 @@ const GiftCardCreateDialogForm: React.FC<GiftCardCreateDialogFormProps> = ({
|
||||||
/>
|
/>
|
||||||
<Divider />
|
<Divider />
|
||||||
<VerticalSpacer />
|
<VerticalSpacer />
|
||||||
<GiftCardCreateExpirySelect
|
<GiftCardCreateExpirySelect {...commonFormProps} />
|
||||||
errors={formErrors}
|
|
||||||
change={change}
|
|
||||||
expirySelected={expirySelected}
|
|
||||||
expiryType={expiryType}
|
|
||||||
expiryPeriodAmount={expiryPeriodAmount}
|
|
||||||
expiryPeriodType={expiryPeriodType}
|
|
||||||
expiryDate={expiryDate}
|
|
||||||
/>
|
|
||||||
<VerticalSpacer />
|
<VerticalSpacer />
|
||||||
<TextField
|
<TextField
|
||||||
name="note"
|
name="note"
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
import TextWithSelectField from "@saleor/components/TextWithSelectField";
|
||||||
|
import { ChangeEvent } from "@saleor/hooks/useForm";
|
||||||
|
import useLocalStorage from "@saleor/hooks/useLocalStorage";
|
||||||
|
import { mapSingleValueNodeToChoice } from "@saleor/utils/maps";
|
||||||
|
import * as React from "react";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
|
import { getGiftCardErrorMessage } from "../GiftCardUpdate/messages";
|
||||||
|
import { GiftCardCreateFormData } from "./GiftCardCreateDialogForm";
|
||||||
|
import { giftCardCreateDialogMessages as messages } from "./messages";
|
||||||
|
import { useChannelCurrencies } from "./queries";
|
||||||
|
import { useGiftCardCreateDialogFormStyles as useStyles } from "./styles";
|
||||||
|
import { GiftCardCreateFormCommonProps } from "./types";
|
||||||
|
|
||||||
|
interface GiftCardCreateDialogMoneyInputProps
|
||||||
|
extends GiftCardCreateFormCommonProps {
|
||||||
|
set: (data: Partial<GiftCardCreateFormData>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GiftCardCreateDialogMoneyInput: React.FC<GiftCardCreateDialogMoneyInputProps> = ({
|
||||||
|
errors,
|
||||||
|
data: { balanceAmount, balanceCurrency },
|
||||||
|
change,
|
||||||
|
set
|
||||||
|
}) => {
|
||||||
|
const intl = useIntl();
|
||||||
|
const classes = useStyles({});
|
||||||
|
|
||||||
|
const { data: channelCurrenciesData } = useChannelCurrencies({});
|
||||||
|
|
||||||
|
const { channelCurrencies } = channelCurrenciesData?.shop;
|
||||||
|
|
||||||
|
const [savedCurrency, setCurrency] = useLocalStorage(
|
||||||
|
"giftCardCreateDialogCurrency",
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
|
||||||
|
const getInitialCurrency = () => {
|
||||||
|
if (
|
||||||
|
savedCurrency &&
|
||||||
|
!!channelCurrencies.find((currency: string) => currency === savedCurrency)
|
||||||
|
) {
|
||||||
|
return savedCurrency;
|
||||||
|
}
|
||||||
|
|
||||||
|
return channelCurrencies[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
set({
|
||||||
|
balanceCurrency: getInitialCurrency()
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleInputChange = (event: ChangeEvent<any>) => {
|
||||||
|
if (event.target?.name === "balanceCurrency") {
|
||||||
|
setCurrency(event.target?.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
change(event);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TextWithSelectField
|
||||||
|
isError={!!errors?.balance}
|
||||||
|
helperText={getGiftCardErrorMessage(errors?.balance, intl)}
|
||||||
|
change={handleInputChange}
|
||||||
|
choices={mapSingleValueNodeToChoice(channelCurrencies)}
|
||||||
|
containerClassName={classes.balanceContainer}
|
||||||
|
textFieldProps={{
|
||||||
|
type: "float",
|
||||||
|
label: intl.formatMessage(messages.amountLabel),
|
||||||
|
name: "balanceAmount",
|
||||||
|
value: balanceAmount,
|
||||||
|
minValue: 0
|
||||||
|
}}
|
||||||
|
selectFieldProps={{
|
||||||
|
name: "balanceCurrency",
|
||||||
|
value: balanceCurrency,
|
||||||
|
className: classes.currencySelectField
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GiftCardCreateDialogMoneyInput;
|
|
@ -3,14 +3,14 @@ import HorizontalSpacer from "@saleor/apps/components/HorizontalSpacer";
|
||||||
import VerticalSpacer from "@saleor/apps/components/VerticalSpacer";
|
import VerticalSpacer from "@saleor/apps/components/VerticalSpacer";
|
||||||
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
||||||
import RadioGroupField from "@saleor/components/RadioGroupField";
|
import RadioGroupField from "@saleor/components/RadioGroupField";
|
||||||
import { GiftCardError } from "@saleor/fragments/types/GiftCardError";
|
|
||||||
import TimePeriodField from "@saleor/giftCards/components/TimePeriodField";
|
import TimePeriodField from "@saleor/giftCards/components/TimePeriodField";
|
||||||
import { GiftCardExpiryType } from "@saleor/giftCards/GiftCardCreateDialog/types";
|
import {
|
||||||
|
GiftCardCreateFormCommonProps,
|
||||||
|
GiftCardExpiryType
|
||||||
|
} from "@saleor/giftCards/GiftCardCreateDialog/types";
|
||||||
import { getExpiryPeriodTerminationDate } from "@saleor/giftCards/GiftCardCreateDialog/utils";
|
import { getExpiryPeriodTerminationDate } from "@saleor/giftCards/GiftCardCreateDialog/utils";
|
||||||
import { getGiftCardErrorMessage } from "@saleor/giftCards/GiftCardUpdate/messages";
|
import { getGiftCardErrorMessage } from "@saleor/giftCards/GiftCardUpdate/messages";
|
||||||
import useCurrentDate from "@saleor/hooks/useCurrentDate";
|
import useCurrentDate from "@saleor/hooks/useCurrentDate";
|
||||||
import { FormChange } from "@saleor/hooks/useForm";
|
|
||||||
import { TimePeriodTypeEnum } from "@saleor/types/globalTypes";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { FormattedMessage } from "react-intl";
|
import { FormattedMessage } from "react-intl";
|
||||||
import { MessageDescriptor, useIntl } from "react-intl";
|
import { MessageDescriptor, useIntl } from "react-intl";
|
||||||
|
@ -34,25 +34,16 @@ const options: UntranslatedOption[] = [
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
interface GiftCardCreateExpirySelectProps {
|
const GiftCardCreateExpirySelect: React.FC<GiftCardCreateFormCommonProps> = ({
|
||||||
change: FormChange;
|
|
||||||
expirySelected: boolean;
|
|
||||||
expiryPeriodType: TimePeriodTypeEnum;
|
|
||||||
expiryPeriodAmount: number;
|
|
||||||
expiryType: GiftCardExpiryType;
|
|
||||||
customOptions?: UntranslatedOption[];
|
|
||||||
errors?: Record<"expiryDate", GiftCardError>;
|
|
||||||
expiryDate?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const GiftCardCreateExpirySelect: React.FC<GiftCardCreateExpirySelectProps> = ({
|
|
||||||
errors,
|
errors,
|
||||||
change,
|
change,
|
||||||
expirySelected,
|
data: {
|
||||||
expiryPeriodType,
|
expirySelected,
|
||||||
expiryPeriodAmount,
|
expiryPeriodType,
|
||||||
expiryType,
|
expiryPeriodAmount,
|
||||||
expiryDate
|
expiryType,
|
||||||
|
expiryDate
|
||||||
|
}
|
||||||
}) => {
|
}) => {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const classes = useStyles({});
|
const classes = useStyles({});
|
||||||
|
|
|
@ -10,29 +10,13 @@ export interface GiftCardCreateFormCustomer {
|
||||||
email: string;
|
email: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface GiftCardCommonFormData {
|
|
||||||
tag: string;
|
|
||||||
balanceAmount: number;
|
|
||||||
balanceCurrency: string;
|
|
||||||
expiryDate: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type GiftCardCreateFormErrors = Record<
|
export type GiftCardCreateFormErrors = Record<
|
||||||
"tag" | "expiryDate" | "customer" | "currency" | "amount",
|
"tag" | "expiryDate" | "customer" | "currency" | "amount" | "balance",
|
||||||
GiftCardError
|
GiftCardError
|
||||||
>;
|
>;
|
||||||
|
|
||||||
export type GiftCardCreateInputData = Pick<
|
|
||||||
GiftCardCreateFormData,
|
|
||||||
| "expirySelected"
|
|
||||||
| "expiryDate"
|
|
||||||
| "expiryPeriodAmount"
|
|
||||||
| "expiryPeriodType"
|
|
||||||
| "expiryType"
|
|
||||||
>;
|
|
||||||
|
|
||||||
export interface GiftCardCreateFormCommonProps {
|
export interface GiftCardCreateFormCommonProps {
|
||||||
change: FormChange;
|
change: FormChange;
|
||||||
errors: GiftCardCreateFormErrors;
|
errors: GiftCardCreateFormErrors;
|
||||||
data: GiftCardCommonFormData;
|
data: GiftCardCreateFormData;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { MetadataFormData } from "@saleor/components/Metadata";
|
import { MetadataFormData } from "@saleor/components/Metadata";
|
||||||
import { GiftCardError } from "@saleor/fragments/types/GiftCardError";
|
import { GiftCardError } from "@saleor/fragments/types/GiftCardError";
|
||||||
import { GiftCardCommonFormData } from "@saleor/giftCards/GiftCardCreateDialog/types";
|
|
||||||
import { MutationResultWithOpts } from "@saleor/hooks/makeMutation";
|
import { MutationResultWithOpts } from "@saleor/hooks/makeMutation";
|
||||||
import useForm, { FormChange, UseFormResult } from "@saleor/hooks/useForm";
|
import useForm, { FormChange, UseFormResult } from "@saleor/hooks/useForm";
|
||||||
import useNotifier from "@saleor/hooks/useNotifier";
|
import useNotifier from "@saleor/hooks/useNotifier";
|
||||||
|
@ -18,7 +17,10 @@ import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTr
|
||||||
import React, { createContext } from "react";
|
import React, { createContext } from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
import { initialData as emptyFormData } from "../../../GiftCardCreateDialog/GiftCardCreateDialogForm";
|
import {
|
||||||
|
GiftCardCreateFormData,
|
||||||
|
initialData as emptyFormData
|
||||||
|
} from "../../../GiftCardCreateDialog/GiftCardCreateDialogForm";
|
||||||
import { useGiftCardUpdateMutation } from "../../mutations";
|
import { useGiftCardUpdateMutation } from "../../mutations";
|
||||||
import { GiftCardUpdate } from "../../types/GiftCardUpdate";
|
import { GiftCardUpdate } from "../../types/GiftCardUpdate";
|
||||||
import useGiftCardDetails from "../GiftCardDetailsProvider/hooks/useGiftCardDetails";
|
import useGiftCardDetails from "../GiftCardDetailsProvider/hooks/useGiftCardDetails";
|
||||||
|
@ -28,7 +30,7 @@ interface GiftCardUpdateFormProviderProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GiftCardUpdateFormData = MetadataFormData &
|
export type GiftCardUpdateFormData = MetadataFormData &
|
||||||
Omit<GiftCardCommonFormData, "balanceAmount" | "balanceCurrency">;
|
Pick<GiftCardCreateFormData, "tag" | "expiryDate">;
|
||||||
|
|
||||||
export interface GiftCardUpdateFormConsumerData
|
export interface GiftCardUpdateFormConsumerData
|
||||||
extends GiftCardUpdateFormErrors {
|
extends GiftCardUpdateFormErrors {
|
||||||
|
|
Loading…
Reference in a new issue