Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#104] feat: add err handle or success for signup/question create/question detail and ssr fetch #108

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion packages/client/app/(withLayout)/question/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { fetchMemberInformation, ssrConfig } from '@shared/api'
import { QueryClient } from '@tanstack/react-query'
import { cookies } from 'next/headers'
import { ClientQuestionDetailPage } from 'src/clientPages/questionDetail'

export default function QuestionDetailPage() {
export default async function QuestionDetailPage(d) {
const queryClient = new QueryClient()
const config = ssrConfig()

try {
const user = await queryClient.fetchQuery({
queryKey: [`/members/information`],
queryFn: () => fetchMemberInformation(config),
})
} catch (e) {
console.log(e)
}
// TODO: give userData by props to ClientQuestionDetailPage
return <ClientQuestionDetailPage />
}
2 changes: 2 additions & 0 deletions packages/client/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './globals.css'
import localFont from 'next/font/local'
import './design-system-globals.css'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { ToastPrepare } from '@gds/component'
import ReactQueryClientProviders from '../src/apps/provider/reactQueryProviders'

export const metadata: Metadata = {
Expand Down Expand Up @@ -53,6 +54,7 @@ export default function RootLayout({
<body>
<div className="mobile-layout">
<ReactQueryClientProviders>
<ToastPrepare />
{children}

{process.env.NODE_ENV !== 'production' && (
Expand Down
10 changes: 10 additions & 0 deletions packages/client/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const withVanillaExtract = createVanillaExtractPlugin()
const API_BASE_URL = process.env.API_BASE_URL

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
trailingSlash: true,
images: {
remotePatterns: [
{
Expand All @@ -19,6 +21,14 @@ const nextConfig = {
},
],
},
async rewrites() {
return [
{
source: '/api/:path*/',
destination: `${API_BASE_URL}/:path*/`,
},
]
},
}

export default withVanillaExtract(nextConfig)
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.52.1",
"react-secure-storage": "^1.3.2",
"react-hot-toast": "^2.4.1",
"react-slick": "^0.30.2",
"slick-carousel": "^1.8.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { QuestionCreateInputs } from '@widgets/QuestionCreateInputs'
import { MainLoader } from '@shared/ui'
import { useState } from 'react'
import { useRouter } from 'next/navigation'
import { Toast } from '@gds/component'
import { usePostQuestion } from '../api/auth'

export function ClientQuestionCreatePage() {
Expand Down Expand Up @@ -37,6 +38,16 @@ export function ClientQuestionCreatePage() {
form.reset()
setImageUrls([])
router.push('/home')
Toast.success({
title: '질문이 등록되었습니다.',
})
},
onError: (e) => {
Toast.error({
title:
e.response?.data?.message ||
'서버 오류가 발생했습니다. 다시 시도해주세요.',
})
},
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { useFetchMemberInformation } from '@shared/api'
import { useParams, useRouter } from 'next/navigation'
import { MainLoader } from '@shared/ui'
import { useState } from 'react'
import { Toast } from '@gds/component'
import { pageWrapper } from './style.css'
import {
useFetchQuestionPost,
Expand All @@ -22,10 +22,21 @@ export function ClientQuestionDetailPage() {
const params = useParams()
const router = useRouter()

const { data: userData } = useFetchMemberInformation()
const { data: questionData } = useFetchQuestionPost({
questionPostId: Number(params.id),
})
const {
data: userData,
isError: userDataIsError,
error: userDataError,
} = useFetchMemberInformation()
const {
data: questionData,
isError: questionDataIsError,
error: questionDataError,
} = useFetchQuestionPost(
{
questionPostId: Number(params.id),
},
{ enabled: !!params.id },
)
const { mutate: createQuestionMeta } = usePostCreateQuestionMeta()
const { mutate: cancelQuestionMeta } = usePostCancelQuestionMeta()
const { mutate: createAnswer } = usePostAnswer()
Expand All @@ -36,6 +47,17 @@ export function ClientQuestionDetailPage() {

if (params.id === undefined) {
router.push('/home')
Toast.error({ title: '잘못된 접근 입니다.' })
} else if (userDataIsError) {
router.push('/home')
Toast.error({
title: userDataError?.message || '서버 오류가 발생했습니다.',
})
} else if (questionDataIsError) {
router.push('/home')
Toast.error({
title: questionDataError?.message || '서버 오류가 발생했습니다.',
})
}

return (
Expand Down Expand Up @@ -70,8 +92,9 @@ export function ClientQuestionDetailPage() {
registerAnswerRequest: { content: answer },
},
{
onSuccess: () => {
refetch()
onSuccess: async () => {
await refetch()
Toast.success({ title: '답변이 등록되었습니다.' })
},
},
)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/clientPages/signup/api/mail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMutation } from '@tanstack/react-query'

import { AuthCodeRequest, MailAPIApi, SendMailRequest } from '@server-api/api'
import { getTmpAuthorizedConfig, config } from '@shared/api/config'
import { config } from '@shared/api/config'

export const useSendVerificationCodeByEmail = () => {
return useMutation({
Expand Down
19 changes: 16 additions & 3 deletions packages/client/src/clientPages/signup/ui/ClientSignupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { SignupInputSection } from '@widgets/SignupInputs'
import { usePostMember } from '@shared/api'
import { useRouter } from 'next/navigation'
import { PageURL } from '@shared/model'
import { useState } from 'react'
import { color, Typo } from '@gds/token'
import * as styles from './style.css'

export function ClientSignupPage() {
Expand All @@ -22,6 +24,7 @@ export function ClientSignupPage() {
} = form.watch()
const { mutate: createMember, status } = usePostMember()
const router = useRouter()
const [submitError, setSubmitError] = useState<string | null>(null)

return (
<>
Expand All @@ -30,6 +33,14 @@ export function ClientSignupPage() {
<div className={styles.Wrapper}>
<SignupInputSection form={form} />
<div className={styles.FinalBtnBox}>
<div
className={Typo.body1.sb}
style={{
color: color.error,
}}
>
{submitError}
</div>
<Button
type="button"
size="medium"
Expand All @@ -51,11 +62,13 @@ export function ClientSignupPage() {
},
{
onSuccess: () => {
setSubmitError(null)
router.push(PageURL.SIGNUP_SUCCESS)
},
onError: () => {
alert(
'회원가입에 실패했습니다. 다시 시도해주세요.',
onError: (e) => {
setSubmitError(
e.response?.data?.message ||
'서버 오류가 발생했습니다. 다시 시도해주세요.',
)
},
},
Expand Down
6 changes: 6 additions & 0 deletions packages/client/src/clientPages/signup/ui/style.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ export const bottomSection = style({

export const FinalBtnBox = style({
marginTop: 'auto',
minHeight: 50,
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'center',
gap: 10,
})
68 changes: 68 additions & 0 deletions packages/client/src/design-system/component/Toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ReactNode } from 'react'
import toast, { Toaster } from 'react-hot-toast'

import { IconClose, IconCheckFilled } from '@gds/icon'
import { color, Typo } from '@gds/token'
import * as style from './toast.css'

export function ToastPrepare() {
return <Toaster />
}

export interface ToastData {
title?: ReactNode
description?: ReactNode
durationMs?: number
}

function renderToast(icon: ReactNode, message: ToastData) {
toast.custom(
(t) => (
<div className={style.toastBase}>
<div className={style.left}>
<div className={style.iconContainer}>{icon}</div>
<div className={style.titleAndMessageContainer}>
{message.title && (
<div className={style.titleContainer}>
<div className={Typo.body2.sb}>{message.title}</div>
</div>
)}
{message.description && (
<div className={style.messageContainer}>
<div className={Typo.body2.lg}>
{message.description}
</div>
</div>
)}
</div>
</div>
<div
className={style.closeContainer}
role="button"
aria-label="Close"
tabIndex={0}
onClick={() => {
toast.remove(t.id)
}}
onKeyDown={(e) => {
if (e.key === 'Escape') {
toast.remove(t.id)
}
}}
>
<IconClose />
</div>
</div>
),
{ position: 'top-right', duration: message.durationMs || 3000 },
)
}

export const Toast = {
success: (message: ToastData) => {
renderToast(<IconCheckFilled color={color['primary-main']} />, message)
},
error: (message: ToastData) => {
renderToast(<IconCheckFilled color={color.error} />, message)
},
}
50 changes: 50 additions & 0 deletions packages/client/src/design-system/component/Toast/toast.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { style } from '@vanilla-extract/css'
import { color } from '@gds/token'

export const toastBase = style({
display: 'flex',
width: '360px',
padding: '12px',
justifyContent: 'space-between',
backgroundColor: color.white,
borderRadius: '9px',
boxShadow: '0px 2px 4px rgba(0, 0, 0, 0.1), 0px 4px 8px rgba(0, 0, 0, 0.1)',
})

export const left = style({
display: 'flex',
height: '100%',
gap: '8px',
})

export const iconContainer = style({
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: '18px',
lineHeight: '18px',
})

export const closeContainer = style({
cursor: 'pointer',
height: '20px',
width: '20px',
})

export const titleAndMessageContainer = style({
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
gap: '8px',
})

export const titleContainer = style({
display: 'flex',
height: '17px',
lineHeight: '17px',
})

export const messageContainer = style({
display: 'flex',
height: '100%',
})
3 changes: 3 additions & 0 deletions packages/client/src/design-system/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ export type { DividerProps } from './Divider/Divider'

export { Spinner } from './Spinner/Spinner'
export type { SpinnerProps } from './Spinner/Spinner'

export { Toast, ToastPrepare } from './Toast/Toast'
export type { ToastData } from './Toast/Toast'
12 changes: 0 additions & 12 deletions packages/client/src/shared/api/axiosInstance.ts

This file was deleted.

Loading