Skip to content

Commit

Permalink
Merge pull request #264 from kakao-tech-campus-2nd-step3/Week11
Browse files Browse the repository at this point in the history
Week11 7
  • Loading branch information
ppochaco authored Nov 14, 2024
2 parents 91a73e1 + 9820fca commit c3b7d89
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/components/PageLayout/SideNavigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { Link } from 'react-router-dom'
import { Box, Flex } from '@chakra-ui/react'

import { useAuthTokenStore } from '@/stores/auth-token'
import { useInviteUrl } from '@/stores/invite-url'
import { useUserInfoStore } from '@/stores/user-info'

export const SideNavigation = () => {
const clearAuthToken = useAuthTokenStore((state) => state.clearAuthToken)
const isLoggedIn = useAuthTokenStore((state) => state.isLoggedIn())
const clearInviteUrl = useInviteUrl((state) => state.clearInviteUrl)

const clearUserInfo = useUserInfoStore((state) => state.clearUserInfo)
const userId = useUserInfoStore((state) => state.userInfo?.userId)
Expand Down Expand Up @@ -51,6 +53,7 @@ export const SideNavigation = () => {
onClick={() => {
clearAuthToken()
clearUserInfo()
clearInviteUrl()
}}
>
<BiLogOut size={26} />
Expand Down
10 changes: 10 additions & 0 deletions src/pages/InvitePage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useEffect } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import { useParams, useSearchParams } from 'react-router-dom'

import { Flex, Heading } from '@chakra-ui/react'

import ErrorPage from '@/pages/ErrorPage'
import { useInviteUrl } from '@/stores/invite-url'

import { InviteCard } from './InviteCard'
import { InviteErrorFallback } from './InviteErrorFallback'
Expand All @@ -12,7 +14,15 @@ export default function InvitePage() {
const { groupId } = useParams<{ groupId: string }>()
const [searchParams] = useSearchParams()

const setInviteUrl = useInviteUrl((state) => state.setInviteUrl)
const inviteCode = searchParams.get('invite-code')

useEffect(() => {
if (inviteCode) {
setInviteUrl(window.location.href)
}
}, [inviteCode, setInviteUrl])

if (!inviteCode) return <ErrorPage />

return (
Expand Down
6 changes: 5 additions & 1 deletion src/pages/RegisterPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import {
} from '@/components/Form'
import { RegisterUserFields, RegisterUserSchema } from '@/schema/user'
import { useAuthTokenStore } from '@/stores/auth-token'
import { useInviteUrl } from '@/stores/invite-url'
import { useUserInfoStore } from '@/stores/user-info'

export default function RegisterPage() {
const navigate = useNavigate()
const role = useUserInfoStore((state) => state.userInfo?.role)
const inviteUrl = useInviteUrl((state) => state.inviteUrl)
const clearInviteUrl = useInviteUrl((state) => state.clearInviteUrl)

const form = useForm<RegisterUserFields>({
resolver: zodResolver(RegisterUserSchema),
Expand All @@ -51,7 +54,8 @@ export default function RegisterPage() {
onSuccess: (data) => {
setAuthToken(data.accessToken)
setUserInfo(data.userInfo)
navigate('/')
window.location.href = `${inviteUrl}`
clearInviteUrl()
},
})

Expand Down
25 changes: 25 additions & 0 deletions src/stores/invite-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'

interface InviteUrlProps {
inviteUrl: string
setInviteUrl: (inviteUrl: string) => void
clearInviteUrl: () => void
}

export const useInviteUrl = create(
persist<InviteUrlProps>(
(set) => ({
inviteUrl: '/',
setInviteUrl: (inviteUrl) => {
set({ inviteUrl })
},
clearInviteUrl: () => {
set({ inviteUrl: '/' })
},
}),
{
name: 'invite-url',
}
)
)

0 comments on commit c3b7d89

Please sign in to comment.