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

fix: fix places data revalidation and slicing #160

Merged
merged 3 commits into from
Oct 4, 2024
Merged
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
7 changes: 6 additions & 1 deletion src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use server'

import { cookies } from 'next/headers'
import { revalidateTag } from 'next/cache'

const setCookie = async (name: string, value: string) => {
cookies().set(name, value)
Expand All @@ -10,4 +11,8 @@ const deleteCookie = async (name: string) => {
cookies().delete(name)
}

export { setCookie, deleteCookie }
const revalidatePlaces = async (mapId: string) => {
revalidateTag(`places-${mapId}`)
}

export { setCookie, deleteCookie, revalidatePlaces }
8 changes: 5 additions & 3 deletions src/app/map/[mapId]/place-list-bottom-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { APIError } from '@/models/api/index'
import type { PlaceType } from '@/models/api/place'
import { useInfiniteScroll } from '@/hooks/use-infinite-scroll'
import { api } from '@/utils/api'
import { revalidatePlaces } from '@/app/actions'

interface PlaceListBottomSheetProps {
places: PlaceType[] | null
Expand All @@ -30,16 +31,17 @@ const PlaceListBottomSheet = forwardRef<
{ places, mapId, selectedFilter, onClickFilterButton, onRefreshOldPlace },
ref,
) => {
const [placeList, setPlaceList] = useState<PlaceType[]>(places || [])
const [placeList, setPlaceList] = useState<PlaceType[]>([])
const { data: slicedPlaceList, listRef } = useInfiniteScroll<PlaceType>({
totalData: places || [],
itemsPerPage: 10,
})

const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
key: ['user'],
})
const userId = user?.id

const numOfSelectedFilter =
(selectedFilter?.category !== 'all' ? 1 : 0) +
(selectedFilter?.tags.length ?? 0)
Expand Down Expand Up @@ -77,7 +79,7 @@ const PlaceListBottomSheet = forwardRef<
placeId: place.place.id,
})
}
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
if (error instanceof APIError) {
notify.error(error.message)
Expand Down
7 changes: 6 additions & 1 deletion src/app/place/[placeId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'use client'

import type { ReactNode } from 'react'
import { useRouter } from 'next/navigation'
import { usePathname, useRouter } from 'next/navigation'

import AccessibleIconButton from '@/components/common/accessible-icon-button'

const PlaceDetailLayout = ({ children }: { children: ReactNode }) => {
const router = useRouter()
const pathname = usePathname()

if (pathname.includes('register')) {
return <>{children}</>
}

return (
<div className="relative flex min-h-dvh flex-col bg-neutral-700">
Expand Down
18 changes: 10 additions & 8 deletions src/app/place/[placeId]/place-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { roundToNthDecimal } from '@/utils/number'
import { allowUserPositionStorage } from '@/utils/storage'
import cn from '@/utils/cn'
import { sendGAEvent } from '@next/third-parties/google'
import { revalidatePlaces } from '@/app/actions'

interface PlaceBoxProps {
place: PlaceDetail
Expand All @@ -40,7 +41,7 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)
const [isRecentlyLike, setIsRecentlyLike] = useState<boolean | null>(null)
const router = useSafeRouter()
const [isAlreadyPick, setIsAlreadyPick] = useState(place.isRegisteredPlace)
const isAlreadyPick = place.isRegisteredPlace
const { userLocation } = useUserGeoLocation()
const isAllowPosition = allowUserPositionStorage.getValueOrNull()
const diffDistance = getDistance(
Expand All @@ -50,10 +51,9 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
place.x,
)

const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
key: ['user'],
})

const { data: mapInfo, isFetching } = useFetch(() => api.maps.id.get(mapId), {
enabled: !!mapId,
})
Expand All @@ -76,6 +76,10 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
return likedUserCount + recentlyLikedBonus
})()

useEffect(() => {
router.refresh()
}, [router])

