Назад к задачам
СБЕРПолучайте помощь с лайвкодингом в реальном времени с Sobes Copilot
Junior — Middle+
39
Task manager implementation
Компании, где спрашивали:
Условие задачи
Create a to‑do list application that utilizes the methods defined below. The UI component should render inside the <main> element. The helper functions sleep, getId and the interface TodoItem are provided, along with an in‑memory store. Implement the asynchronous functions getItems, createItem, and deleteItem which simulate latency with sleep and operate on the store. The functions must return clones of items, add new items with unique ids and timestamps, and remove items by id, throwing an error when the id does not exist.
const App = () => {
return <main></main>;
};
export default App;
export function sleep(ms: number) {
return new Promise((res) => setTimeout(res, ms));
}
let count = 0;
function getId() {
return count++;
}
export interface TodoItem {
id: number;
title: string;
complete: boolean;
createAt: number;
}
const store: TodoItem[] = [
{
id: 666,
title: "default",
complete: false,
createAt: 123
}
];
export async function getItems() {
await sleep(1_000);
// clone items so we can safely mutate them in store
return store.map((item) => ({ ...item }));
}
export async function createItem(title: string) {
await sleep(1_000);
const item = {
id: getId(),
title,
complete: false,
createAt: Date.now()
};
store.push(item);
return item;
}
export async function deleteItem(id: number) {
await sleep(1_000);
const storedItemIndex = store.findIndex((item) => item.id === id);
if (storedItemIndex === -1) {
throw new Error("item not found");
}
store.splice(storedItemIndex, 1);
}