Skip to content

Commit

Permalink
solution 0.01
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavKapytsia committed Dec 15, 2024
1 parent 56d0477 commit ac7b388
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export const App: React.FC = () => {
};

const updateTodos = async (updateTodo: TodoInterface[]) => {
const updatedTodos: TodoInterface[] = [];

for (const upTodo of updateTodo) {
try {
const updatedTodo = (await updTodo(upTodo)) as TodoInterface;
Expand All @@ -157,6 +159,8 @@ export const App: React.FC = () => {
return newTodosList;
});

updatedTodos.push(updatedTodo);

if (inputForFocusRef.current) {
inputForFocusRef.current.focus();
}
Expand All @@ -166,6 +170,8 @@ export const App: React.FC = () => {
}

setTodosForModify([]);

return updatedTodos;
};

const handleClose = () => {
Expand Down Expand Up @@ -225,7 +231,27 @@ export const App: React.FC = () => {

setTodosForModify(content);

await Promise.allSettled(content.map(todo => updateTodos([todo])));
await Promise.allSettled(content.map(todo => updateTodos([todo]))).then(
results => {
const successfulResults = results
.filter(result => result.status === 'fulfilled')
.flatMap(result => result.value);

setTodos(currnet => {
const newList = [...currnet];

successfulResults.forEach(updatedTodo => {
const index = newList.findIndex(todo => todo.id === updatedTodo.id);

if (index !== -1) {
newList[index] = updatedTodo;
}
});

return newList;
});
},
);

setTodosForModify([]);
};
Expand Down

0 comments on commit ac7b388

Please sign in to comment.