-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add product from autocomplete input (#98)
* Init ProductAutocomplete component * Refactor ProductAutocomplete * Refactor ProductAutocomplete inner dialog * Init ProductInputDialog * Add and edit value on the fly * Load products from API * Allow temporary values that do not exist in options * Added validation props * Added caloriesCost and defaultQuantity to input form * Added category to input form * Fixed input validation * Use different types for autocomplete option * Added test for new ProductInputDialog * ProductAutocomplete tests: select existing option case * ProductAutocomplete tests: add/edit on the fly * Refactored tests * Use new ProductAutocomplete for add/edit note * Create freeSolo product on note submit * Show loading when freeSolo product is creating * Clear previous freeSolo input * Implemented ProductAutocomplete without dialog * WIP: testing ProductAutocompleteWithoutDialog in AddNote * WIP: use InputDialogStateType instead of productEditting toggle * Refactor AddNote state and event handlers * AddNote: added loading progress * Fixed submit disabled validation for note * Fixed submit disabled validation for product * Added remaining fields * Fixed loading progress flickering * Added reusable NoteInputDialog for AddNote and EditNote * NoteInputDialog children -> renderProp * Removed async logic from NoteInputDialog * Fixed tests * Added created product Id to API response * Fixed renderMode for NoteInputDialog * Added tests new NoteInputDialog * Split NoteInputDialog logic into hooks * Disabled refetchOnFocus * Make submit button text shorter * Removed unused code * Removed duplicate EMPTY_DIALOG_VALUE * Renamed 'dialogValue' to 'formValues', added useFormValues hook * Renamed 'productsModel' to 'productModel' * Split useAutocomplete into two hooks * Fixed eslint warning * Move createProductIfNotExists to hook, refactor mappings
- Loading branch information
Showing
59 changed files
with
1,605 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/backend/src/FoodDiary.Application/Products/Create/CreateProductRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FoodDiary.Domain.Entities; | ||
using FoodDiary.Domain.Repositories.v2; | ||
using JetBrains.Annotations; | ||
using MediatR; | ||
|
||
namespace FoodDiary.Application.Products.Create; | ||
|
||
public record CreateProductRequest( | ||
string Name, | ||
int CaloriesCost, | ||
int DefaultQuantity, | ||
int CategoryId) : IRequest<CreateProductResponse>; | ||
|
||
public abstract record CreateProductResponse | ||
{ | ||
public record ProductAlreadyExists : CreateProductResponse; | ||
|
||
public record Success(Product Product) : CreateProductResponse; | ||
} | ||
|
||
[UsedImplicitly] | ||
internal class CreateProductRequestHandler( | ||
IProductsRepository repository) : IRequestHandler<CreateProductRequest, CreateProductResponse> | ||
{ | ||
public async Task<CreateProductResponse> Handle(CreateProductRequest request, CancellationToken cancellationToken) | ||
{ | ||
var productWithTheSameName = await repository.FindByExactName(request.Name, cancellationToken); | ||
|
||
if (productWithTheSameName is not null) | ||
{ | ||
return new CreateProductResponse.ProductAlreadyExists(); | ||
} | ||
|
||
var product = new Product | ||
{ | ||
Name = request.Name, | ||
CaloriesCost = request.CaloriesCost, | ||
DefaultQuantity = request.DefaultQuantity, | ||
CategoryId = request.CategoryId | ||
}; | ||
|
||
await repository.Create(product, cancellationToken); | ||
|
||
return new CreateProductResponse.Success(product); | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
src/backend/src/FoodDiary.Application/Products/Handlers/CreateProductRequestHandler.cs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/backend/src/FoodDiary.Application/Products/Requests/CreateProductRequest.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/backend/src/FoodDiary.Contracts/Products/CreateProductResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace FoodDiary.Contracts.Products; | ||
|
||
[PublicAPI] | ||
public record CreateProductResponse(int Id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ui'; | ||
export * as productModel from './model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type FormValues } from './types'; | ||
|
||
export const EMPTY_FORM_VALUES: FormValues = { | ||
name: '', | ||
defaultQuantity: 100, | ||
caloriesCost: 100, | ||
category: null, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './types'; | ||
export * from './constants'; | ||
export * from './useAutocompleteData'; | ||
export * from './useAutocompleteInput'; | ||
export * from './useFormValues'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type SelectOption } from '@/types'; | ||
|
||
export interface FormValues { | ||
name: string; | ||
caloriesCost: number; | ||
defaultQuantity: number; | ||
category: SelectOption | null; | ||
} | ||
|
||
interface AutocompleteBaseOption { | ||
freeSolo?: boolean; | ||
name: string; | ||
defaultQuantity: number; | ||
} | ||
|
||
export interface AutocompleteExistingOption extends AutocompleteBaseOption { | ||
freeSolo?: false; | ||
id: number; | ||
} | ||
|
||
export interface AutocompleteFreeSoloOption extends AutocompleteBaseOption { | ||
freeSolo: true; | ||
editing: boolean; | ||
caloriesCost: number; | ||
category: SelectOption | null; | ||
inputValue?: string; | ||
} | ||
|
||
export type AutocompleteOptionType = AutocompleteExistingOption | AutocompleteFreeSoloOption; |
Oops, something went wrong.