Skip to content

Commit

Permalink
feat: useToast 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
uiop5809 committed Sep 4, 2024
1 parent 5f1d984 commit 201236a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/app/board/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const BoardDetail = () => {
}
postBoardLike(boardId, {
onSuccess: () => {
showToast(isLiked ? '공감을 취소하였습니다' : '공감을 눌렀습니다')
setIsLiked(!isLiked)
setLikeCount((prevCount) => (isLiked ? prevCount - 1 : prevCount + 1))
},
Expand All @@ -71,6 +72,7 @@ const BoardDetail = () => {
deleteBoard(boardId, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.boardList })
showToast('게시글이 삭제되었습니다')
router.back()
},
})
Expand Down Expand Up @@ -135,11 +137,7 @@ const BoardDetail = () => {
<Image
src={`/images/board/${isLiked ? 'like_fill' : 'like_empty'}.svg`}
alt="like_btn"
className={`object-cover ${
userInfo && userInfo.id === boardDetail.memberSimpleInfo.id
? 'cursor-default'
: 'cursor-pointer'
}`}
className="object-cover cursor-pointer"
fill
onClick={handleLikeToggle}
/>
Expand Down
7 changes: 5 additions & 2 deletions src/app/user/[id]/update/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Button from '@/components/common/Button'
import UserUpdateProfile from '@/components/user/UserProfileUpdate'
import { useState, useEffect } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import { useToast } from '@/hooks/useToast'

const UserUpdatePage = () => {
const { id } = useParams()
Expand All @@ -19,10 +20,11 @@ const UserUpdatePage = () => {
const queryClient = useQueryClient()

const { data: userInfo } = useUserInfo()

const { data: profile } = useProfile(profileId)
const { mutate: patchProfile } = usePatchProfile()

const { showToast } = useToast()

const [updatedProfile, setUpdatedProfile] = useState({})
const [badgeId, setBadgeId] = useState<number | null>(null)

Expand Down Expand Up @@ -52,7 +54,8 @@ const UserUpdatePage = () => {
const handleSubmit = () => {
patchProfile(updatedProfile, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['profile', profileId] }) // queryKey를 객체로 전달
queryClient.invalidateQueries({ queryKey: ['profile', profileId] })
showToast('프로필 수정을 완료하였습니다')
router.back()
},
})
Expand Down
13 changes: 12 additions & 1 deletion src/components/board/MbtiCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
usePostCategoryBookmark,
} from '@/service/board/useBoardService'
import { useState, useEffect } from 'react'
import { useToast } from '@/hooks/useToast'

export interface MbtiCategoriesProps {
selectedMbti: string
Expand All @@ -23,6 +24,8 @@ const MbtiCategories = ({ selectedMbti }: MbtiCategoriesProps) => {
const { mutate: postCategoryBookmark } = usePostCategoryBookmark()
const [favorites, setFavorites] = useState<Record<string, boolean>>({})

const { showToast } = useToast()

useEffect(() => {
if (categoryBookmark) {
const favoriteMbti = categoryBookmark.reduce(
Expand All @@ -47,7 +50,15 @@ const MbtiCategories = ({ selectedMbti }: MbtiCategoriesProps) => {
...prevFavorites,
[mbti]: !prevFavorites[mbti],
}))
postCategoryBookmark(mbti)
postCategoryBookmark(mbti, {
onSuccess: () => {
if (favorites[mbti]) {
showToast('즐겨찾기에서 제거하였습니다.')
return
}
showToast('즐겨찾기에 추가하였습니다.')
},
})
}

return (
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useEffect, useState } from 'react'

const TOAST_REMOVE_DELAY = 2000
const TOAST_REMOVE_DELAY = 1000

interface Toast {
id: string
Expand Down

0 comments on commit 201236a

Please sign in to comment.