Skip to content

Commit

Permalink
Add useRefetchTrips hook for activity create, update, and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
samuraikun committed Feb 16, 2024
1 parent e123580 commit 468aaba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/activity/hooks/useActivityCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useToast } from '@chakra-ui/react'
import { useRouter } from 'next/navigation'
import { formatToISODate } from '@/libs/utils'
import { ActivitySchema } from '../schema'
import { useRefetchTrips } from './useRefetchTrips'
import { useUploadFiles } from './useUploadFiles'
import { useCreateActivityMutation } from '@generated/api'

Expand All @@ -11,6 +12,7 @@ export const useActivityCreate = (tripId: string) => {
const [createActivityMutation, { loading: isActivityCreating }] =
useCreateActivityMutation()
const { uploadFiles } = useUploadFiles()
const { refetchTrips } = useRefetchTrips()

const createActivity = async (activityData: ActivitySchema) => {
try {
Expand Down Expand Up @@ -41,6 +43,7 @@ export const useActivityCreate = (tripId: string) => {
await uploadFiles(activityData.newFiles, { id: createdActivityId, tripId })
}

refetchTrips(tripId)
router.push(`/activity/${createdActivityId}`)
toast({
title: 'Successfully created!',
Expand Down
3 changes: 3 additions & 0 deletions app/activity/hooks/useActivityUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useToast } from '@chakra-ui/react'
import { useRouter } from 'next/navigation'
import { formatToISODate } from '@/libs/utils'
import { ActivitySchema } from '../schema'
import { useRefetchTrips } from './useRefetchTrips'
import { useUploadFiles } from './useUploadFiles'
import { useUpdateActivityMutation } from '@generated/api'

Expand All @@ -11,6 +12,7 @@ export const useActivityUpdate = (tripId: string) => {
const [updateActivityMutation, { loading: isActivityUpdating }] =
useUpdateActivityMutation()
const { uploadFiles } = useUploadFiles()
const { refetchTrips } = useRefetchTrips()

const updateActivity = async (activityId: string, activityData: ActivitySchema) => {
try {
Expand Down Expand Up @@ -41,6 +43,7 @@ export const useActivityUpdate = (tripId: string) => {
await uploadFiles(activityData.newFiles, { id: activityId, tripId })
}

refetchTrips(tripId)
router.push(`/activity/${activityId}`)
toast({
title: 'Successfully updated!',
Expand Down
18 changes: 18 additions & 0 deletions app/activity/hooks/useRefetchTrips.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useTripsCollectionQuery } from "@generated/api"

// This hook is used to refetch trips after an activity is created, updated, or deleted.
export const useRefetchTrips = () => {
const { refetch } = useTripsCollectionQuery({})

const refetchTrips = (tripId: string) => {
refetch({
filter: {
id: { eq: tripId }
},
first: 12,
after: null
})
}

return { refetchTrips }
}

0 comments on commit 468aaba

Please sign in to comment.