Skip to content

Commit

Permalink
fix: like and favorite function
Browse files Browse the repository at this point in the history
  • Loading branch information
HiHoi committed Feb 15, 2024
1 parent 4467a90 commit 2f4c394
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/app/showcase/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const Showcase = () => {
message={message}
addCard={addCard}
addDisabled={draggedCardList.length === 0}
mutate={setCardList}
/>
</Stack>
)
Expand All @@ -118,6 +119,7 @@ const Showcase = () => {
removeCard={removeCard}
message={message}
addCard={addCard}
mutate={setCardList}
/>
)
}
Expand Down
5 changes: 4 additions & 1 deletion src/app/showcase/panel/CardContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import React from 'react'
import React, { Dispatch, SetStateAction } from 'react'
import useMedia from '@/hook/useMedia'
import { IconButton, Stack, Typography } from '@mui/material'
import * as containerStyle from './CardContainer.style'
Expand All @@ -16,12 +16,14 @@ const CardContainer = ({
message,
addCard,
addDisabled,
mutate,
}: {
cardList: Array<ICardData>
removeCard: (recruit_id: number) => void
message: string
addCard?: () => void
addDisabled?: boolean
mutate: Dispatch<SetStateAction<ICardData[]>>
}) => {
const { isPc, isTablet } = useMedia()
return (
Expand Down Expand Up @@ -62,6 +64,7 @@ const CardContainer = ({
cardList={cardList}
removeCard={removeCard}
addCard={addCard}
mutate={mutate}
/>
) : (
<Typography variant="CaptionEmphasis">{message}</Typography>
Expand Down
5 changes: 4 additions & 1 deletion src/app/showcase/panel/CardStack.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { Box } from '@mui/material'
import React, { useState } from 'react'
import React, { Dispatch, SetStateAction, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import * as style from './ShowcaseCard.style'
import { ICardData } from '@/app/showcase/panel/types'
Expand All @@ -18,10 +18,12 @@ const CardStack = ({
cardList,
removeCard,
addCard,
mutate,
}: {
cardList: Array<ICardData>
removeCard: (recruit_id: number) => void
addCard?: () => void
mutate: Dispatch<SetStateAction<ICardData[]>>
}) => {
const [dragged, setDragged] = useState(false)

Expand Down Expand Up @@ -119,6 +121,7 @@ const CardStack = ({
data={card}
dragged={dragged}
setDragged={setDragged}
mutate={mutate}
/>
</motion.div>
)
Expand Down
59 changes: 48 additions & 11 deletions src/app/showcase/panel/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ function PostCard({
liked,
isFavorite,
onClick,
mutate,
}: IPostCardShowcase) {
const ref = React.useRef<HTMLDivElement>(null)
const [currentCardWidth, setCurrentCardWidth] = useState<number>(0)
const [favorite, setFavorite] = useState<boolean>(isFavorite)
const [likeNum, setLikeNum] = useState<number>(like)
const [isLiked, setIsLiked] = useState<boolean>(liked)
const [favorite, setFavorite] = useState<boolean>(false)
const [likeNum, setLikeNum] = useState<number>(0)
const [isLiked, setIsLiked] = useState<boolean>(false)
const axiosWithAuth = useAxiosWithAuth()

useEffect(() => {
setFavorite(isFavorite)
setLikeNum(like)
setIsLiked(liked)
}, [like, liked, isFavorite])

useEffect(() => {
if (ref.current) {
setCurrentCardWidth(ref.current.clientWidth)
Expand All @@ -59,28 +66,58 @@ function PostCard({
)
.then((res) => {
if (res.status === 200) {
setFavorite(!favorite)
mutate((prev) => {
return prev.map((card) => {
if (card.id === postId) {
return {
...card,
favorite: !favorite,
}
}
return card
})
})
}
})
.catch(() => {})
}, [setFavorite, axiosWithAuth])
}, [favorite, setFavorite, mutate])

const clickLike = useCallback(() => {
axiosWithAuth
.post(`${process.env.NEXT_PUBLIC_CSR_API}/api/v1/showcase/like/${postId}`)
.then((res) => {
if (res.status === 200) {
if (liked === false) {
setIsLiked(true)
setLikeNum(likeNum + 1)
if (isLiked === false) {
mutate((prev) => {
return prev.map((card) => {
if (card.id === postId) {
return {
...card,
like: likeNum + 1,
liked: !isLiked,
}
}
return card
})
})
} else {
setIsLiked(false)
setLikeNum(likeNum - 1)
mutate((prev) => {
return prev.map((card) => {
if (card.id === postId) {
return {
...card,
like: likeNum - 1,
liked: !isLiked,
}
}
return card
})
})
}
}
})
.catch(() => {})
}, [setIsLiked, setLikeNum, axiosWithAuth])
}, [isLiked, likeNum, setLikeNum, setIsLiked])

return (
<Card
Expand Down
11 changes: 10 additions & 1 deletion src/app/showcase/panel/ShowcaseCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import {
Box,
} from '@mui/material'
import { useRouter } from 'next/navigation'
import React, { useEffect, useRef, useState } from 'react'
import React, {
Dispatch,
SetStateAction,
useEffect,
useRef,
useState,
} from 'react'
import DropdownMenu from '@/components/DropdownMenu'
import * as style from './ShowcaseCard.style'
import { ICardData } from '@/app/showcase/panel/types'
Expand Down Expand Up @@ -237,11 +243,13 @@ const ShowcaseCard = ({
data,
dragged,
setDragged,
mutate,
}: {
data: ICardData
sx?: SxProps
dragged: boolean
setDragged: React.Dispatch<React.SetStateAction<boolean>>
mutate: Dispatch<SetStateAction<ICardData[]>>
}) => {
const [isFlipped, setIsFlipped] = useState(false)
const [currentDomain, setCurrentDomain] = useState('')
Expand Down Expand Up @@ -285,6 +293,7 @@ const ShowcaseCard = ({
transform: 'translate(-50%, 0)',
}}
onClick={handleMouseUp}
mutate={mutate}
/>
<ShowcaseCardBack
postId={data.id}
Expand Down
4 changes: 3 additions & 1 deletion src/types/IPostCard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SxProps } from '@mui/material'
import { ITag } from './IPostDetail'
import { IShowcaseTag } from '@/app/showcase/panel/types'
import { ICardData, IShowcaseTag } from '@/app/showcase/panel/types'
import { Dispatch, SetStateAction } from 'react'

export interface IPostCard {
authorImage: string // 글 작성자 프로필 이미지
Expand Down Expand Up @@ -36,6 +37,7 @@ export interface IPostCardShowcase {
liked: boolean
sx?: SxProps
onClick?: (e: React.MouseEvent) => void
mutate: Dispatch<SetStateAction<ICardData[]>>
}

export interface IHitchhikingCardBack {
Expand Down

0 comments on commit 2f4c394

Please sign in to comment.