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

refactor: Remove lodash #97

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"react/prop-types": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-unused-vars": [
"error",
{
"varsIgnorePattern": "^_",
"argsIgnorePattern": "^_"
}
],
"prettier/prettier": ["error", { "singleQuote": true }],
"react/display-name": 0
},
Expand Down
15 changes: 7 additions & 8 deletions src/modules/catalog/category/CategoryModalContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import _ from 'lodash';
import * as Yup from 'yup';
import { LabeledSelect, Switch, Field } from '@tabetalt/kit';
import { useFormik } from 'formik';
Expand Down Expand Up @@ -34,13 +33,13 @@ export const CategoryModalContent: React.FC<{
const onSubmit = async (
values: CategoryUpdateInput | CategoryCreateInput
) => {
const input = _.pick(
{
...values,
parentId: values.parentId ? Number(values.parentId) : null,
},
['status', 'title', 'showInMainMenu', 'parentId', 'tenantId']
);
const input = {
status: values.status,
title: values.title,
showInMainMenu: values.showInMainMenu,
tenantId: values.tenantId,
parentId: values.parentId ? Number(values.parentId) : null,
};

if (currentCategory) {
await updateCategory({
Expand Down
51 changes: 35 additions & 16 deletions src/modules/catalog/product/ProductUpdate.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import React, { useCallback } from 'react';
import _ from 'lodash';
import { useRoutes, useParams, useNavigate } from 'react-router-dom';
import { Box, Button, Heading } from 'theme-ui';
import { LoaderIcon } from '@tabetalt/kit';
import Layout from '../../../components/layout/Layout';
import ProductBasicOptions from './components/ProductBasicOptions';
import ProductDescription from './components/ProductDescription';
import ProductInventory from './components/ProductInventory';
import ProductCampaign from './components/ProductCampaign';
import ProductVariants from './components/ProductVariants';
import ProductBasicOptions, {
ProductBasicOptionsValues,
} from './components/ProductBasicOptions';
import ProductDescription, {
ProductDescriptionValues,
} from './components/ProductDescription';
import ProductInventory, {
ProductInventoryValues,
} from './components/ProductInventory';
import ProductCampaign, {
ProductCampaignValues,
} from './components/ProductCampaign';
import ProductVariants, {
ProductVariantsValues,
} from './components/ProductVariants';
import ProductNavigation from './components/ProductNavigation';
import { Error } from '../../../components/common';
import { headerLinks } from '../products';
Expand All @@ -20,8 +29,17 @@ import {
useGetProductQuery,
useUpdateProductMutation,
ProductUpdateInput,
GetProductQuery,
} from '../../../generated/graphql';

export type Product = GetProductQuery['product'];
export type InputProductValues =
| ProductBasicOptionsValues
| ProductDescriptionValues
| ProductInventoryValues
| ProductVariantsValues
| ProductCampaignValues;

const ProductUpdate: React.FC = () => {
const navigate = useNavigate();
const params = useParams();
Expand All @@ -45,19 +63,20 @@ const ProductUpdate: React.FC = () => {
});

const onSubmit = useCallback(
async (values) => {
const input = _.omit({ tenantId: 1, ...data?.product, ...values }, [
'id',
'__typename',
'price',
'categories',
]) as ProductUpdateInput;
async (values: Partial<ProductBasicOptionsValues>) => {
const { id: _id, __typename, price, categories, ...inputVars } = {
...data?.product,
...values,
};

const input = inputVars as ProductUpdateInput;

if (values.price) {
input.price = values.price.replace(',', '.') * 100;
if (values.price && typeof values.price === 'string') {
// TODO: This is not the way we should handle monetary values.
input.price = Number(values.price.replace(',', '.')) * 100;
}
if (values.categories) {
input.categories = (values.categories as TagProps[]).map(
input.categories = ((values.categories as unknown) as TagProps[]).map(
({ id, name }) => ({ id: id as number, title: name })
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import _ from 'lodash';
import { Box, Button } from 'theme-ui';
import {
Field,
Expand Down Expand Up @@ -36,14 +35,14 @@ interface ProductBasicOptionsProps {
error?: boolean;
}

interface ProductBasicOptionsValues {
export interface ProductBasicOptionsValues {
title: string;
slug: string;
price: string;
status: ProductStatus;
isOnMainPage: boolean;
categories?: TagProps[];
images?: string[];
images?: { url: string }[];
}

const defaultValues: ProductBasicOptionsValues = {
Expand Down Expand Up @@ -122,7 +121,7 @@ const ProductBasicOptions: React.FC<ProductBasicOptionsProps> = ({

const uploadProductImage = useCallback(
async (files: FileList) => {
_.map(Array.from(files), async ({ name, type, size }, i) => {
Array.from(files).map(async ({name, type, size}, i) => {
const {
data: {
signedUrl: { url, accessUrl },
Expand Down
2 changes: 1 addition & 1 deletion src/modules/catalog/product/components/ProductCampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ProductCampaignProps {
error?: boolean;
}

interface ProductCampaignValues {
export interface ProductCampaignValues {
title: string;
}

Expand Down
7 changes: 4 additions & 3 deletions src/modules/catalog/product/components/ProductDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import * as Yup from 'yup';
import { FormikHelpers, useFormik } from 'formik';
import { Error } from '../../../../components/common';
import { GetProductQuery } from '../../../../generated/graphql';
import { InputProductValues } from '../ProductUpdate';

export type Product = GetProductQuery['product'];

interface ProductDescriptionProps {
product?: Product | null;
onSubmit: (
values: ProductDescriptionValues,
formikHelpers: FormikHelpers<ProductDescriptionValues>
values: InputProductValues,
formikHelpers: FormikHelpers<InputProductValues>
) => void;
error?: boolean;
}

interface ProductDescriptionValues {
export interface ProductDescriptionValues {
shortDescription: string | null;
description: string | null;
technicalDescription: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ const ProductInventory: React.FC<ProductInventoryProps> = ({
);
};

export default ProductInventory;
export default ProductInventory;
2 changes: 1 addition & 1 deletion src/modules/catalog/product/components/ProductVariants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface ProductVariantsProps {
error?: boolean;
}

interface ProductVariantsValues {
export interface ProductVariantsValues {
title: string;
}

Expand Down