|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import React from "react"; |
4 | | -import { useQuery, useMutation } from "@tanstack/react-query"; |
| 4 | +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; |
5 | 5 | import { eventsService } from "@/services/events"; |
6 | 6 | import { uploadsService } from "@/services/uploads"; |
7 | 7 | import { EventFormType, CreateEventPayload } from "@/domains/Events"; |
8 | 8 | import DialogSuccess from "@/components/common/DialogGlobal/DialogSuccess"; |
9 | 9 | import { useRouter } from "@/lib/navigation"; |
10 | 10 | import { useDialog } from "@/contexts"; |
11 | 11 | import DialogError from "@/components/common/DialogGlobal/DialogError"; |
| 12 | +import { getFileNameFromUrl } from "@/lib/image"; |
12 | 13 |
|
13 | 14 | export const useEventById = (eventId: string) => { |
14 | 15 | return useQuery({ |
@@ -54,11 +55,14 @@ export const useEventsAdmin = (page: number, limit: number, search?: string) => |
54 | 55 | export const useCreateEvent = (t: (key: string) => string) => { |
55 | 56 | const router = useRouter(); |
56 | 57 | const { openDialog, closeDialog } = useDialog(); |
| 58 | + const queryClient = useQueryClient(); |
57 | 59 |
|
58 | 60 | const submitMutation = useMutation({ |
59 | 61 | mutationKey: ["createEvent"], |
60 | 62 | mutationFn: (payload: CreateEventPayload) => eventsService.createEventAdmin(payload), |
61 | 63 | onSuccess: () => { |
| 64 | + queryClient.invalidateQueries({ queryKey: ["eventsAdmin"] }); |
| 65 | + |
62 | 66 | openDialog({ |
63 | 67 | content: React.createElement(DialogSuccess, { |
64 | 68 | title: t("EventForm.create-success-title"), |
@@ -116,3 +120,97 @@ export const useCreateEvent = (t: (key: string) => string) => { |
116 | 120 | isLoading, |
117 | 121 | }; |
118 | 122 | }; |
| 123 | + |
| 124 | +export const useGetDetailEventAdmin = (id: string) => { |
| 125 | + return useQuery({ |
| 126 | + queryKey: ["getDetailEventAdmin", id], |
| 127 | + queryFn: async () => eventsService.getDetailEventAdmin(id), |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +export const useUpdateEvent = (t: (key: string) => string, id: string) => { |
| 132 | + const router = useRouter(); |
| 133 | + const { openDialog, closeDialog } = useDialog(); |
| 134 | + const queryClient = useQueryClient(); |
| 135 | + |
| 136 | + const { mutate: mutateUpdateEvent, isPending: loadingCreateEvent } = useMutation({ |
| 137 | + mutationKey: ["updateEvent"], |
| 138 | + mutationFn: (payload: CreateEventPayload) => eventsService.updateEventAdmin(id, payload), |
| 139 | + onSuccess: () => { |
| 140 | + queryClient.invalidateQueries({ queryKey: ["getDetailEventAdmin", id] }); |
| 141 | + queryClient.invalidateQueries({ queryKey: ["eventsAdmin"] }); |
| 142 | + |
| 143 | + openDialog({ |
| 144 | + content: React.createElement(DialogSuccess, { |
| 145 | + title: t("EventForm.update-success-title"), |
| 146 | + description: t("EventForm.update-success-description"), |
| 147 | + }), |
| 148 | + confirmText: t("EventForm.back-to-list"), |
| 149 | + onConfirm: () => { |
| 150 | + router.push("/admin/events"); |
| 151 | + closeDialog(); |
| 152 | + }, |
| 153 | + classAction: "sm:justify-center", |
| 154 | + }); |
| 155 | + }, |
| 156 | + onError: () => { |
| 157 | + openDialog({ |
| 158 | + content: React.createElement(DialogError, { |
| 159 | + title: t("EventForm.update-error-title"), |
| 160 | + description: t("EventForm.update-error-description"), |
| 161 | + }), |
| 162 | + cancelText: "OK", |
| 163 | + classAction: "sm:justify-center", |
| 164 | + }); |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + const { mutate: mutateUpdateImage, isPending: loadingCreateImage } = useMutation({ |
| 169 | + mutationFn: (payload: EventFormType) => |
| 170 | + uploadsService.updateImageAdmin(payload.image, "events", getFileNameFromUrl(payload?.file_name as string)), |
| 171 | + onSuccess: (data, variables) => { |
| 172 | + const filename = data.data.file_name; |
| 173 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 174 | + const { image, ...rest } = variables; |
| 175 | + mutateUpdateEvent({ |
| 176 | + ...rest, |
| 177 | + file_name: filename, |
| 178 | + }); |
| 179 | + }, |
| 180 | + onError: () => { |
| 181 | + openDialog({ |
| 182 | + content: React.createElement(DialogError, { |
| 183 | + title: t("EventForm.upload-error-title"), |
| 184 | + description: t("EventForm.upload-error-description"), |
| 185 | + }), |
| 186 | + onConfirm: () => { |
| 187 | + closeDialog(); |
| 188 | + }, |
| 189 | + confirmText: "OK", |
| 190 | + classAction: "sm:justify-center", |
| 191 | + }); |
| 192 | + }, |
| 193 | + }); |
| 194 | + |
| 195 | + const updateEvent = (payload: EventFormType) => { |
| 196 | + if (payload.image instanceof File) { |
| 197 | + return mutateUpdateImage(payload); |
| 198 | + } else if (payload.file_name) { |
| 199 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 200 | + const { image, ...rest } = payload; |
| 201 | + return mutateUpdateEvent({ |
| 202 | + ...rest, |
| 203 | + file_name: getFileNameFromUrl(payload.file_name), |
| 204 | + }); |
| 205 | + } else { |
| 206 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 207 | + const { image, file_name, ...rest } = payload; |
| 208 | + return mutateUpdateEvent(rest); |
| 209 | + } |
| 210 | + }; |
| 211 | + |
| 212 | + return { |
| 213 | + updateEvent, |
| 214 | + isLoading: loadingCreateEvent || loadingCreateImage, |
| 215 | + }; |
| 216 | +}; |
0 commit comments