Refactor: Home 페이지(로그인 하는곳) 코드 리팩터

This commit is contained in:
정현조 2024-09-11 13:41:35 +09:00
parent 5de7b9de3f
commit 5f982a70bf
2 changed files with 41 additions and 79 deletions

View File

@ -1,35 +0,0 @@
import '@/index.css';
import { Meta, StoryObj } from '@storybook/react';
import Home from '.';
import { useState } from 'react';
const HomeWrapper = ({ initialLoggedIn }: { initialLoggedIn: boolean }) => {
const [isLoggedIn, setIsLoggedIn] = useState(initialLoggedIn);
return (
<Home
isLoggedIn={isLoggedIn}
setIsLoggedIn={setIsLoggedIn}
/>
);
};
const meta: Meta<typeof Home> = {
title: 'Components/Home',
component: Home,
parameters: {
layout: 'fullscreen',
},
};
export default meta;
type Story = StoryObj<typeof Home>;
export const GoogleLogin: Story = {
render: () => <HomeWrapper initialLoggedIn={false} />,
};
export const SelectWorkspace: Story = {
render: () => <HomeWrapper initialLoggedIn={true} />,
};

View File

@ -1,38 +1,45 @@
import { useState } from 'react';
import { useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue, SelectGroup } from '../ui/select';
import GoogleLogo from '@/assets/icons/web_neutral_rd_ctn@1x.png';
import useAuthStore from '@/stores/useAuthStore';
import { Button } from '@/components/ui/button';
import { fetchProfileApi } from '@/api/authApi';
interface HomeProps {
isLoggedIn?: boolean;
setIsLoggedIn?: (loggedIn: boolean) => void;
}
const DOMAIN = 'https://j11s002.p.ssafy.io';
export default function Home({ isLoggedIn = false, setIsLoggedIn }: HomeProps) {
const [, setSelectedWorkspace] = useState('');
const [loggedIn, setLoggedIn] = useState(isLoggedIn);
export default function Home() {
const navigate = useNavigate();
const { isLoggedIn, setLoggedIn, profile, setProfile } = useAuthStore();
const hasFetchedProfile = useRef(false);
const workspaces = [
{ id: 1, name: 'Workspace 1' },
{ id: 2, name: 'Workspace 2' },
{ id: 3, name: 'Workspace 3' },
];
if (!isLoggedIn && !profile.id && !hasFetchedProfile.current) {
const accessToken = localStorage.getItem('accessToken');
if (accessToken) {
setLoggedIn(true, accessToken);
fetchProfileApi()
.then((data) => {
if (data?.isSuccess && data.data) {
setProfile({
id: data.data.id,
nickname: data.data.nickname,
profileImage: data.data.profileImage,
});
hasFetchedProfile.current = true;
}
})
.catch((error) => {
alert('프로필을 가져오는 중 오류가 발생했습니다. 다시 시도해주세요.');
console.error('프로필 가져오기 실패:', error);
});
}
}
const handleGoogleSignIn = () => {
console.log('구글로 계속하기');
setLoggedIn(true);
if (setIsLoggedIn) {
setIsLoggedIn(true);
}
window.location.href = `${DOMAIN}/api/login/oauth2/authorization/google`;
};
const handleWorkspaceSelect = (value: string) => {
const selected = workspaces.find((workspace) => workspace.name === value);
if (selected) {
navigate(`/browse/${selected.id}`);
}
setSelectedWorkspace(value);
const handleStart = () => {
navigate('/browse');
};
return (
@ -41,7 +48,7 @@ export default function Home({ isLoggedIn = false, setIsLoggedIn }: HomeProps) {
<h2 className="mb-4 text-2xl font-bold text-gray-900"> </h2>
<p className="mb-4 text-base text-gray-700">
(AI)
. .
.
</p>
<p className="mb-4 text-base text-gray-700">
.
@ -56,7 +63,7 @@ export default function Home({ isLoggedIn = false, setIsLoggedIn }: HomeProps) {
</p>
</div>
{!loggedIn ? (
{!isLoggedIn ? (
<button
onClick={handleGoogleSignIn}
className="mb-4 transition hover:opacity-90 focus:outline-none focus:ring-2 focus:ring-gray-300 active:opacity-80"
@ -68,23 +75,13 @@ export default function Home({ isLoggedIn = false, setIsLoggedIn }: HomeProps) {
/>
</button>
) : (
<Select onValueChange={handleWorkspaceSelect}>
<SelectTrigger className="mb-4 w-72">
<SelectValue placeholder="워크스페이스를 선택하세요" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{workspaces.map((workspace) => (
<SelectItem
key={workspace.id}
value={workspace.name}
>
{workspace.name}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<Button
variant="outlinePrimary"
size="lg"
onClick={handleStart}
>
</Button>
)}
</div>
);