|
| 1 | +import { Res } from 'common/types/responses' |
| 2 | +import { Req } from 'common/types/requests' |
| 3 | +import { service } from 'common/service' |
| 4 | + |
| 5 | +export const trustRelationshipService = service |
| 6 | + .enhanceEndpoints({ |
| 7 | + addTagTypes: ['TrustRelationship', 'MasterAPIKeyWithMasterAPIKeyRole'], |
| 8 | + }) |
| 9 | + .injectEndpoints({ |
| 10 | + endpoints: (builder) => ({ |
| 11 | + createTrustRelationship: builder.mutation< |
| 12 | + Res['trustRelationship'], |
| 13 | + Req['createTrustRelationship'] |
| 14 | + >({ |
| 15 | + invalidatesTags: [{ id: 'LIST', type: 'TrustRelationship' }], |
| 16 | + query: (query: Req['createTrustRelationship']) => ({ |
| 17 | + body: query.body, |
| 18 | + method: 'POST', |
| 19 | + url: `organisations/${query.organisation_id}/trust-relationships/`, |
| 20 | + }), |
| 21 | + }), |
| 22 | + deleteTrustRelationship: builder.mutation< |
| 23 | + void, |
| 24 | + Req['deleteTrustRelationship'] |
| 25 | + >({ |
| 26 | + invalidatesTags: [{ id: 'LIST', type: 'TrustRelationship' }], |
| 27 | + query: (query: Req['deleteTrustRelationship']) => ({ |
| 28 | + method: 'DELETE', |
| 29 | + url: `organisations/${query.organisation_id}/trust-relationships/${query.id}/`, |
| 30 | + }), |
| 31 | + }), |
| 32 | + getTrustRelationships: builder.query< |
| 33 | + Res['trustRelationships'], |
| 34 | + Req['getTrustRelationships'] |
| 35 | + >({ |
| 36 | + providesTags: [{ id: 'LIST', type: 'TrustRelationship' }], |
| 37 | + query: (query: Req['getTrustRelationships']) => ({ |
| 38 | + url: `organisations/${query.organisation_id}/trust-relationships/`, |
| 39 | + }), |
| 40 | + }), |
| 41 | + updateTrustRelationship: builder.mutation< |
| 42 | + Res['trustRelationship'], |
| 43 | + Req['updateTrustRelationship'] |
| 44 | + >({ |
| 45 | + // Turning `is_admin` on detaches the backing key's roles server-side, |
| 46 | + // so the cached role list has to go with it. |
| 47 | + invalidatesTags: (res) => [ |
| 48 | + { id: 'LIST', type: 'TrustRelationship' }, |
| 49 | + { id: res?.id, type: 'TrustRelationship' }, |
| 50 | + 'MasterAPIKeyWithMasterAPIKeyRole', |
| 51 | + ], |
| 52 | + query: (query: Req['updateTrustRelationship']) => ({ |
| 53 | + body: query.body, |
| 54 | + method: 'PUT', |
| 55 | + url: `organisations/${query.organisation_id}/trust-relationships/${query.id}/`, |
| 56 | + }), |
| 57 | + }), |
| 58 | + // END OF ENDPOINTS |
| 59 | + }), |
| 60 | + }) |
| 61 | + |
| 62 | +export async function getTrustRelationships( |
| 63 | + store: any, |
| 64 | + data: Req['getTrustRelationships'], |
| 65 | + options?: Parameters< |
| 66 | + typeof trustRelationshipService.endpoints.getTrustRelationships.initiate |
| 67 | + >[1], |
| 68 | +) { |
| 69 | + return store.dispatch( |
| 70 | + trustRelationshipService.endpoints.getTrustRelationships.initiate( |
| 71 | + data, |
| 72 | + options, |
| 73 | + ), |
| 74 | + ) |
| 75 | +} |
| 76 | +// END OF FUNCTION_EXPORTS |
| 77 | + |
| 78 | +export const { |
| 79 | + useCreateTrustRelationshipMutation, |
| 80 | + useDeleteTrustRelationshipMutation, |
| 81 | + useGetTrustRelationshipsQuery, |
| 82 | + useUpdateTrustRelationshipMutation, |
| 83 | + // END OF EXPORTS |
| 84 | +} = trustRelationshipService |
| 85 | + |
| 86 | +/* Usage examples: |
| 87 | +const { data, isLoading } = useGetTrustRelationshipsQuery({ organisation_id: 2 }) //get hook |
| 88 | +const [createTrustRelationship, { isLoading, data, isSuccess }] = useCreateTrustRelationshipMutation() //create hook |
| 89 | +trustRelationshipService.endpoints.getTrustRelationships.select({organisation_id: 2})(store.getState()) //access data from any function |
| 90 | +*/ |
0 commit comments