Probably should normalize the System query

This commit is contained in:
Luke Berndt
2023-03-22 09:52:48 -04:00
parent d474659c40
commit 24d2bc5706
7 changed files with 223 additions and 197 deletions

View File

@@ -90,7 +90,17 @@ export const apiSlice = createApi({
credentials: "include"
}),
invalidatesTags: (result, error, group) => [{ type: 'Group', id: group._id }],
async onQueryStarted(group, { dispatch, queryFulfilled }) {
try {
await queryFulfilled
const patchResult = dispatch(
apiSlice.util.updateQueryData('getGroups', group.shortName, (groups) => {
const groupIndex = groups.findIndex((obj => obj.groupId == group.groupId));
groups.splice(groupIndex,1);
})
)
} catch { }
},
}),
saveGroupOrder: builder.mutation({
query: ({shortName, order}) => ({
@@ -138,7 +148,8 @@ export const apiSlice = createApi({
try {
const { data: newSystem } = await queryFulfilled
const patchResult = dispatch(
apiSlice.util.updateQueryData('getSystems', system.shortName, (systems) => {
apiSlice.util.updateQueryData('getSystems', undefined, (systemsData) => {
const systems = systemsData.systems;
systems.push(newSystem);
})
)
@@ -156,7 +167,8 @@ export const apiSlice = createApi({
try {
const { data: updatedSystem } = await queryFulfilled
const patchResult = dispatch(
apiSlice.util.updateQueryData('getSystems', system.shortName, (systems) => {
apiSlice.util.updateQueryData('getSystems', undefined, (systemsData) => {
const systems = systemsData.systems;
const systemIndex = systems.findIndex((obj => obj.shortName == updatedSystem.shortName));
systems[systemIndex] = updatedSystem;
})
@@ -164,8 +176,27 @@ export const apiSlice = createApi({
} catch {}
},
}),
deleteSystem: builder.mutation({
query: (shortName) => ({
url: `/systems/${shortName}`,
method: 'DELETE',
credentials: "include"
}),
async onQueryStarted(shortName, { dispatch, queryFulfilled }) {
try {
await queryFulfilled
const patchResult = dispatch(
apiSlice.util.updateQueryData('getSystems', undefined, (systemsData) => {
const systems = systemsData.systems;
const systemIndex = systems.findIndex((obj => obj.shortName == shortName));
systems.splice(systemIndex, 1);
})
)
} catch {}
},
}),
})
})
// Export the auto-generated hook for the `getPosts` query endpoint
export const { useGetGroupsQuery, useGetSystemsQuery, useGetTalkgroupsQuery, useGetErrorsQuery, useDeleteGroupMutation, useCreateGroupMutation, useCreateSystemMutation, useUpdateSystemMutation, useSaveGroupOrderMutation, useUpdateGroupMutation, useImportTalkgroupsMutation } = apiSlice
export const { useGetGroupsQuery, useGetSystemsQuery, useGetTalkgroupsQuery, useGetErrorsQuery, useDeleteGroupMutation, useCreateGroupMutation, useCreateSystemMutation, useUpdateSystemMutation, useDeleteSystemMutation, useSaveGroupOrderMutation, useUpdateGroupMutation, useImportTalkgroupsMutation } = apiSlice