@@ -2,9 +2,10 @@ import { Link, useNavigate } from 'react-router-dom';
22
33import { CustomError } from '@/api/axios.ts' ;
44import { deleteReservation , getReservation } from '@/api/reservation.ts' ;
5- import { postLogout } from '@/api/user.ts' ;
5+ import { getGuestLogin , postLogout } from '@/api/user.ts' ;
66
77import { useAuthContext } from '@/hooks/useAuthContext.tsx' ;
8+ import useConfirm from '@/hooks/useConfirm.tsx' ;
89
910import ReservationCard from '@/components/Navbar/ReservationCard.tsx' ;
1011import { toast } from '@/components/Toast/index.ts' ;
@@ -14,15 +15,25 @@ import Popover from '@/components/common/Popover';
1415import Separator from '@/components/common/Separator.tsx' ;
1516
1617import type { Reservation } from '@/type/reservation.ts' ;
17- import { useMutation , useMutationState , useQuery , useQueryClient } from '@tanstack/react-query' ;
18+ import type { Guest } from '@/type/user.ts' ;
19+ import {
20+ useIsFetching ,
21+ useMutation ,
22+ useMutationState ,
23+ useQuery ,
24+ useQueryClient ,
25+ } from '@tanstack/react-query' ;
1826import { cx } from 'class-variance-authority' ;
1927
2028const POPOVER_WIDTH = 460 ;
2129
2230const RESERVATION_DELETE_MUTATION_KEY = [ 'reservation' ] ;
31+ const GUEST_LOGIN_QUERY_KEY = [ 'guest' ] ;
2332
2433export default function Navbar ( ) {
25- const { isLogin, userId, logout } = useAuthContext ( ) ;
34+ const { isLogin, userId, logout, login } = useAuthContext ( ) ;
35+ const isGuestLoginPending = ! ! useIsFetching ( { queryKey : GUEST_LOGIN_QUERY_KEY } ) ;
36+ const { confirm } = useConfirm ( ) ;
2637 const navigate = useNavigate ( ) ;
2738 const queryClient = useQueryClient ( ) ;
2839 const { data : reservations } = useQuery < Reservation [ ] , CustomError > ( {
@@ -31,6 +42,7 @@ export default function Navbar() {
3142 enabled : isLogin ,
3243 staleTime : Infinity ,
3344 } ) ;
45+ const sliced = userId ?. slice ( 0 , 12 ) ;
3446
3547 const { mutate : requestDeleteReservation } = useMutation ( {
3648 mutationKey : RESERVATION_DELETE_MUTATION_KEY ,
@@ -44,6 +56,40 @@ export default function Navbar() {
4456 } ,
4557 } ) ;
4658
59+ const loginAsGuest = async ( ) => {
60+ const isConfirm = await confirm ( {
61+ title : '게스트로 입장하기' ,
62+ description : '게스트 계정은 로그아웃하시면 다시 사용 할 수 없습니다.\n 그래도 입장하시겠습니까?' ,
63+ buttons : {
64+ ok : {
65+ title : '확인' ,
66+ color : 'success' ,
67+ } ,
68+ cancel : {
69+ title : '취소' ,
70+ } ,
71+ } ,
72+ } ) ;
73+ if ( isConfirm ) {
74+ await queryClient
75+ . fetchQuery < Guest > ( {
76+ queryKey : GUEST_LOGIN_QUERY_KEY ,
77+ queryFn : getGuestLogin ,
78+ } )
79+ . then ( ( data ) => {
80+ const sliced = data . loginId . slice ( 0 , 12 ) ;
81+ if ( login ) {
82+ login ( sliced ) ;
83+ toast . success ( 'geust로 로그인 되었습니다' ) ;
84+ }
85+ } )
86+ . catch ( ( ) => {
87+ toast . error ( '로그인에 실패했습니다\n잠시 후 다시 시도해주세요.' ) ;
88+ } ) ;
89+ return ;
90+ }
91+ } ;
92+
4793 const deletingReservationIdList = useMutationState ( {
4894 filters : { mutationKey : RESERVATION_DELETE_MUTATION_KEY , status : 'pending' } ,
4995 select : ( mutation ) => mutation . state . variables ,
@@ -78,7 +124,7 @@ export default function Navbar() {
78124 render = { ( togglePopover , triggerRef ) => (
79125 < Button size = "middle" intent = { 'ghost' } onClick = { togglePopover } ref = { triggerRef } >
80126 < Icon iconName = "User" />
81- < span className = "text-label2 text-typo" > { userId } </ span >
127+ < span className = "text-label2 text-typo" > { ` ${ sliced } 님` } </ span >
82128 < Icon iconName = "DownArrow" />
83129 </ Button >
84130 ) }
@@ -119,6 +165,21 @@ export default function Navbar() {
119165 </ Popover . Root >
120166 ) : (
121167 < nav className = "flex gap-4" >
168+ < Button
169+ size = { 'middle' }
170+ color = { 'primary' }
171+ intent = { 'outline' }
172+ onClick = { loginAsGuest }
173+ disabled = { isGuestLoginPending } >
174+ { isGuestLoginPending ? (
175+ < >
176+ < Icon iconName = "Loading" className = "animate-spin" />
177+ < span className = "f text-label2 text-typo-disable" > 로그인중..</ span >
178+ </ >
179+ ) : (
180+ < span className = "text-label2 text-primary" > 게스트로 입장하기</ span >
181+ ) }
182+ </ Button >
122183 < Button intent = { 'outline' } color = { 'primary' } size = { 'middle' } asChild >
123184 < Link to = { '/signUp' } >
124185 < span className = "text-label2 text-primary" > 회원가입</ span >
0 commit comments