-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): move Rollouts to use Redux storage (#2465)
* refactor(ui): move Rollouts to use Redux storage Refs: #2434 * fix the issue reported by eslint * it should be finally fixed --------- Co-authored-by: Mark Phelps <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f6f41a3
commit 035de76
Showing
7 changed files
with
269 additions
and
156 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { createApi } from '@reduxjs/toolkit/query/react'; | ||
import { IRollout, IRolloutBase, IRolloutList } from '~/types/Rollout'; | ||
|
||
import { baseQuery } from '~/utils/redux-rtk'; | ||
|
||
export const rolloutsApi = createApi({ | ||
reducerPath: 'rollouts', | ||
baseQuery, | ||
tagTypes: ['Rollout'], | ||
endpoints: (builder) => ({ | ||
// get list of rollouts | ||
listRollouts: builder.query< | ||
IRolloutList, | ||
{ namespaceKey: string; flagKey: string } | ||
>({ | ||
query: ({ namespaceKey, flagKey }) => | ||
`/namespaces/${namespaceKey}/flags/${flagKey}/rollouts`, | ||
providesTags: (_result, _error, { namespaceKey, flagKey }) => [ | ||
{ type: 'Rollout', id: namespaceKey + '/' + flagKey } | ||
] | ||
}), | ||
// delete the rollout | ||
deleteRollout: builder.mutation< | ||
void, | ||
{ namespaceKey: string; flagKey: string; rolloutId: string } | ||
>({ | ||
query({ namespaceKey, flagKey, rolloutId }) { | ||
return { | ||
url: `/namespaces/${namespaceKey}/flags/${flagKey}/rollouts/${rolloutId}`, | ||
method: 'DELETE' | ||
}; | ||
}, | ||
invalidatesTags: (_result, _error, { namespaceKey, flagKey }) => [ | ||
{ type: 'Rollout', id: namespaceKey + '/' + flagKey } | ||
] | ||
}), | ||
// create the rollout | ||
createRollout: builder.mutation< | ||
void, | ||
{ | ||
namespaceKey: string; | ||
flagKey: string; | ||
values: IRolloutBase; | ||
} | ||
>({ | ||
query({ namespaceKey, flagKey, values }) { | ||
return { | ||
url: `/namespaces/${namespaceKey}/flags/${flagKey}/rollouts`, | ||
method: 'POST', | ||
body: values | ||
}; | ||
}, | ||
invalidatesTags: (_result, _error, { namespaceKey, flagKey }) => [ | ||
{ type: 'Rollout', id: namespaceKey + '/' + flagKey } | ||
] | ||
}), | ||
// update the rollout | ||
updateRollout: builder.mutation< | ||
void, | ||
{ | ||
namespaceKey: string; | ||
flagKey: string; | ||
rolloutId: string; | ||
values: IRolloutBase; | ||
} | ||
>({ | ||
query({ namespaceKey, flagKey, rolloutId, values }) { | ||
return { | ||
url: `/namespaces/${namespaceKey}/flags/${flagKey}/rollouts/${rolloutId}`, | ||
method: 'PUT', | ||
body: values | ||
}; | ||
}, | ||
invalidatesTags: (_result, _error, { namespaceKey, flagKey }) => [ | ||
{ type: 'Rollout', id: namespaceKey + '/' + flagKey } | ||
] | ||
}), | ||
// reorder the rollouts | ||
orderRollouts: builder.mutation< | ||
IRollout, | ||
{ | ||
namespaceKey: string; | ||
flagKey: string; | ||
rolloutIds: string[]; | ||
} | ||
>({ | ||
query({ namespaceKey, flagKey, rolloutIds }) { | ||
return { | ||
url: `/namespaces/${namespaceKey}/flags/${flagKey}/rollouts/order`, | ||
method: 'PUT', | ||
body: { rolloutIds: rolloutIds } | ||
}; | ||
}, | ||
async onQueryStarted( | ||
{ namespaceKey, flagKey, rolloutIds }, | ||
{ dispatch, queryFulfilled } | ||
) { | ||
// this is manual optimistic cache update of the listRollouts | ||
// to set a desire order of rules of the listRollouts while server is updating the state. | ||
// If we don't do this we will have very strange UI state with rules in old order | ||
// until the result will be get from the server. It's very visible on slow connections. | ||
const patchResult = dispatch( | ||
rolloutsApi.util.updateQueryData( | ||
'listRollouts', | ||
{ namespaceKey, flagKey }, | ||
(draft: IRolloutList) => { | ||
const rules = draft.rules; | ||
const resortedRules = rules.sort((a, b) => { | ||
const ida = rolloutIds.indexOf(a.id); | ||
const idb = rolloutIds.indexOf(b.id); | ||
if (ida < idb) { | ||
return -1; | ||
} else if (ida > idb) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
return Object.assign(draft, { rules: resortedRules }); | ||
} | ||
) | ||
); | ||
try { | ||
await queryFulfilled; | ||
} catch { | ||
patchResult.undo(); | ||
} | ||
}, | ||
invalidatesTags: (_result, _error, { namespaceKey, flagKey }) => [ | ||
{ type: 'Rollout', id: namespaceKey + '/' + flagKey } | ||
] | ||
}) | ||
}) | ||
}); | ||
|
||
export const { | ||
useListRolloutsQuery, | ||
useCreateRolloutMutation, | ||
useUpdateRolloutMutation, | ||
useDeleteRolloutMutation, | ||
useOrderRolloutsMutation | ||
} = rolloutsApi; |
This file contains 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
Oops, something went wrong.