Skip to content

Commit

Permalink
Fix promo code (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb authored Nov 8, 2023
1 parent f2fc5e5 commit aa7f9b2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 86 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name: Build, TypeScripts, tests
on:
push:
branches: [canary]
pull_request:
branches: [canary]
on: deployment_status

concurrency:
group: tests-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build_and_test:
if: ${{ github.event.deployment_status.state == 'success' }}

runs-on: ubuntu-latest
env:
NEXT_PUBLIC_SALEOR_API_URL: https://storefront1.saleor.cloud/graphql/
Expand Down
9 changes: 0 additions & 9 deletions src/checkout/hooks/useAlerts/consts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import clsx from "clsx";
import { type TypeOptions } from "react-toastify";

export const alertsContainerProps = {
toastClassName: "rounded shadow-none mb-2 p-0 flex font-sans text-base text-neutral-900 min-h-0 bg-white",
bodyClassName: (data?: { type?: TypeOptions }) =>
clsx("flex w-full items-start px-5 py-3", {
["bg-red-400"]: data?.type === "error",
["bg-green-400"]: data?.type === "success",
}),
autoClose: 4000,
hideProgressBar: true,
closeButton: () => null,
Expand Down
75 changes: 4 additions & 71 deletions src/checkout/hooks/useForm/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,17 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { useFormik, useFormikContext } from "formik";
import { isEqual } from "lodash-es";
import { useCallback, useState } from "react";
import { type ValidationError } from "yup";
import {
type ChangeHandler,
type FormDataBase,
type FormDataField,
type FormProps,
type UseFormReturn,
} from "@/checkout/hooks/useForm/types";
import { type FormDataBase, type FormProps, type UseFormReturn } from "@/checkout/hooks/useForm/types";

export const useForm = <TData extends FormDataBase>({
initialDirty = false,
...formProps
}: FormProps<TData>): UseFormReturn<TData> => {
export const useForm = <TData extends FormDataBase>(formProps: FormProps<TData>) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { validationSchema } = formProps;
// @ts-expect-error because the props we pass and overwrite here don't
// always match what formik wants like e.g validateForm
const form = useFormik<TData>(formProps);
// we do this because in some cases it's not updated properly
// https://github.com/jaredpalmer/formik/issues/3165
const [dirty, setDirty] = useState(initialDirty);
const [formValues, setFormValues] = useState(formProps.initialValues);

const {
handleSubmit: handleFormikSubmit,
handleChange: formikHandleChange,
setErrors: setFormikErrors,
setFieldValue: setFormikFieldValue,
} = form;

const handleSubmit = useCallback(
(event?: React.FormEvent<HTMLFormElement>) => {
event?.preventDefault();

// we do it here because formik doesn't pass props like dirty to onSubmit
if (dirty) {
handleFormikSubmit(event);
}
},
[dirty, handleFormikSubmit],
);

const setValues = useCallback(
(newValues: Partial<TData>) => {
const updatedValues = { ...formValues, ...newValues };
setDirty(!isEqual(formProps.initialValues, updatedValues));
setFormValues(updatedValues);
},
[formProps.initialValues, formValues],
);

const handleChange: ChangeHandler = useCallback(
(event) => {
const { name, value } = event.target;

setValues({ [name]: value } as Partial<TData>);

formikHandleChange(event);
},
[setValues, formikHandleChange],
);

const setFieldValue = async (field: FormDataField<TData>, value: TData[FormDataField<TData>]) => {
if (formValues[field] === value) {
return;
}

await setFormikFieldValue(field, value);
setFormValues({ ...formValues, [field]: value });
};
const { setErrors: setFormikErrors } = form;

const validateForm = (values: TData) => {
if (!validationSchema) {
Expand Down Expand Up @@ -103,14 +42,8 @@ export const useForm = <TData extends FormDataBase>({

return {
...form,
handleSubmit,
handleChange,
values: formValues,
dirty,
setFieldValue,
validateForm,
setValues,
};
} as unknown as UseFormReturn<TData>;
};

export const useFormContext = useFormikContext;
2 changes: 1 addition & 1 deletion src/checkout/sections/Summary/PromoCodeAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const PromoCodeAdd: FC<Classes> = ({ className }) => {
<TextInput required={false} name="promoCode" label="Add gift card or discount code" />
{showApplyButton && (
<Button
className="absolute right-7 top-7"
className="absolute bottom-2.5 right-3"
variant="tertiary"
ariaLabel="apply"
label="Apply"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const SummaryItemMoneyEditableSection: React.FC<SummaryItemMoneyEditableS
onBlur={handleQuantityInputBlur}
name="quantity"
label="Quantity"
className="max-w-[6ch] text-center"
/>
</FormProvider>
</div>
Expand Down

0 comments on commit aa7f9b2

Please sign in to comment.