useEffect(() => {
if (!place || !user) return
setIsLikePlace(
Expand All @@ -93,7 +97,7 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(false)
setIsRecentlyLike(
Expand All @@ -115,7 +119,7 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(true)
setIsRecentlyLike(
Expand All @@ -136,9 +140,8 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
mapId,
})

setIsAlreadyPick(false)
revalidatePlaces(mapId)
setIsDeleteModalOpen(false)
window.location.reload()
} catch (error) {
if (error instanceof APIError || error instanceof Error) {
notify.error(error.message)
Expand All @@ -154,7 +157,6 @@ const PlaceBox = ({ place, mapId }: PlaceBoxProps) => {
label: 'register',
})
router.push(`/place/${place.kakaoId}/register`)
revalidate(['places', mapId])
} catch (error) {
if (error instanceof APIError || error instanceof Error) {
notify.error(error.message)
Expand Down
6 changes: 3 additions & 3 deletions src/app/place/[placeId]/register/register-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { TagItem } from '@/models/api/maps'
import type { PlaceDetail } from '@/models/api/place'
import { api } from '@/utils/api'
import get조사 from '@/utils/조사'
import useFetch from '@/hooks/use-fetch'
import { revalidatePlaces } from '@/app/actions'

const toTagNames = (tags: TagItem[]): TagItem['name'][] =>
tags.map((tag) => tag.name)
Expand All @@ -29,7 +29,6 @@ const RegisterBox = ({
tags: TagItem[]
mapId: string
}) => {
const { revalidate } = useFetch()
const router = useSafeRouter()
const [selectedTags, setSelectedTags] = useState<TagItem[]>([])
const [isOpenBackModal, setIsOpenBackModal] = useState(false)
Expand All @@ -46,7 +45,8 @@ const RegisterBox = ({
tagNames: toTagNames(selectedTags),
})

revalidate(['map-list'])
revalidatePlaces(mapId)

notify.success('맛집 등록이 완료되었습니다.')
router.safeBack({ defaultHref: `/place/${place.kakaoId}` })
} catch (error) {
Expand Down
7 changes: 4 additions & 3 deletions src/app/profile/[mapId]/[id]/place-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { api } from '@/utils/api'
import cn from '@/utils/cn'
import { roundOnePoint } from '@/utils/number'
import { getStarByScore } from '@/utils/score'
import { revalidatePlaces } from '@/app/actions'

interface PlaceItemProps extends ClassName {
selectedPlace: PlaceType
Expand All @@ -29,7 +30,7 @@ interface PlaceItemProps extends ClassName {
const PlaceItem = forwardRef<HTMLAnchorElement, PlaceItemProps>(
({ selectedPlace, className, mapId, onRefreshOldPlace }, ref) => {
const [isLikePlace, setIsLikePlace] = useState(false)
const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
key: ['user'],
})

Expand All @@ -56,7 +57,7 @@ const PlaceItem = forwardRef<HTMLAnchorElement, PlaceItemProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(false)
if (error instanceof APIError || error instanceof Error) {
Expand All @@ -78,7 +79,7 @@ const PlaceItem = forwardRef<HTMLAnchorElement, PlaceItemProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(true)
if (error instanceof APIError || error instanceof Error) {
Expand Down
7 changes: 4 additions & 3 deletions src/app/search/[query]/result-place-map-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { api } from '@/utils/api'
import cn from '@/utils/cn'
import { roundOnePoint } from '@/utils/number'
import { getStarByScore } from '@/utils/score'
import { revalidatePlaces } from '@/app/actions'

interface ResultPlaceMapPopupProps extends ClassName {
mapId: MapInfo['id']
Expand All @@ -35,7 +36,7 @@ const ResultPlaceMapPopup = forwardRef<HTMLElement, ResultPlaceMapPopupProps>(

const [isLoading, setIsLoading] = useState(false)
const [place, setPlace] = useState<PlaceDetail | null>(null)
const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
onLoadEnd: (userData) => {
setIsLikePlace(
!!place?.likedUser?.find((liked) => liked.id === userData.id),
Expand Down Expand Up @@ -92,7 +93,7 @@ const ResultPlaceMapPopup = forwardRef<HTMLElement, ResultPlaceMapPopupProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(false)
setIsRecentlyLike(
Expand All @@ -112,7 +113,7 @@ const ResultPlaceMapPopup = forwardRef<HTMLElement, ResultPlaceMapPopupProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(true)
setIsRecentlyLike(
Expand Down
7 changes: 4 additions & 3 deletions src/app/search/[query]/result-search-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { api } from '@/utils/api'
import cn from '@/utils/cn'
import { formatDistance, getDistance } from '@/utils/location'
import { allowUserPositionStorage } from '@/utils/storage'
import { revalidatePlaces } from '@/app/actions'

interface ResultSearchListBoxProps extends ClassName {
places: SearchPlace[]
Expand All @@ -26,7 +27,7 @@ const ResultSearchListBox = ({
places,
mapId,
}: ResultSearchListBoxProps) => {
const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
key: ['user'],
})
const { userLocation } = useUserGeoLocation()
Expand Down Expand Up @@ -87,7 +88,7 @@ const ResultSearchListBox = ({
if (!mapId) return
optimisticUpdateLikeOrUnLike(placeId)
await api.place.mapId.placeId.like.put({ placeId, mapId })
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {}
}

Expand All @@ -96,7 +97,7 @@ const ResultSearchListBox = ({
if (!mapId) return
optimisticUpdateLikeOrUnLike(placeId)
await api.place.mapId.placeId.like.delete({ placeId, mapId })
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {}
}

Expand Down
7 changes: 4 additions & 3 deletions src/components/place/place-map-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cn from '@/utils/cn'
import { roundOnePoint } from '@/utils/number'
import { getStarByScore } from '@/utils/score'
import PlacePopupSkeleton from '@/components/place/place-popup-skeleton'
import { revalidatePlaces } from '@/app/actions'

interface PlaceMapPopupProps extends ClassName {
selectedPlace: PlaceType
Expand All @@ -30,7 +31,7 @@ interface PlaceMapPopupProps extends ClassName {
const PlaceMapPopup = forwardRef<HTMLAnchorElement, PlaceMapPopupProps>(
({ selectedPlace, className, mapId, onRefreshOldPlace }, ref) => {
const [isLikePlace, setIsLikePlace] = useState(false)
const { data: user, revalidate } = useFetch(api.users.me.get, {
const { data: user } = useFetch(api.users.me.get, {
key: ['user'],
})

Expand Down Expand Up @@ -59,7 +60,7 @@ const PlaceMapPopup = forwardRef<HTMLAnchorElement, PlaceMapPopupProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(false)
if (error instanceof APIError || error instanceof Error) {
Expand All @@ -81,7 +82,7 @@ const PlaceMapPopup = forwardRef<HTMLAnchorElement, PlaceMapPopupProps>(
placeId: place.id,
mapId,
})
revalidate(['places', mapId])
revalidatePlaces(mapId)
} catch (error) {
setIsLikePlace(true)
if (error instanceof APIError || error instanceof Error) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ const search = {
mapId,
}: QueryParams & { mapId: MapInfo['id'] }): Promise<
ResponseWithMessage<SearchPlace[]>
> => client.public.get(`/search/places?q=${q}&rect=${rect}&mapId=${mapId}`),
> =>
client.public.get(`/search/places?q=${q}&rect=${rect}&mapId=${mapId}`, {
tags: ['places', mapId],
}),
},
}

Expand Down
Loading