diff --git a/src/model/user.store.ts b/src/model/user.store.ts index e6f088e..7298080 100644 --- a/src/model/user.store.ts +++ b/src/model/user.store.ts @@ -1,13 +1,16 @@ import { writable } from 'svelte/store'; +import { get } from '../utils/rest.utils'; import type { User, UserStoreData } from './user.model'; -const mockUser: User = { - id: '1', - name: '1', -}; - export const userStore = writable(null); export const loadUserByApartmentNumber = (apartmentNumber: number) => { - userStore.set(mockUser); + get<{ data: User[] }>('/users') + .then((response) => response.data) + .then((users) => { + const user = users.find( + (user) => user.name === apartmentNumber.toString() + ); + userStore.set(user ?? null); + }); }; diff --git a/src/utils/rest.utils.ts b/src/utils/rest.utils.ts index f99fb27..a077a0d 100644 --- a/src/utils/rest.utils.ts +++ b/src/utils/rest.utils.ts @@ -1,15 +1,15 @@ import { BASE_URL } from '../consts'; -export const get = (endpoint: string) => { - fetch(`${BASE_URL}${endpoint}`, { +export const get = async (endpoint: string) => { + return fetch(`${BASE_URL}${endpoint}`, { method: 'GET', - }).then((response: Response) => response.json()); + }).then((response: Response) => response.json() as T); }; -export const post = (endpoint: string, body: T): Promise => { +export const post = async (endpoint: string, body: T): Promise => { const json = JSON.stringify(body); return fetch(`${BASE_URL}${endpoint}`, { method: 'POST', body: json, - }).then((response: Response) => response.json()); + }).then((response: Response) => response.json() as T); };