fix: 코드 재검토 반영 — 댓글 더 보기, 긴 비밀번호 500 방지, 토큰 갱신 실패 처리, 연타 막기
This commit is contained in:
1 parent
ae6b06ac1f
commit
be8f827556
7 files changed
+88
-30
No files matched your search
@@ -229,7 +229,7 @@ function FullCard({ post }: PostProps) {
|
||||
// 화면을 먼저 바꾸고, 실패하면 되돌린다 (서버는 실패도 200 + result.success=false 로 준다)
|
||||
const setLike = (on: boolean) => {
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
if (on === liked) return
|
||||
if (on === liked || like.isPending || unlike.isPending) return
|
||||
if (on) setPop((p) => p + 1)
|
||||
patch({ liked: on, like_count: likeCount + (on ? 1 : -1) })
|
||||
;(on ? like : unlike).mutate(
|
||||
@@ -246,6 +246,7 @@ function FullCard({ post }: PostProps) {
|
||||
|
||||
const toggleSave = () => {
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
if (save.isPending || unsave.isPending) return
|
||||
const on = !saved
|
||||
patch({ saved: on })
|
||||
;(on ? save : unsave).mutate(
|
||||
|
||||
+23
-10
@@ -1,4 +1,4 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState, type FormEvent, type ReactNode } from 'react'
|
||||
import { setTokens, subscribeAuth } from '../api/auth'
|
||||
import {
|
||||
@@ -9,8 +9,8 @@ import {
|
||||
getIndexOptions,
|
||||
getSeasonWeekOptions,
|
||||
likeCommentMutation,
|
||||
listCommentsQueryKey,
|
||||
listCommentsOptions,
|
||||
listCommentsInfiniteOptions,
|
||||
listCommentsInfiniteQueryKey,
|
||||
listRegionsOptions,
|
||||
loginMutation,
|
||||
meQueryKey,
|
||||
@@ -426,6 +426,7 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
|
||||
|
||||
const toggle = () => {
|
||||
if (!loggedIn) return open({ type: 'login' })
|
||||
if (like.isPending || unlike.isPending) return
|
||||
const next = !liked
|
||||
setLiked(next)
|
||||
setCount((n) => n + (next ? 1 : -1))
|
||||
@@ -456,7 +457,7 @@ function CommentRow({ c, postId }: { c: CommentData; postId: number }) {
|
||||
onSuccess: (res) => {
|
||||
setRemoveFailed(!res.result?.success)
|
||||
if (!res.result?.success) return
|
||||
queryClient.invalidateQueries({ queryKey: listCommentsQueryKey({ path: { post_id: postId } }) })
|
||||
queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) })
|
||||
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) })
|
||||
},
|
||||
onError: () => setRemoveFailed(true),
|
||||
@@ -496,12 +497,16 @@ function CommentsSheet({ postId }: { postId: number }) {
|
||||
const { open } = useSheet()
|
||||
const { loggedIn, me } = useAuth()
|
||||
const queryClient = useQueryClient()
|
||||
const list = useQuery(listCommentsOptions({ path: { post_id: postId } }))
|
||||
const list = useInfiniteQuery({
|
||||
...listCommentsInfiniteOptions({ path: { post_id: postId } }),
|
||||
initialPageParam: { path: { post_id: postId } },
|
||||
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
||||
})
|
||||
const add = useMutation(addCommentMutation())
|
||||
const [addFailed, setAddFailed] = useState(false)
|
||||
const [text, setText] = useState('')
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const comments = list.data?.comments ?? []
|
||||
const comments = list.data?.pages.flatMap((p) => p.comments ?? []) ?? []
|
||||
|
||||
const submit = (e: FormEvent) => {
|
||||
e.preventDefault()
|
||||
@@ -515,7 +520,7 @@ function CommentsSheet({ postId }: { postId: number }) {
|
||||
setAddFailed(!res.result?.success)
|
||||
if (!res.result?.success) return
|
||||
setText('')
|
||||
queryClient.invalidateQueries({ queryKey: listCommentsQueryKey({ path: { post_id: postId } }) })
|
||||
queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) })
|
||||
queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) })
|
||||
},
|
||||
onError: () => setAddFailed(true),
|
||||
@@ -527,7 +532,15 @@ function CommentsSheet({ postId }: { postId: number }) {
|
||||
<>
|
||||
<Body>
|
||||
{comments.length ? (
|
||||
comments.map((c) => <CommentRow key={c.comment_id} c={c} postId={postId} />)
|
||||
<>
|
||||
{/* 다시 받은 좋아요 상태(로그인 바뀜 등)가 행에 반영되도록 key 에 넣는다 */}
|
||||
{comments.map((c) => <CommentRow key={`${c.comment_id}:${c.liked}:${c.like_count}`} c={c} postId={postId} />)}
|
||||
{list.hasNextPage && (
|
||||
<button type="button" onClick={() => list.fetchNextPage()} disabled={list.isFetchingNextPage} className="my-2 w-full cursor-pointer text-center text-sm font-bold text-(--ink3) disabled:opacity-50">
|
||||
{list.isFetchingNextPage ? '불러오는 중…' : '댓글 더 보기'}
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
) : list.isSuccess ? (
|
||||
<p className="my-6 text-center font-semibold text-(--ink3)">
|
||||
아직 댓글이 없어요.
|
||||
@@ -571,7 +584,7 @@ function CommentsSheet({ postId }: { postId: number }) {
|
||||
const AUTH_ERRORS: Record<string, string> = {
|
||||
AUTH_FAILED: '아이디나 비밀번호가 맞지 않아요.',
|
||||
AUTH_DUPLICATE_ID: '이미 쓰고 있는 아이디예요.',
|
||||
INVALID_INPUT: '아이디는 영어 소문자·숫자·_ 4~20자, 비밀번호는 8자 이상, 닉네임은 2~12자로 써 주세요.',
|
||||
INVALID_INPUT: '아이디는 영어 소문자·숫자·_ 4~20자, 비밀번호는 8~64자, 닉네임은 2~12자로 써 주세요.',
|
||||
}
|
||||
|
||||
function LoginSheet({ onDone }: { onDone: () => void }) {
|
||||
@@ -622,7 +635,7 @@ function LoginSheet({ onDone }: { onDone: () => void }) {
|
||||
))}
|
||||
</div>
|
||||
<input value={loginId} onChange={(e) => setLoginId(e.target.value)} autoComplete="username" required minLength={4} maxLength={20} placeholder="아이디" aria-label="아이디" className={field} />
|
||||
<input value={password} onChange={(e) => setPassword(e.target.value)} type="password" autoComplete={mode === 'login' ? 'current-password' : 'new-password'} required minLength={mode === 'signup' ? 8 : undefined} placeholder={mode === 'signup' ? '비밀번호 (8자 이상)' : '비밀번호'} aria-label="비밀번호" className={field} />
|
||||
<input value={password} onChange={(e) => setPassword(e.target.value)} type="password" autoComplete={mode === 'login' ? 'current-password' : 'new-password'} required minLength={mode === 'signup' ? 8 : undefined} maxLength={64} placeholder={mode === 'signup' ? '비밀번호 (8자 이상)' : '비밀번호'} aria-label="비밀번호" className={field} />
|
||||
{mode === 'signup' && <input value={nickname} onChange={(e) => setNickname(e.target.value)} required minLength={2} maxLength={12} placeholder="닉네임 (2~12자)" aria-label="닉네임" className={field} />}
|
||||
<p className="min-h-5 text-center text-[13px] font-bold text-(--heart)" aria-live="polite">
|
||||
{error}
|
||||
|
||||
Reference in new issue
Block a user