|
| 1 | +import { Res } from 'common/types/responses' |
| 2 | +import { Req } from 'common/types/requests' |
| 3 | +import { service } from 'common/service' |
| 4 | +import toFormData from 'common/utils/toFormData' |
| 5 | + |
| 6 | +export const cohortService = service |
| 7 | + .enhanceEndpoints({ addTagTypes: ['Cohort', 'Segment'] }) |
| 8 | + .injectEndpoints({ |
| 9 | + endpoints: (builder) => ({ |
| 10 | + createCohort: builder.mutation<Res['cohort'], Req['createCohort']>({ |
| 11 | + invalidatesTags: (q, e, arg) => [ |
| 12 | + { id: 'LIST', type: 'Cohort' }, |
| 13 | + { id: `LIST${arg.projectId}`, type: 'Segment' }, |
| 14 | + ], |
| 15 | + query: (query) => ({ |
| 16 | + body: { |
| 17 | + description: query.description, |
| 18 | + metadata: query.metadata, |
| 19 | + name: query.name, |
| 20 | + }, |
| 21 | + method: 'POST', |
| 22 | + url: `environments/${query.environmentApiKey}/cohorts/`, |
| 23 | + }), |
| 24 | + }), |
| 25 | + deleteCohort: builder.mutation<void, Req['deleteCohort']>({ |
| 26 | + invalidatesTags: (q, e, arg) => [ |
| 27 | + { id: 'LIST', type: 'Cohort' }, |
| 28 | + { id: `LIST${arg.projectId}`, type: 'Segment' }, |
| 29 | + ], |
| 30 | + query: (query) => ({ |
| 31 | + method: 'DELETE', |
| 32 | + url: `environments/${query.environmentApiKey}/cohorts/${query.cohortId}/`, |
| 33 | + }), |
| 34 | + }), |
| 35 | + syncCohortCsv: builder.mutation< |
| 36 | + Res['cohortCsvSync'], |
| 37 | + Req['syncCohortCsv'] |
| 38 | + >({ |
| 39 | + invalidatesTags: (q, e, arg) => [ |
| 40 | + { id: arg.cohortId, type: 'Cohort' }, |
| 41 | + { id: `LIST${arg.projectId}`, type: 'Segment' }, |
| 42 | + ], |
| 43 | + queryFn: async (query, baseQueryApi, extraOptions, baseQuery) => { |
| 44 | + // projectId only feeds tag invalidation; keep it out of the form data. |
| 45 | + const { cohortId, environmentApiKey, projectId: _, ...rest } = query |
| 46 | + const formData = toFormData({ ...rest }) |
| 47 | + const { data, error } = await baseQuery({ |
| 48 | + body: formData, |
| 49 | + method: 'POST', |
| 50 | + url: `environments/${environmentApiKey}/cohorts/${cohortId}/sync-csv/`, |
| 51 | + }) |
| 52 | + return { data, error } as { |
| 53 | + data: Res['cohortCsvSync'] |
| 54 | + error: never |
| 55 | + } |
| 56 | + }, |
| 57 | + }), |
| 58 | + // END OF ENDPOINTS |
| 59 | + }), |
| 60 | + }) |
| 61 | + |
| 62 | +export async function deleteCohort( |
| 63 | + store: any, |
| 64 | + data: Req['deleteCohort'], |
| 65 | + options?: Parameters<typeof cohortService.endpoints.deleteCohort.initiate>[1], |
| 66 | +) { |
| 67 | + return store.dispatch( |
| 68 | + cohortService.endpoints.deleteCohort.initiate(data, options), |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +export const { |
| 73 | + useCreateCohortMutation, |
| 74 | + useDeleteCohortMutation, |
| 75 | + useSyncCohortCsvMutation, |
| 76 | + // END OF EXPORTS |
| 77 | +} = cohortService |
| 78 | + |
| 79 | +/* Usage examples: |
| 80 | +const [createCohort, { isLoading, data, isSuccess }] = useCreateCohortMutation() |
| 81 | +const [syncCohortCsv, { isLoading }] = useSyncCohortCsvMutation() |
| 82 | +*/ |
0 commit comments