Skip to content

Commit

Permalink
Fix display order calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Sep 12, 2024
1 parent a27628b commit 1e2740f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
28 changes: 10 additions & 18 deletions src/frontend/src/entities/note/lib/useNextDisplayOrder.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { type NoteItem, noteApi } from '../api';
import { useMemo } from 'react';
import { type NoteItem } from '../api';

const getMaxDisplayOrder = (notes: NoteItem[]): number =>
notes.reduce(
(maxOrder, note) => (note.displayOrder > maxOrder ? note.displayOrder : maxOrder),
-1,
export const useNextDisplayOrder = (notes: NoteItem[]): number =>
useMemo(
() =>
notes.reduce(
(maxOrder, note) => (note.displayOrder > maxOrder ? note.displayOrder : maxOrder),
-1,
) + 1,
[notes],
);

export const useNextDisplayOrder = (date: string): number => {
const { nextDisplayOrder } = noteApi.useNotesQuery(
{ date },
{
selectFromResult: ({ data, isSuccess }) => ({
nextDisplayOrder: isSuccess ? getMaxDisplayOrder(data.notes) + 1 : 0,
}),
},
);

return nextDisplayOrder;
};
4 changes: 2 additions & 2 deletions src/frontend/src/features/note/addEdit/ui/AddNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import { NoteInputFlow } from './NoteInputFlow';
interface Props {
date: string;
mealType: noteModel.MealType;
displayOrder: number;
}

export const AddNote: FC<Props> = ({ date, mealType }) => {
export const AddNote: FC<Props> = ({ date, mealType, displayOrder }) => {
const [addNote, { reset, ...addNoteResponse }] = noteApi.useCreateNoteMutation();

const addProductIfNotExists = useAddProductIfNotExists();
const [recognizeNotes, recognizeNotesResult] = useRecognizeNotes();

const notes = noteLib.useNotes(date);
const displayOrder = noteLib.useNextDisplayOrder(date);

const { clearValues: clearNoteForm, ...noteForm } = noteLib.useFormValues({
date,
Expand Down
26 changes: 15 additions & 11 deletions src/frontend/src/widgets/MealsList/ui/NotesList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { List, ListItem } from '@mui/material';
import { type FC } from 'react';
import { type NoteItem, type noteModel } from '@/entities/note';
import { noteLib, type NoteItem, type noteModel } from '@/entities/note';
import { AddNote } from '@/features/note/addEdit';
import { NotesListItem } from './NotesListItem';

Expand All @@ -10,13 +10,17 @@ interface Props {
notes: NoteItem[];
}

export const NotesList: FC<Props> = ({ date, mealType, notes }) => (
<List disablePadding>
{notes.map(note => (
<NotesListItem key={note.id} note={note} />
))}
<ListItem disableGutters>
<AddNote date={date} mealType={mealType} />
</ListItem>
</List>
);
export const NotesList: FC<Props> = ({ date, mealType, notes }) => {
const displayOrder = noteLib.useNextDisplayOrder(notes);

return (
<List disablePadding>
{notes.map(note => (
<NotesListItem key={note.id} note={note} />
))}
<ListItem disableGutters>
<AddNote date={date} mealType={mealType} displayOrder={displayOrder} />
</ListItem>
</List>
);
};

0 comments on commit 1e2740f

Please sign in to comment.