Skip to content

Commit

Permalink
feat: 첫 로딩 시 유저 정보 fetch 전 로딩 페이지 보여주기 (#28)
Browse files Browse the repository at this point in the history
* feat: 내 정보 조회 전까지 로딩 페이지

* feat: 로그아웃 시 toast
  • Loading branch information
son-daehyeon authored Sep 7, 2024
1 parent 6678a25 commit c283f5b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/api/WinkApiApplication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

import React, { useEffect } from 'react';

import { Loading } from '@/components';

import { WinkApi } from '@/api';

import { useApplicationState } from '@/store';

interface WinkApiApplicationProps {
children: React.ReactNode;
}

export const WinkApiApplication: React.FC<WinkApiApplicationProps> = ({
children,
}: WinkApiApplicationProps) => {
const { loaded } = useApplicationState();

useEffect(() => {
WinkApi.init();
}, []);

if (!loaded) {
return <Loading />;
}

return <>{children}</>;
};
8 changes: 7 additions & 1 deletion src/api/request/WinkApiRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toast } from 'react-toastify';

import { RefreshResponseDto, User } from '@/api';

import { useUserStore } from '@/store';
import { useUserStore, useApplicationState } from '@/store';

interface WinkRawApiResponse<T> {
code: number;
Expand All @@ -29,6 +29,8 @@ export class WinkApiRequest {

if (accessToken && refreshToken) {
this.setToken(accessToken, refreshToken);
} else {
useApplicationState.setState({ loaded: true });
}
}

Expand Down Expand Up @@ -141,6 +143,10 @@ export class WinkApiRequest {

this.get('/auth/me').then((user) => {
useUserStore.setState({ user: user as User });

if (!useApplicationState.getState().loaded) {
useApplicationState.setState({ loaded: true });
}
});
}
}
5 changes: 4 additions & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use client';

import React, { useEffect, useState } from 'react';
import { toast } from 'react-toastify';

import Image from 'next/image';
import Link from 'next/link';
import { usePathname } from 'next/navigation';

import { WinkApi } from '@/api';

import { useUserStore } from '@/store/useUserStore';
import { useUserStore } from '@/store';

import logo from '@/public/logo.png';

Expand Down Expand Up @@ -92,6 +93,8 @@ export const Header: React.FC = () => {
e.preventDefault();

WinkApi.Request.removeToken();

toast.info('로그아웃 되었습니다.');
};

const toggleDropdown = (type: string) => {
Expand Down
9 changes: 9 additions & 0 deletions src/components/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

export const Loading: React.FC = () => {
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="w-16 h-16 border-t-4 border-blue-500 border-solid rounded-full animate-spin"></div>
</div>
);
};
2 changes: 2 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './Loading';

export * from './Header';
export * from './Footer';

Expand Down
1 change: 1 addition & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './useUserStore';
export * from './useApplicationState';
11 changes: 11 additions & 0 deletions src/store/useApplicationState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { create } from 'zustand';

interface ApplicationState {
loaded: boolean;
setLoaded: (loaded: boolean) => void;
}

export const useApplicationState = create<ApplicationState>((set) => ({
loaded: false,
setLoaded: (loaded) => set({ loaded }),
}));

0 comments on commit c283f5b

Please sign in to comment.