1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

fix: override built in stickiness options (#3563)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Created migration for updating context fields
Context fields with stickiness are now the source of truth for
stickiness options.

## About the changes
`userId` context field is updated to have stickiness on by default
`sessionId` context field is added with stickiness on by default
If `default` or `random` are not part of the context field - we add them
This will allow for the option to disable them, by creating a eg.
`random` context field with stickiness off


https://user-images.githubusercontent.com/104830839/233581147-9a514af4-1fdd-4c2e-b954-3cfa13b1a203.mov



<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes to #
[1-794](https://linear.app/unleash/issue/1-794/reach-out-to-yousician-for-early-beta-test)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2023-04-21 17:13:42 +03:00 committed by GitHub
parent 7ad4053c9b
commit f9b8abfe34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 20 deletions

View File

@ -1,12 +1,11 @@
import Select from 'component/common/select';
import { SelectChangeEvent, useTheme } from '@mui/material';
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
const builtInStickinessOptions = [
{ key: 'default', label: 'default' },
{ key: 'userId', label: 'userId' },
{ key: 'sessionId', label: 'sessionId' },
{ key: 'random', label: 'random' },
];
type OptionType = { key: string; label: string };
const DEFAULT_RANDOM_OPTION = 'random';
const DEFAULT_STICKINESS_OPTION = 'default';
interface IStickinessSelectProps {
label: string;
@ -25,20 +24,27 @@ export const StickinessSelect = ({
const { context } = useUnleashContext();
const theme = useTheme();
const resolveStickinessOptions = () =>
builtInStickinessOptions.concat(
context
.filter(contextDefinition => contextDefinition.stickiness)
.filter(
contextDefinition =>
!builtInStickinessOptions.find(
builtInStickinessOption =>
builtInStickinessOption.key ===
contextDefinition.name
)
)
.map(c => ({ key: c.name, label: c.name }))
);
const resolveStickinessOptions = () => {
const options = context
.filter(field => field.stickiness)
.map(c => ({ key: c.name, label: c.name })) as OptionType[];
if (
!options.find(option => option.key === 'default') &&
!context.find(field => field.name === DEFAULT_STICKINESS_OPTION)
) {
options.push({ key: 'default', label: 'default' });
}
if (
!options.find(option => option.key === 'random') &&
!context.find(field => field.name === DEFAULT_RANDOM_OPTION)
) {
options.push({ key: 'random', label: 'random' });
}
return options;
};
const stickinessOptions = resolveStickinessOptions();
return (

View File

@ -0,0 +1,28 @@
'use strict';
exports.up = function (db, callback) {
db.runSql(
`
INSERT INTO context_fields(name, description, sort_order, stickiness) VALUES('sessionId', 'Allows you to constrain on sessionId', 4, true);
UPDATE context_fields
SET stickiness = true
WHERE name LIKE 'userId' AND stickiness is null;
`,
callback,
);
};
exports.down = function (db, callback) {
db.runSql(
`
DELETE FROM context_fields
WHERE name LIKE 'sessionId';
UPDATE context_fields
SET stickiness = null
WHERE name LIKE 'userId';
`,
callback,
);
};