Created
October 24, 2023 07:23
-
-
Save tanat/429b1f0f8484c39e4af111834d7f1e16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormJob, Job, ListResponse, Region, Tag } from '@/models' | |
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react" | |
export const api = createApi({ | |
reducerPath: 'api', | |
baseQuery: fetchBaseQuery({ | |
baseUrl: process.env.NEXT_PUBLIC_API_URL | |
}), | |
endpoints: (builder) => ({ | |
getTags: builder.query<Tag[], void>({ | |
query: () => "tags/", | |
}), | |
getRegions: builder.query<Region[], void>({ | |
query: () => "regions/", | |
}), | |
getJobs: builder.query<ListResponse<Job>>({ | |
query: ({ tag, page, regions, pageSize = 2 }) => { | |
let params: Record<string, any> = { | |
'page_size': pageSize, | |
} | |
if (tag) { | |
params['tags'] = tag.id | |
} | |
if (page) { | |
params['page'] = page | |
} | |
if (regions) { | |
params['regions'] = regions.map(region => region.id).join(',') | |
} | |
return { | |
url: 'jobs/', | |
params | |
} | |
}, | |
}), | |
getCsrfToken: builder.query<{ csrfToken: string }, void>({ | |
query: () => "token/", | |
}), | |
createJob: builder.mutation<Job, Partial<Job & { csrfToken: string, uuid: string }>>({ | |
query: ({ regions, additionalServices, csrfToken, ...job }) => ({ | |
url: `jobs/ `, | |
method: 'POST', | |
headers: { | |
'X-CSRFToken': csrfToken, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: { | |
...job, | |
regionsIds: regions!!.map(r => r.id), | |
additionalServicesIds: [] | |
}, | |
}), | |
}), | |
updateJob: builder.mutation<Job, Partial<Job & { csrfToken: string, uuid: string }>>({ | |
query: ({ regions, additionalServices, uuid, csrfToken, ...job }) => ({ | |
url: `jobs/uuid/${uuid}/edit`, | |
method: 'POST', | |
credentials: 'include', | |
headers: { | |
'X-CSRFToken': csrfToken, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: { | |
...job, | |
regionsIds: regions!!.map(r => r.id), | |
additionalServicesIds: [] | |
}, | |
}), | |
}), | |
verifyCaptcha: builder.mutation({ | |
query: (token: string) => ({ | |
url: `${location.origin}/api/captcha`, | |
method: 'POST', | |
body: { token }, | |
}), | |
}), | |
}), | |
}) | |
export const { | |
useGetTagsQuery, | |
useLazyGetTagsQuery, | |
useGetRegionsQuery, | |
useLazyGetRegionsQuery, | |
useGetJobsQuery, | |
useLazyGetJobsQuery, | |
useUpdateJobMutation, | |
useCreateJobMutation, | |
useVerifyCaptchaMutation, | |
useGetCsrfTokenQuery, | |
useLazyGetCsrfTokenQuery, | |
util: { getRunningQueriesThunk }, | |
} = api | |
export const { getTags } = api.endpoints |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment