1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/component/feature/FeatureStrategy/FeatureStrategyEdit/FeatureStrategyEdit.tsx

166 lines
6.0 KiB
TypeScript
Raw Normal View History

feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
import React, { useEffect, useState } from 'react';
import { useFeature } from 'hooks/api/getters/useFeature/useFeature';
import { FeatureStrategyForm } from 'component/feature/FeatureStrategy/FeatureStrategyForm/FeatureStrategyForm';
2022-03-28 10:49:59 +02:00
import FormTemplate from 'component/common/FormTemplate/FormTemplate';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
import { useRequiredQueryParam } from 'hooks/useRequiredQueryParam';
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
2022-03-28 10:49:59 +02:00
import useFeatureStrategyApi from 'hooks/api/actions/useFeatureStrategyApi/useFeatureStrategyApi';
import { formatUnknownError } from 'utils/formatUnknownError';
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
import { useHistory } from 'react-router-dom';
2022-03-28 10:49:59 +02:00
import useToast from 'hooks/useToast';
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
import { IFeatureStrategy, IStrategyPayload } from 'interfaces/strategy';
import { UPDATE_FEATURE_STRATEGY } from 'component/providers/AccessProvider/permissions';
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
import { ISegment } from 'interfaces/segment';
import { useSegmentsApi } from 'hooks/api/actions/useSegmentsApi/useSegmentsApi';
import { useSegments } from 'hooks/api/getters/useSegments/useSegments';
import { formatStrategyName } from 'utils/strategyNames';
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
export const FeatureStrategyEdit = () => {
const projectId = useRequiredPathParam('projectId');
const featureId = useRequiredPathParam('featureId');
const environmentId = useRequiredQueryParam('environmentId');
const strategyId = useRequiredQueryParam('strategyId');
const [strategy, setStrategy] = useState<Partial<IFeatureStrategy>>({});
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
const [segments, setSegments] = useState<ISegment[]>([]);
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
const { updateStrategyOnFeature, loading } = useFeatureStrategyApi();
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
const { segments: savedStrategySegments } = useSegments(strategyId);
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
const { feature, refetchFeature } = useFeature(projectId, featureId);
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
const { setStrategySegments } = useSegmentsApi();
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
const { setToastData, setToastApiError } = useToast();
const { uiConfig } = useUiConfig();
const { unleashUrl } = uiConfig;
const { push } = useHistory();
useEffect(() => {
const savedStrategy = feature.environments
.flatMap(environment => environment.strategies)
.find(strategy => strategy.id === strategyId);
setStrategy(prev => ({ ...prev, ...savedStrategy }));
}, [strategyId, feature]);
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
useEffect(() => {
// Fill in the selected segments once they've been fetched.
savedStrategySegments && setSegments(savedStrategySegments);
}, [savedStrategySegments]);
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
const onSubmit = async () => {
try {
await updateStrategyOnFeature(
projectId,
featureId,
environmentId,
strategyId,
createStrategyPayload(strategy)
);
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
await setStrategySegments({
environmentId,
projectId,
strategyId,
segmentIds: segments.map(s => s.id),
});
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
setToastData({
title: 'Strategy updated',
type: 'success',
confetti: true,
});
refetchFeature();
push(formatFeaturePath(projectId, featureId));
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
};
// Wait until the strategy has loaded before showing the form.
if (!strategy.id) {
return null;
}
return (
<FormTemplate
modal
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
title={formatStrategyName(strategy.name ?? '')}
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
description={featureStrategyHelp}
documentationLink={featureStrategyDocsLink}
formatApiCode={() =>
formatUpdateStrategyApiCode(
projectId,
featureId,
environmentId,
strategy,
unleashUrl
)
}
>
<FeatureStrategyForm
feature={feature}
strategy={strategy}
setStrategy={setStrategy}
feat: add segments (#780) * feat: create segmentation structure and list * feat: remove unused deps and change route * feat: change header style and add renderNoSegments * fix: style table header * feat: create useSegments hook * feat: add segmentApi hook * feat: create segment * fix: errors * feat: add contextfields list * fix: remove user from create segment api * feat: add form structure * feat: add SegmentFormStepOne * fix: tests and routes * feat: add constraint view * feat: UI to match the sketch * feat: add constraint on context select * fix: duplication * fix adding constraints Co-authored-by: olav <mail@olav.io> * fix: input date not showing up in constraint view Co-authored-by: olav <mail@olav.io> * fix: minor bugs Co-authored-by: olav <mail@olav.io> * fix: create context modal in segment page Co-authored-by: olav <mail@olav.io> * fix: validate constraint before create segment Co-authored-by: olav <mail@olav.io> * feat: create useSegment hook Co-authored-by: olav <mail@olav.io> * feat: create edit component Co-authored-by: olav <mail@olav.io> * refactor: move constraint validation endpoint * refactor: add missing route snapshot * refactor: fix segment constraints unsaved/editing state * refactor: remove create segment from mobile header menu * refactor: update segments form description * refactor: extract SegmentFormStepList component * refactor: add an optional FormTemplate docs link label * refactor: fix update segment payload * feat: finish edit component Co-authored-by: olav <mail@olav.io> * refactor: move step list above segment form * fix: update PR based on feedback Co-authored-by: olav <mail@olav.io> * refactor: fix constraint validation endpoint path * refactor: improve constraint state field name * refactor: extract AutocompleteBox component * feat: add strategy segment selection * refactor: add strategy segment previews * refactor: fix double section separator line * feat: disable deleting a usable segment * refactor: warn about segments without constraints * refactor: update text in delete segment dialogue * refactur: improve arg names * refactor: improve index var name * refactor: clarify steps list logic * refactor: use a required prop for the segment name * refactor: use ConditionallyRender for segment deletion * refactor: fix segments refetch * refactor: improve CreateUnleashContext component names * refactor: adjust segment form styles * refactor: adjust text * refactor: fix info icon tooltip hover target * refactor: add missing aria attrs to preview button * refactor: add strat name to delete segment modal * refactor: fix segment chip text alighment * refactor: use bulk endpoint for strategy segments * refactor: fix imports after merge Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: olav <mail@olav.io>
2022-03-29 09:30:57 +02:00
segments={segments}
setSegments={setSegments}
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
environmentId={environmentId}
onSubmit={onSubmit}
loading={loading}
permission={UPDATE_FEATURE_STRATEGY}
/>
</FormTemplate>
);
};
export const createStrategyPayload = (
strategy: Partial<IFeatureStrategy>
): IStrategyPayload => {
return {
name: strategy.name,
constraints: strategy.constraints ?? [],
parameters: strategy.parameters ?? {},
};
};
export const formatFeaturePath = (
projectId: string,
featureId: string
): string => {
return `/projects/${projectId}/features/${featureId}`;
};
export const formatEditStrategyPath = (
projectId: string,
featureId: string,
environmentId: string,
strategyId: string
): string => {
const params = new URLSearchParams({ environmentId, strategyId });
return `/projects/${projectId}/features/${featureId}/strategies/edit?${params}`;
};
const formatUpdateStrategyApiCode = (
projectId: string,
featureId: string,
environmentId: string,
strategy: Partial<IFeatureStrategy>,
unleashUrl?: string
): string => {
if (!unleashUrl) {
return '';
}
const url = `${unleashUrl}/api/admin/projects/${projectId}/features/${featureId}/${environmentId}/development/strategies/${strategy.id}`;
const payload = JSON.stringify(strategy, undefined, 2);
return `curl --location --request PUT '${url}' \\
--header 'Authorization: INSERT_API_KEY' \\
--header 'Content-Type: application/json' \\
--data-raw '${payload}'`;
};
export const featureStrategyHelp = `
An activation strategy will only run when a feature toggle is enabled and provides a way to control who will get access to the feature.
If any of a feature toggle's activation strategies returns true, the user will get access.
`;
export const featureStrategyDocsLink =
'https://docs.getunleash.io/user_guide/activation_strategy';