1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-04 11:17:02 +02:00
unleash.unleash/frontend/src/component/segments/SegmentFormStepTwo/SegmentFormStepTwo.tsx
olav 24c11332b5 chore: update MUI to v5 (#923)
* refactor: update mui packages

* refactor: run mui codemods

* refactor: format files after codemods

* refactor: fix broken types

* refactor: clean up theme

* refactor: fix broken tests

* refactor: replace @mui/styles with tss-react

* refactor: move breakpoints into classes for tss

* refactor: fix crash on missing feature description

* refactor: remove void classNames

* refactor: adjust styles to new defaults

* refactor: remove broken rollout slider e2e test

* refactor: fix duplicate e2e testid

* refactor: update makeStyles after rebase

* refactor: add missing snapshot after rebase

* refactor: fix TableCellSortable focus styles

* refactor: use 1.4 as the default line-height

* refactor: hide webkit search field icons

* refactor: fix select box label

* refactor: make AutocompleteBox smaller

* refactor: make heading smaller

* refactor: fix toast close icon color

* refactor: update snapshots

* refactor: add missing test event awaits

* refactor: fix default button line-height
2022-05-02 15:52:41 +02:00

164 lines
6.5 KiB
TypeScript

import React, { useRef, useState, useContext } from 'react';
import { Button } from '@mui/material';
import { Add } from '@mui/icons-material';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import PermissionButton from 'component/common/PermissionButton/PermissionButton';
import { SidebarModal } from 'component/common/SidebarModal/SidebarModal';
import { CreateUnleashContext } from 'component/context/CreateUnleashContext/CreateUnleashContext';
import {
CREATE_CONTEXT_FIELD,
CREATE_SEGMENT,
UPDATE_SEGMENT,
} from 'component/providers/AccessProvider/permissions';
import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext';
import { IConstraint } from 'interfaces/strategy';
import { useHistory } from 'react-router-dom';
import { useStyles } from 'component/segments/SegmentFormStepTwo/SegmentFormStepTwo.styles';
import {
ConstraintAccordionList,
IConstraintAccordionListRef,
} from 'component/common/ConstraintAccordion/ConstraintAccordionList/ConstraintAccordionList';
import { SegmentFormStep, SegmentFormMode } from '../SegmentForm/SegmentForm';
import {
AutocompleteBox,
IAutocompleteBoxOption,
} from 'component/common/AutocompleteBox/AutocompleteBox';
import {
SegmentDocsValuesWarning,
SegmentDocsValuesError,
} from 'component/segments/SegmentDocs/SegmentDocs';
import { useSegmentValuesCount } from 'component/segments/hooks/useSegmentValuesCount';
import { SEGMENT_VALUES_LIMIT } from 'utils/segmentLimits';
import AccessContext from 'contexts/AccessContext';
interface ISegmentFormPartTwoProps {
constraints: IConstraint[];
setConstraints: React.Dispatch<React.SetStateAction<IConstraint[]>>;
setCurrentStep: React.Dispatch<React.SetStateAction<SegmentFormStep>>;
mode: SegmentFormMode;
}
export const SegmentFormStepTwo: React.FC<ISegmentFormPartTwoProps> = ({
children,
constraints,
setConstraints,
setCurrentStep,
mode,
}) => {
const constraintsAccordionListRef = useRef<IConstraintAccordionListRef>();
const history = useHistory();
const { hasAccess } = useContext(AccessContext);
const { classes: styles } = useStyles();
const { context = [] } = useUnleashContext();
const [open, setOpen] = useState(false);
const segmentValuesCount = useSegmentValuesCount(constraints);
const overSegmentValuesLimit = segmentValuesCount > SEGMENT_VALUES_LIMIT;
const modePermission = mode === 'create' ? CREATE_SEGMENT : UPDATE_SEGMENT;
const autocompleteOptions = context.map(c => ({
value: c.name,
label: c.name,
}));
const onChange = ([option]: IAutocompleteBoxOption[]) => {
constraintsAccordionListRef.current?.addConstraint?.(option.value);
};
return (
<>
<div className={styles.form}>
<div className={styles.warning}>
<SegmentDocsValuesWarning />
</div>
<div>
<p className={styles.inputDescription}>
Select the context fields you want to include in the
segment.
</p>
<p className={styles.inputDescription}>
Use a predefined context field:
</p>
<AutocompleteBox
label="Select a context"
options={autocompleteOptions}
onChange={onChange}
/>
</div>
<div className={styles.addContextContainer}>
<p className={styles.inputDescription}>
...or add a new context field:
</p>
<SidebarModal
label="Create new context"
onClose={() => setOpen(false)}
open={open}
>
<CreateUnleashContext
onSubmit={() => setOpen(false)}
onCancel={() => setOpen(false)}
modal
/>
</SidebarModal>
<PermissionButton
permission={CREATE_CONTEXT_FIELD}
className={styles.addContextButton}
startIcon={<Add />}
onClick={() => setOpen(true)}
>
Add context field
</PermissionButton>
{overSegmentValuesLimit && (
<div className={styles.error}>
<SegmentDocsValuesError
values={segmentValuesCount}
/>
</div>
)}
</div>
<ConditionallyRender
condition={constraints.length === 0}
show={
<div className={styles.noConstraintText}>
<p className={styles.subtitle}>
Start adding context fields by selecting an
option from above, or you can create a new
context field and use it right away
</p>
</div>
}
/>
<div className={styles.constraintContainer}>
<ConstraintAccordionList
ref={constraintsAccordionListRef}
constraints={constraints}
setConstraints={
hasAccess(modePermission)
? setConstraints
: undefined
}
/>
</div>
</div>
<div className={styles.buttonContainer}>
<Button
type="button"
onClick={() => setCurrentStep(1)}
className={styles.backButton}
>
Back
</Button>
{children}
<Button
type="button"
className={styles.cancelButton}
onClick={() => {
history.push('/segments');
}}
>
Cancel
</Button>
</div>
</>
);
};