Skip to content

Instantly share code, notes, and snippets.

@mrhat24
Created September 9, 2022 07:00
Show Gist options
  • Select an option

  • Save mrhat24/28497f591566b210258a312ca8901c1e to your computer and use it in GitHub Desktop.

Select an option

Save mrhat24/28497f591566b210258a312ca8901c1e to your computer and use it in GitHub Desktop.
import {
CustomDictionaryItemServiceGetAllApiArg,
CustomDictionaryServiceGetAllApiArg,
CustomFieldDefinitionServiceGetAllApiResponse,
DataSourceServiceGetAllApiResponse,
generatedApi,
RsmTierServiceGetAllApiResponse,
TagServiceGetAllApiArg,
} from './generatedApi';
import { ApiTags } from '../tags';
import { AppState } from '../../redux/store';
export const coreApi = generatedApi.enhanceEndpoints({
endpoints: {
relationNamingRulesServiceGetAll: {
providesTags: [ApiTags.RelationNamingRules]
},
relationNamingRulesServiceInsert: {
invalidatesTags: [ApiTags.RelationNamingRules],
},
relationNamingRulesServiceUpdate: {
invalidatesTags: [ApiTags.RelationNamingRules],
},
relationNamingRulesServiceSoftDelete: {
invalidatesTags: [ApiTags.RelationNamingRules],
},
assetFilterServiceFilter: {
providesTags: [ApiTags.Asset],
},
assetServiceGet: {
providesTags: [ApiTags.Asset],
},
assetServiceInsert: {
invalidatesTags: [ApiTags.Asset],
},
assetServiceDelete: {
invalidatesTags: [ApiTags.Asset],
},
assetServiceUpdate: {
invalidatesTags: [ApiTags.Asset],
},
dataSourceServiceGetAll: {
transformResponse: (response: DataSourceServiceGetAllApiResponse) => response.sort((a, b) => b.weight - a.weight),
providesTags: [ApiTags.DataSource],
},
dataSourceServiceInsert: {
invalidatesTags: [ApiTags.DataSource],
},
dataSourceServiceUpdate: {
invalidatesTags: [ApiTags.DataSource],
},
dataSourceServiceSoftDelete: {
invalidatesTags: [ApiTags.DataSource],
},
dataSourceServiceMoveToUpdate: {
invalidatesTags: [ApiTags.DataSource],
},
relationServiceDelete: {
invalidatesTags: [ApiTags.Asset],
},
relationServiceInsert: {
invalidatesTags: [ApiTags.Asset],
},
relationServiceFilter: {
providesTags: [ApiTags.Asset],
},
maintenancePlanServiceFilter: {
providesTags: [ApiTags.MaintenancePlan],
},
maintenancePlanServiceGetAll: {
providesTags: [ApiTags.MaintenancePlan],
},
maintenancePlanServiceInsert: {
invalidatesTags: [ApiTags.MaintenancePlan],
},
maintenancePlanServiceSoftDelete: {
invalidatesTags: [ApiTags.MaintenancePlan],
},
maintenancePlanServiceUpdate: {
invalidatesTags: [ApiTags.MaintenancePlan],
},
assetGroupServiceGetAll: {
providesTags: [ApiTags.AssetGroup],
},
assetGroupServiceInsert: {
invalidatesTags: [ApiTags.AssetGroup],
},
assetGroupServiceSoftDelete: {
invalidatesTags: [ApiTags.AssetGroup],
},
assetGroupServiceUpdate: {
invalidatesTags: [ApiTags.AssetGroup],
},
rsmTierServiceInsert: {
invalidatesTags: [ApiTags.RsmTier],
},
rsmTierServiceMoveToUpdate: {
invalidatesTags: [ApiTags.RsmTier],
},
rsmTierServiceSoftDelete: {
invalidatesTags: [ApiTags.RsmTier],
},
rsmTierServiceUpdate: {
invalidatesTags: [ApiTags.RsmTier],
},
rsmTierServiceGetAll: {
transformResponse: (response: RsmTierServiceGetAllApiResponse) => response.sort((a, b) => a.order - b.order),
providesTags: [ApiTags.RsmTier],
},
organizationalUnitServiceGetAll: {
providesTags: [ApiTags.OrganizationalUnit],
},
organizationalUnitServiceInsert: {
invalidatesTags: [ApiTags.OrganizationalUnit],
},
organizationalUnitServiceSoftDelete: {
invalidatesTags: [ApiTags.OrganizationalUnit],
},
organizationalUnitServiceUpdate: {
invalidatesTags: [ApiTags.OrganizationalUnit],
},
tagServiceInsert: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'tagServiceGetAll');
const { data } = await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData('tagServiceGetAll', query?.originalArgs as TagServiceGetAllApiArg, (draft) => {
draft.unshift(data);
})
)
);
},
},
tagServiceUpdate: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'tagServiceGetAll');
const { data } = await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData('tagServiceGetAll', query?.originalArgs as TagServiceGetAllApiArg, (draft) => {
const current = draft.find((draftItem) => draftItem.id === data.id);
if (current) {
draft.splice(draft.indexOf(current), 1, data);
}
})
)
);
},
},
tagServiceSoftDelete: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'tagServiceGetAll');
await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData('tagServiceGetAll', query?.originalArgs as TagServiceGetAllApiArg, (draft) => {
const current = draft.find((draftItem) => draftItem.id === queryArg.id);
if (current) {
if ((query?.originalArgs as TagServiceGetAllApiArg)?.withDeleted) {
return;
}
draft.splice(draft.indexOf(current), 1);
}
})
)
);
},
},
customDictionaryServiceInsert: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'customDictionaryServiceGetAll');
const { data } = await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryServiceGetAll',
query?.originalArgs as CustomDictionaryServiceGetAllApiArg,
(draft) => {
draft.unshift(data);
}
)
)
);
},
},
customDictionaryServiceUpdate: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'customDictionaryServiceGetAll');
const { data } = await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryServiceGetAll',
query?.originalArgs as CustomDictionaryServiceGetAllApiArg,
(draft) => {
const current = draft.find((draftItem) => draftItem.id === data.id);
if (current) {
draft.splice(draft.indexOf(current), 1, data);
}
}
)
)
);
},
},
customDictionaryServiceSoftDelete: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'customDictionaryServiceGetAll');
await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryServiceGetAll',
query?.originalArgs as CustomDictionaryServiceGetAllApiArg,
(draft) => {
const current = draft.find((draftItem) => draftItem.id === queryArg.id);
if (current) {
if ((query?.originalArgs as CustomDictionaryServiceGetAllApiArg)?.withDeleted) {
draft.splice(draft.indexOf(current), 1, { ...current, deleted: true });
return;
}
draft.splice(draft.indexOf(current), 1);
}
}
)
)
);
},
},
customDictionaryItemServiceInsert: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const { data } = await queryFulfilled;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter(
(query) =>
query?.endpointName === 'customDictionaryItemServiceGetAll' &&
(query?.originalArgs as CustomDictionaryItemServiceGetAllApiArg)?.customDictionaryId === data.dictionary_id
);
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryItemServiceGetAll',
query?.originalArgs as CustomDictionaryItemServiceGetAllApiArg,
(draft) => {
draft.unshift(data);
}
)
)
);
},
},
customDictionaryItemServiceUpdate: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const { data } = await queryFulfilled;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter(
(query) =>
query?.endpointName === 'customDictionaryItemServiceGetAll' &&
queryArg.customDictionaryItemUpdaterVm.dictionary_id === data.dictionary_id
);
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryItemServiceGetAll',
query?.originalArgs as CustomDictionaryItemServiceGetAllApiArg,
(draft) => {
const current = draft.find((draftItem) => draftItem.id === data.id);
if (current) {
draft.splice(draft.indexOf(current), 1, data);
}
}
)
)
);
},
},
customDictionaryItemServiceSoftDelete: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch, getState }) {
const state = getState() as AppState;
const queriesToUpdate = Object.keys(state.coreApi.queries)
.map((queryKey) => state.coreApi.queries[queryKey])
.filter((query) => query?.endpointName === 'customDictionaryItemServiceGetAll');
await queryFulfilled;
queriesToUpdate.forEach((query) =>
dispatch(
coreApi.util.updateQueryData(
'customDictionaryItemServiceGetAll',
query?.originalArgs as CustomDictionaryItemServiceGetAllApiArg,
(draft) => {
const current = draft.find((draftItem) => draftItem.id === queryArg.id);
if (current) {
if ((query?.originalArgs as CustomDictionaryItemServiceGetAllApiArg).withDeleted) {
draft.splice(draft.indexOf(current), 1, { ...current, deleted: true });
return;
}
draft.splice(draft.indexOf(current), 1);
}
}
)
)
);
},
},
customFieldDefinitionServiceGetAll: {
transformResponse: (response: CustomFieldDefinitionServiceGetAllApiResponse) =>
response.sort((a, b) => a.field_name.localeCompare(b.field_name)),
},
customFieldDefinitionServiceInsert: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch }) {
const { data } = await queryFulfilled;
dispatch(
coreApi.util.updateQueryData('customFieldDefinitionServiceGetAll', {}, (draft) => {
draft.unshift(data);
})
);
},
},
customFieldDefinitionServiceUpdate: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch }) {
const { data } = await queryFulfilled;
dispatch(
coreApi.util.updateQueryData('customFieldDefinitionServiceGetAll', {}, (draft) => {
const current = draft.find((draftItem) => draftItem.id === data.id);
if (current) {
draft.splice(draft.indexOf(current), 1, data);
}
})
);
},
},
customFieldDefinitionServiceSoftDelete: {
async onQueryStarted(queryArg, { queryFulfilled, dispatch }) {
await queryFulfilled;
dispatch(
coreApi.util.updateQueryData('customFieldDefinitionServiceGetAll', {}, (draft) => {
const current = draft.find((draftItem) => draftItem.id === queryArg.id);
if (current) {
draft.splice(draft.indexOf(current), 1);
}
})
);
},
},
},
});
export const {
useRelationServiceFilterQuery,
useSystemLogServiceFilterQuery,
useAssetFilterServiceFilterQuery,
useAssetServiceGetQuery,
useAssetTypeServiceGetAllQuery,
useAssetTypeServiceSoftDeleteMutation,
useAssetTypeServiceInsertMutation,
useAssetTypeServiceUpdateMutation,
useAssetServiceDeleteMutation,
useAssetServiceUpdateMutation,
useAssetServiceInsertMutation,
useOrganizationalUnitServiceGetAllQuery,
useDataSourceServiceGetAllQuery,
useDataSourceServiceInsertMutation,
useDataSourceServiceUpdateMutation,
useDataSourceServiceSoftDeleteMutation,
useDataSourceServiceMoveToUpdateMutation,
useOrganizationalUnitServiceInsertMutation,
useOrganizationalUnitServiceUpdateMutation,
useOrganizationalUnitServiceSoftDeleteMutation,
useAssetServiceGetVersionsQuery,
useAssetServiceComparedVersionsQuery,
useTagServiceGetAllQuery,
useTagServiceInsertMutation,
useTagServiceUpdateMutation,
useTagServiceSoftDeleteMutation,
useAssetGroupServiceGetAllQuery,
useAssetGroupServiceInsertMutation,
useAssetGroupServiceUpdateMutation,
useAssetGroupServiceSoftDeleteMutation,
useAssetFilterServiceFilterVersionQuery,
useRelationServiceInsertMutation,
useRelationServiceDeleteMutation,
useCustomDictionaryServiceGetAllQuery,
useCustomDictionaryServiceInsertMutation,
useCustomDictionaryServiceUpdateMutation,
useCustomDictionaryServiceSoftDeleteMutation,
useCustomDictionaryItemServiceGetAllQuery,
useCustomDictionaryItemServiceInsertMutation,
useCustomDictionaryItemServiceUpdateMutation,
useCustomDictionaryItemServiceSoftDeleteMutation,
useCustomFieldDefinitionServiceGetAllQuery,
useScmdbInstanceServiceGetAllQuery,
useCustomFieldDefinitionServiceInsertMutation,
useCustomFieldDefinitionServiceUpdateMutation,
useCustomFieldDefinitionServiceSoftDeleteMutation,
useSettingServiceGetSettingsQuery,
useRsmTierServiceGetAllQuery,
useRsmTierServiceInsertMutation,
useRsmTierServiceUpdateMutation,
useRsmTierServiceSoftDeleteMutation,
useRsmTierServiceMoveToUpdateMutation,
useRelationNamingRulesServiceGetAllQuery,
useRelationNamingRulesServiceInsertMutation,
useRelationNamingRulesServiceSoftDeleteMutation,
useRelationNamingRulesServiceUpdateMutation,
useAssetServiceGetDashboardQuery,
useAssetServiceGetTreeQuery,
useAssetServiceLoadFileToNifiMutation,
useMaintenancePlanServiceFilterQuery,
useMaintenancePlanServiceInsertMutation,
useMaintenancePlanServiceUpdateMutation,
useMaintenancePlanServiceSoftDeleteMutation,
} = coreApi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment