Skip to content

Commit

Permalink
Use first category as a default value
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Oct 23, 2024
1 parent c8978d5 commit ddcb2c4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/frontend/src/features/addNote/ui/NoteInputFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ import { type FC } from 'react';
import { useAppDispatch, useAppSelector } from '@/app/store';
import { categoryLib } from '@/entities/category';
import { type productModel } from '@/entities/product';
import { type SelectOption } from '@/shared/types';
import { actions } from '../model';
import { type ProductFormValues } from '../model/productForm';
import { ProductForm } from './ProductForm';
import { QuantityForm } from './QuantityForm';
import { SearchProducts } from './SearchProducts';

const toProductFormValues = ({
const toProductFormValues = (
{ name, caloriesCost, defaultQuantity, category }: productModel.AutocompleteFreeSoloOption,
categories: SelectOption[],
): ProductFormValues => ({
name,
caloriesCost,
defaultQuantity,
category,
}: productModel.AutocompleteFreeSoloOption): ProductFormValues => ({
name,
caloriesCost,
defaultQuantity,
// TODO: use first category as a default value
category,
category: categories.at(0) ?? category,
});

export const NoteInputFlow: FC = () => {
const product = useAppSelector(state => state.addNote.draft?.product);
const categorySelectData = categoryLib.useCategorySelectData();
const categorySelect = categoryLib.useCategorySelectData();
const dispatch = useAppDispatch();

if (!product) {
Expand All @@ -33,9 +31,9 @@ export const NoteInputFlow: FC = () => {
if (product.freeSolo && product.editing) {
return (
<ProductForm
defaultValues={toProductFormValues(product)}
categories={categorySelectData.data}
categoriesLoading={categorySelectData.isLoading}
defaultValues={toProductFormValues(product, categorySelect.data)}
categories={categorySelect.data}
categoriesLoading={categorySelect.isLoading}
onSubmit={data => dispatch(actions.productSaved(data))}
/>
);
Expand Down

0 comments on commit ddcb2c4

Please sign in to comment.