-
Notifications
You must be signed in to change notification settings - Fork 529
refactor(multivariate): move-mv-option-saves-to-rtk-query #7760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zaimwa9
wants to merge
6
commits into
feat/label-variant-key-ui
Choose a base branch
from
refactor/mv-options-rtk
base: feat/label-variant-key-ui
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9e6a24
refactor(multivariate): move-mv-option-requests-to-rtk-query
Zaimwa9 5864ed7
refactor(multivariate): consolidate-mv-option-saves-into-single-rtk-m…
Zaimwa9 4d5d93a
refactor(multivariate): drop-mv-save-invalidation-and-hoist-error-reset
Zaimwa9 5cfbbfd
refactor(multivariate): prune-unused-mv-option-endpoints
Zaimwa9 2b2f314
fix(multivariate): stable-variant-ordering-and-mv-option-e2e-tests
Zaimwa9 82f0ab2
test(multivariate): move-v2-mv-coverage-into-versioning-tests
Zaimwa9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,132 @@ | ||
| import { MultivariateOption, ProjectFlag, Res } from 'common/types/responses' | ||
| import { Req } from 'common/types/requests' | ||
| import { service } from 'common/service' | ||
|
|
||
| export const multivariateOptionService = service.injectEndpoints({ | ||
| endpoints: (builder) => ({ | ||
| createMultivariateOption: builder.mutation< | ||
| Res['multivariateOption'], | ||
| Req['createMultivariateOption'] | ||
| >({ | ||
| query: (query) => ({ | ||
| body: query.body, | ||
| method: 'POST', | ||
| url: `projects/${query.project_id}/features/${query.feature_id}/mv-options/`, | ||
| }), | ||
| }), | ||
| saveMultivariateOptions: builder.mutation< | ||
| Res['saveMultivariateOptions'], | ||
| Req['saveMultivariateOptions'] | ||
| >({ | ||
| // No invalidatesTags: every save chain already ends with a broad | ||
| // invalidateTags(['ProjectFlag', 'FeatureList']) once the downstream | ||
| // feature-state save completes — invalidating here too would refetch | ||
| // every subscribed query twice per save. | ||
| queryFn: async (args, _, _2, baseQuery) => { | ||
| const featureUrl = `projects/${args.project_id}/features/${args.feature_id}/` | ||
| // Diff against the server's current options rather than any client | ||
| // cache, so stale state can never turn an update into a duplicate | ||
| // create. | ||
| const flagRes = await baseQuery({ method: 'GET', url: featureUrl }) | ||
| if (flagRes.error) { | ||
| return { error: flagRes.error } | ||
| } | ||
| const serverOptions = | ||
| (flagRes.data as ProjectFlag)?.multivariate_options || [] | ||
| const errors: Record<number, any> = {} | ||
| // Results are written back by input index — downstream feature | ||
| // state saves map weights to option ids positionally. Requests run | ||
| // sequentially so newly created options get ascending ids in input | ||
| // order, which is the order the UI displays. | ||
| const ordered: MultivariateOption[] = [] | ||
| for (let i = 0; i < args.multivariate_options.length; i++) { | ||
| const v = args.multivariate_options[i] | ||
| let original | ||
| if (v.id) { | ||
| original = serverOptions.find((m) => m.id === v.id) | ||
| } else if (v.key) { | ||
| original = serverOptions.find((m) => !!m.key && m.key === v.key) | ||
| } | ||
| const body = { | ||
| ...v, | ||
| default_percentage_allocation: 0, | ||
| feature: args.feature_id, | ||
| } | ||
| const res = await baseQuery( | ||
| original | ||
| ? { | ||
| body, | ||
| method: 'PUT', | ||
| url: `${featureUrl}mv-options/${original.id}/`, | ||
| } | ||
| : { | ||
| body, | ||
| method: 'POST', | ||
| url: `${featureUrl}mv-options/`, | ||
| }, | ||
| ) | ||
| if (res.error) { | ||
| errors[i] = (res.error as { data?: any })?.data ?? null | ||
| } else { | ||
| ordered[i] = res.data as MultivariateOption | ||
| } | ||
| } | ||
| if (Object.keys(errors).length) { | ||
| return { data: { errors, multivariate_options: ordered } } | ||
| } | ||
| const deleted = serverOptions.filter( | ||
| (m) => !ordered.find((o) => o?.id === m.id), | ||
| ) | ||
| const deleteResults = await Promise.all( | ||
| deleted.map((m) => | ||
| baseQuery({ | ||
| method: 'DELETE', | ||
| url: `${featureUrl}mv-options/${m.id}/`, | ||
| }), | ||
| ), | ||
| ) | ||
| const failedDelete = deleteResults.find((r) => r.error) | ||
| if (failedDelete) { | ||
| return { error: failedDelete.error } | ||
| } | ||
| return { data: { errors: null, multivariate_options: ordered } } | ||
| }, | ||
| }), | ||
| // END OF ENDPOINTS | ||
| }), | ||
| }) | ||
|
|
||
| export async function createMultivariateOption( | ||
| store: any, | ||
| data: Req['createMultivariateOption'], | ||
| options?: Parameters< | ||
| typeof multivariateOptionService.endpoints.createMultivariateOption.initiate | ||
| >[1], | ||
| ) { | ||
| return store.dispatch( | ||
| multivariateOptionService.endpoints.createMultivariateOption.initiate( | ||
| data, | ||
| options, | ||
| ), | ||
| ) | ||
| } | ||
| export async function saveMultivariateOptions( | ||
| store: any, | ||
| data: Req['saveMultivariateOptions'], | ||
| options?: Parameters< | ||
| typeof multivariateOptionService.endpoints.saveMultivariateOptions.initiate | ||
| >[1], | ||
| ) { | ||
| return store.dispatch( | ||
| multivariateOptionService.endpoints.saveMultivariateOptions.initiate( | ||
| data, | ||
| options, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| export const { | ||
| useCreateMultivariateOptionMutation, | ||
| useSaveMultivariateOptionsMutation, | ||
| // END OF EXPORTS | ||
| } = multivariateOptionService | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential type mismatch issues (e.g., if
v.idis passed as a string from legacy form inputs or client state whilem.idis a number from the server), it is safer to compare them by normalizing both to strings or numbers. Strict equality===will fail if one is a string and the other is a number, which would result in creating duplicate options instead of updating the existing ones.