diff --git a/front/src/api/user/index.ts b/front/src/api/user/index.ts index 9f489b4..322c690 100644 --- a/front/src/api/user/index.ts +++ b/front/src/api/user/index.ts @@ -1,19 +1,11 @@ import { API_URL } from '../../config' import { UserResponse, User } from './types' -import { faker } from '@faker-js/faker/locale/ru' - - -const initialUser: User = import.meta.env.DEV ? { // Temporary, until api is realized - id: Math.random() * 100, - name: faker.person.firstName() + ' ' + faker.person.lastName(), - regDate: faker.date.anytime().getTime(), - points: Math.round(Math.random() * 1000), -} : { - id: 1, - name: 'Вася пупкин', - regDate: 0, - points: 100, +const initialUser: User = { + id: -1, + nickname: '', + regDate: '', + points: -1, } const composeUserURL = () => ( @@ -21,7 +13,10 @@ const composeUserURL = () => ( ) const processUser = (data: UserResponse): User => { - return data + return { + ...data, + regDate: data.reg_date, + } } export { initialUser, composeUserURL, processUser } diff --git a/front/src/api/user/types.ts b/front/src/api/user/types.ts index ba96548..bbbd4d2 100644 --- a/front/src/api/user/types.ts +++ b/front/src/api/user/types.ts @@ -2,30 +2,22 @@ import { isObject } from '../../utils/types' type User = { id: number, - name: string, - regDate: number, + nickname: string, + regDate: string, points: number, } -const isUser = (obj: unknown): obj is User => ( +type UserResponse = Omit & { reg_date: string } + +const isUserResponse = (obj: unknown): obj is UserResponse => ( isObject(obj, { 'id': 'number', - 'name': 'string', - 'regDate': 'number', + 'nickname': 'string', + 'reg_date': 'string', 'points': 'number', }) ) -type UserResponse = User - -// const isUserResponse = (obj: unknown): obj is UserResponse => ( -// isObject(obj, { - -// }) -// ) - -const isUserResponse = isUser - export type { UserResponse, User } -export { isUserResponse, isUser } +export { isUserResponse } diff --git a/front/src/hooks/api/useUser.ts b/front/src/hooks/api/useUser.ts index 758ba8f..ce55ab0 100644 --- a/front/src/hooks/api/useUser.ts +++ b/front/src/hooks/api/useUser.ts @@ -1,23 +1,16 @@ -import { initialUser } from '../../api/user' -import { User } from '../../api/user/types' -import { UseFetchReturn } from '../useFetch' +import { composeUserURL, initialUser, processUser } from '../../api/user' +import { User, isUserResponse } from '../../api/user/types' +import useFetch, { UseFetchReturn } from '../useFetch' const useUser = (): UseFetchReturn => ( - // useFetch( - // composeUserURL(), - // 'GET', - // true, - // isUserResponse, - // processUser, - // initialUser - // ) - - { - data: initialUser, - loading: false, - error: null, - refetch: () => { return }, - } + useFetch( + composeUserURL(), + 'GET', + true, + isUserResponse, + processUser, + initialUser + ) ) export default useUser diff --git a/front/src/pages/UserPage.tsx b/front/src/pages/UserPage.tsx index f02bcc1..cf15d5a 100644 --- a/front/src/pages/UserPage.tsx +++ b/front/src/pages/UserPage.tsx @@ -5,17 +5,19 @@ import { userCategories } from '../assets/userCategories' import { BackHeader, CategoryPreview, Poetry, Points, SignOut } from '../components' import { gotError, gotResponse } from '../hooks/useFetch' +import styles from '../styles/UserPage.module.css' + function UserPage() { const user = useUser() return ( - +