|
| 1 | +import { |
| 2 | + useInfiniteQuery, |
| 3 | + useMutation, |
| 4 | + useQuery, |
| 5 | + useQueryClient, |
| 6 | +} from "@tanstack/react-query"; |
| 7 | +import { http } from "./config"; |
| 8 | + |
| 9 | +const DEFAULT_POPULATE = "developerId,category,technologies,awards"; |
| 10 | + |
| 11 | +const normalizeArrayParam = (value) => { |
| 12 | + if (!value) return undefined; |
| 13 | + if (Array.isArray(value)) return value.filter(Boolean).join(","); |
| 14 | + return value; |
| 15 | +}; |
| 16 | + |
| 17 | +const buildParams = ({ search, page, limit, start, populate, filters } = {}) => { |
| 18 | + const params = {}; |
| 19 | + if (search) params.q = search; |
| 20 | + if (typeof start === "number") params._start = start; |
| 21 | + if (typeof limit === "number") params._limit = limit; |
| 22 | + if (typeof page === "number") params.page = page; |
| 23 | + |
| 24 | + const populateValue = normalizeArrayParam(populate); |
| 25 | + if (populateValue) params.populate = populateValue; |
| 26 | + |
| 27 | + if (filters && typeof filters === "object") { |
| 28 | + Object.entries(filters).forEach(([key, value]) => { |
| 29 | + if (value === undefined || value === null || value === "") return; |
| 30 | + params[key] = normalizeArrayParam(value) ?? value; |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + return params; |
| 35 | +}; |
| 36 | + |
| 37 | +const fetchProjects = async ({ |
| 38 | + search, |
| 39 | + page, |
| 40 | + limit, |
| 41 | + start, |
| 42 | + populate, |
| 43 | + filters, |
| 44 | + signal, |
| 45 | +}) => { |
| 46 | + const { data } = await http.get("/api/projects", { |
| 47 | + params: buildParams({ search, page, limit, start, populate, filters }), |
| 48 | + signal, |
| 49 | + }); |
| 50 | + return data; |
| 51 | +}; |
| 52 | + |
| 53 | +export const useProjects = ({ |
| 54 | + search = "", |
| 55 | + page, |
| 56 | + limit = 20, |
| 57 | + start, |
| 58 | + populate = DEFAULT_POPULATE, |
| 59 | + filters, |
| 60 | + enabled = true, |
| 61 | +} = {}) => |
| 62 | + useQuery({ |
| 63 | + queryKey: ["projects", { search, page, limit, start, populate, filters }], |
| 64 | + queryFn: ({ signal }) => |
| 65 | + fetchProjects({ search, page, limit, start, populate, filters, signal }), |
| 66 | + enabled, |
| 67 | + }); |
| 68 | + |
| 69 | +export const useInfiniteProjects = ({ |
| 70 | + search = "", |
| 71 | + limit = 6, |
| 72 | + populate = DEFAULT_POPULATE, |
| 73 | + filters, |
| 74 | +} = {}) => |
| 75 | + useInfiniteQuery({ |
| 76 | + queryKey: ["projects", { search, limit, populate, filters, mode: "infinite" }], |
| 77 | + queryFn: ({ pageParam = 0, signal }) => |
| 78 | + fetchProjects({ |
| 79 | + search, |
| 80 | + limit, |
| 81 | + start: pageParam, |
| 82 | + populate, |
| 83 | + filters, |
| 84 | + signal, |
| 85 | + }), |
| 86 | + getNextPageParam: (lastPage, allPages) => { |
| 87 | + if (!Array.isArray(lastPage)) return undefined; |
| 88 | + if (lastPage.length < limit) return undefined; |
| 89 | + return allPages.length * limit; |
| 90 | + }, |
| 91 | + initialPageParam: 0, |
| 92 | + }); |
| 93 | + |
| 94 | +export const useProject = (id, { populate = DEFAULT_POPULATE } = {}) => |
| 95 | + useQuery({ |
| 96 | + queryKey: ["project", id, populate], |
| 97 | + queryFn: async ({ signal }) => { |
| 98 | + const { data } = await http.get(`/api/projects/${id}`, { |
| 99 | + params: buildParams({ populate }), |
| 100 | + signal, |
| 101 | + }); |
| 102 | + return data; |
| 103 | + }, |
| 104 | + enabled: Boolean(id), |
| 105 | + }); |
| 106 | + |
| 107 | +export const useCreateProject = () => { |
| 108 | + const queryClient = useQueryClient(); |
| 109 | + return useMutation({ |
| 110 | + mutationFn: async (payload) => { |
| 111 | + const { data } = await http.post("/api/projects", payload); |
| 112 | + return data; |
| 113 | + }, |
| 114 | + onSuccess: (created) => { |
| 115 | + queryClient.invalidateQueries({ queryKey: ["projects"] }); |
| 116 | + if (created?.id || created?._id) { |
| 117 | + queryClient.setQueryData( |
| 118 | + ["project", created.id || created._id, DEFAULT_POPULATE], |
| 119 | + created |
| 120 | + ); |
| 121 | + } |
| 122 | + }, |
| 123 | + }); |
| 124 | +}; |
| 125 | + |
| 126 | +export const useUpdateProject = () => { |
| 127 | + const queryClient = useQueryClient(); |
| 128 | + return useMutation({ |
| 129 | + mutationFn: async ({ id, payload }) => { |
| 130 | + const { data } = await http.patch(`/api/projects/${id}`, payload); |
| 131 | + return data; |
| 132 | + }, |
| 133 | + onSuccess: (updated, variables) => { |
| 134 | + if (variables?.id) { |
| 135 | + queryClient.setQueryData( |
| 136 | + ["project", variables.id, DEFAULT_POPULATE], |
| 137 | + updated |
| 138 | + ); |
| 139 | + } |
| 140 | + queryClient.invalidateQueries({ queryKey: ["projects"] }); |
| 141 | + }, |
| 142 | + }); |
| 143 | +}; |
| 144 | + |
| 145 | +export const useDeleteProject = () => { |
| 146 | + const queryClient = useQueryClient(); |
| 147 | + return useMutation({ |
| 148 | + mutationFn: async (id) => { |
| 149 | + const { data } = await http.delete(`/api/projects/${id}`); |
| 150 | + return data; |
| 151 | + }, |
| 152 | + onSuccess: (_data, id) => { |
| 153 | + queryClient.removeQueries({ queryKey: ["project", id] }); |
| 154 | + queryClient.invalidateQueries({ queryKey: ["projects"] }); |
| 155 | + }, |
| 156 | + }); |
| 157 | +}; |
| 158 | + |
0 commit comments