Skip to content
Open
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
31 changes: 16 additions & 15 deletions frontend/src/modules/products/components/EditProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { CreateProductDTO, ProductDTO } from 'generated-api';
import { Input } from '../../../shared/components/form/Input/Input';
import { useEditProduct } from '../hooks/useEditProduct';
import { productFormValidationSchema } from './ProductForm';
import { useGlobalStore } from '../../../shared/stores';
import { CompanyDTOTypeEnum } from 'generated-api';

interface EditProductFormProps {
product: ProductDTO;
Expand All @@ -25,6 +27,19 @@ export const EditProductForm: React.FC<EditProductFormProps> = ({
},
});
};
const getUser = useGlobalStore((state) => state.getUser);
const companyType = getUser()?.company?.type;

const isSuppManu = () => {
if (
companyType === CompanyDTOTypeEnum.Supplier ||
CompanyDTOTypeEnum.Manufacturer
) {
return true;
} else {
return false;
}
};
Comment on lines +33 to +42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simplify this to just be

Suggested change
const isSuppManu = () => {
if (
companyType === CompanyDTOTypeEnum.Supplier ||
CompanyDTOTypeEnum.Manufacturer
) {
return true;
} else {
return false;
}
};
const isSuppManu = () => {
return companyType === CompanyDTOTypeEnum.Supplier || companyType === CompanyDTOTypeEnum.Manufacturer
};

also, we can't just do:

companyType === CompanyDTOTypeEnum.Supplier || CompanyDTOTypeEnum.Manufacturer

like you had it because what that condition is saying is companyType === CompanyDTOTypeEnum.Supplier (which correctly checks if the company type is SUPPLIER) OR CompanyDTOTypeEnum.Manufacturer (which is just checking if this value is defined). instead, we need to do like in my suggestion above:

companyType === CompanyDTOTypeEnum.Supplier || companyType === CompanyDTOTypeEnum.Manufacturer

this way, we check if companyType === CompanyDTOTypeEnum.Supplier (which checks if company type is SUPPLIER) OR companyType === CompanyDTOTypeEnum.Manufacturer (which checks if company type is MANUFACTURER


return (
<Formik
Expand Down Expand Up @@ -63,21 +78,7 @@ export const EditProductForm: React.FC<EditProductFormProps> = ({
/>
)}
</Field>
<Field name='bulk' type='boolean'>
{(fieldProps: FieldProps<boolean, CreateProductDTO>) => (
<Input
fieldProps={fieldProps}
name='bulk'
label='Bulk'
type='checkbox'
id='bulk'
borderColor='gray.300'
bgColor='gray.50'
color='gray.800'
/>
)}
</Field>
{props.values.bulk && (
{isSuppManu() && (
<Field name='bulkQuantity' type='number'>
{(fieldProps: FieldProps<string, CreateProductDTO>) => (
<Input
Expand Down
35 changes: 18 additions & 17 deletions frontend/src/modules/products/components/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { CreateProductDTO } from 'generated-api';
import * as Yup from 'yup';
import { Input } from '../../../shared/components/form/Input/Input';
import { useCreateProduct } from '../hooks/useCreateProduct';
import { CompanyDTOTypeEnum } from 'generated-api';
import { useGlobalStore } from '../../../shared/stores';

interface ProductFormProps {
companyId: string;
Expand All @@ -30,8 +32,21 @@ export const ProductForm: React.FC<ProductFormProps> = ({
const initialValues: CreateProductDTO = {
name: '',
price: 0,
bulk: false,
bulkQuantity: 0,
bulk: true,
bulkQuantity: undefined,
};
const getUser = useGlobalStore((state) => state.getUser);
const companyType = getUser()?.company?.type;

const isSuppManu = () => {
if (
companyType === CompanyDTOTypeEnum.Supplier ||
CompanyDTOTypeEnum.Manufacturer
) {
return true;
} else {
return false;
}
Comment on lines +41 to +49

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, since we reuse this logic, we can extract it to its own function. preferably, if you could extract this to its own custom hook, that would be great :). alternatively, you can also just extract it to a utility function and pass in the companyType as a parameter.

};

const onSubmit = (dto: CreateProductDTO) => {
Expand Down Expand Up @@ -81,21 +96,7 @@ export const ProductForm: React.FC<ProductFormProps> = ({
/>
)}
</Field>
<Field name='bulk' type='boolean'>
{(fieldProps: FieldProps<boolean, CreateProductDTO>) => (
<Input
fieldProps={fieldProps}
name='bulk'
label='Bulk'
type='checkbox'
id='bulk'
borderColor='gray.300'
bgColor='gray.50'
color='gray.800'
/>
)}
</Field>
{props.values.bulk && (
{isSuppManu() && (
<Field name='bulkQuantity' type='number'>
{(fieldProps: FieldProps<string, CreateProductDTO>) => (
<Input
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/modules/products/components/ProductRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DeleteIcon, EditIcon } from '@chakra-ui/icons';
import {
Tr,
Td,
Checkbox,
Modal,
ModalBody,
ModalCloseButton,
Expand Down Expand Up @@ -46,9 +45,6 @@ export const ProductRow: React.FC<ProductRowProps> = ({ product }) => {
<Link href={`/products/${product.id}`}>{product.name}</Link>
</Td>
<Td isNumeric>{product.price}</Td>
<Td>
<Checkbox defaultChecked={product.bulk} disabled />
</Td>
<Td isNumeric>{product.bulkQuantity ?? 'n/a'}</Td>
<Td>
<HStack spacing='4'>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/products/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const Products = () => {
<Tr>
<Th>Product name</Th>
<Th isNumeric>Price</Th>
<Th>Bulk</Th>
<Th isNumeric>Bulk Quantity</Th>
<Th>Actions</Th>
</Tr>
Expand Down