Skip to content
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

feat(1314737): [Boost] Add Proportional Attribute Component to the Fi… #219

Open
wants to merge 2 commits into
base: 2.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/components/public/locales/en/boost.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"boost_zero": "boost",
"boost_one": "boost",
"boost_other": "boosts",
"preview": "Preview"
"preview": "Preview",
"boostConfig.sourceField": "Source field",
"boostConfig.sourceField.placeholder": "Select a source field",
"boostConfig.boostImpact": "Boost impact",
"boostConfig.scaleFactor": "Scale factor"
}
6 changes: 5 additions & 1 deletion packages/components/public/locales/fr/boost.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"boost_zero": "boost",
"boost_one": "boost",
"boost_other": "boosts",
"preview": "Prévisualiser"
"preview": "Prévisualiser",
"boostConfig.sourceField": "Source field",
"boostConfig.sourceField.placeholder": "Sélectionnez un source field",
"boostConfig.boostImpact": "Impacte du Boost",
"boostConfig.scaleFactor": "Facteur d'échelle"
}
4 changes: 1 addition & 3 deletions packages/components/src/components/atoms/form/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import RadioGroupWithoutError, {
} from './RadioGroupWithoutError'
import InputTextWithoutError from './InputTextWithoutError'

interface IRadioGroupErrorProps extends IFieldErrorProps, IRadioGroupProps {
required?: boolean
}
interface IRadioGroupErrorProps extends IFieldErrorProps, IRadioGroupProps {}

function RadioGroup(props: IRadioGroupErrorProps): JSX.Element {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import React, { ReactNode, SyntheticEvent } from 'react'
import {
FormControl,
FormControlLabel,
FormHelperText,
InputLabel,
Radio,
RadioGroupProps,
RadioGroup as RadioGrp,
} from '@mui/material'
import { IOption, IOptions } from '@elastic-suite/gally-admin-shared'
import IonIcon from '../IonIcon/IonIcon'
import InfoTooltip from '../../atoms/form/InfoTooltip'

export interface IRadioGroupProps extends Omit<RadioGroupProps, 'onChange'> {
options: IOptions<unknown>
onChange?: (value: string, event: SyntheticEvent) => void
error?: boolean
helperText?: ReactNode
helperIcon?: string
label?: string
infoTooltip?: string
required?: boolean
}

function RadioGroupWithoutError(props: IRadioGroupProps): JSX.Element {
Expand All @@ -24,17 +30,30 @@ function RadioGroupWithoutError(props: IRadioGroupProps): JSX.Element {
helperIcon,
options,
onChange,
infoTooltip,
label,
...radioGroupProps
} = props
const foundNameDefaultValue = options.find((element) => element.default)

return (
<div>
<FormControl variant="standard" margin="normal">
{Boolean(label || infoTooltip) && (
<InputLabel
shrink
style={{ position: 'relative' }}
required={radioGroupProps.required}
>
{label}
{infoTooltip ? <InfoTooltip title={infoTooltip} /> : null}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si tu veux éviter le ternaire tu peux faire comme ce que tu as fait à la ligne 41:

{Boolean(infoTooltip) && <InfoTooltip title={infoTooltip} />}

</InputLabel>
)}
<RadioGrp
{...radioGroupProps}
onChange={(event, value): void => {
onChange(value, event)
}}
//todo : la default value ne fonctionne pas et lors de la dernière demo on avait note des contraintes + un label pour le scale factor
defaultValue={
radioGroupProps.defaultChecked ? foundNameDefaultValue?.value : null
}
Expand Down Expand Up @@ -62,7 +81,7 @@ function RadioGroupWithoutError(props: IRadioGroupProps): JSX.Element {
{helperText}
</FormHelperText>
)}
</div>
</FormControl>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

exports[`RadioGroup sould match snapshot 1`] = `
<div>
<div>
<div
class="MuiFormControl-root MuiFormControl-marginNormal css-e5cteh-MuiFormControl-root"
>
<div
class="MuiFormGroup-root css-dmmspl-MuiFormGroup-root"
role="radiogroup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

exports[`RadioGroupWithoutError match snapshot Radio group defaultChecked true with default 1`] = `
<div>
<div>
<div
class="MuiFormControl-root MuiFormControl-marginNormal css-e5cteh-MuiFormControl-root"
>
<div
class="MuiFormGroup-root MuiFormGroup-row css-qfz70r-MuiFormGroup-root"
role="radiogroup"
Expand Down Expand Up @@ -111,7 +113,9 @@ exports[`RadioGroupWithoutError match snapshot Radio group defaultChecked true w

exports[`RadioGroupWithoutError match snapshot RadioGroupWithoutError defaultChecked False with default true 1`] = `
<div>
<div>
<div
class="MuiFormControl-root MuiFormControl-marginNormal css-e5cteh-MuiFormControl-root"
>
<div
class="MuiFormGroup-root MuiFormGroup-row css-qfz70r-MuiFormGroup-root"
role="radiogroup"
Expand Down Expand Up @@ -219,7 +223,9 @@ exports[`RadioGroupWithoutError match snapshot RadioGroupWithoutError defaultChe

exports[`RadioGroupWithoutError match snapshot RadioGroupWithoutError defaultChecked False without default 1`] = `
<div>
<div>
<div
class="MuiFormControl-root MuiFormControl-marginNormal css-e5cteh-MuiFormControl-root"
>
<div
class="MuiFormGroup-root MuiFormGroup-row css-qfz70r-MuiFormGroup-root"
role="radiogroup"
Expand Down Expand Up @@ -327,7 +333,9 @@ exports[`RadioGroupWithoutError match snapshot RadioGroupWithoutError defaultChe

exports[`RadioGroupWithoutError match snapshot RadioGroupWithoutError defaultChecked true without default 1`] = `
<div>
<div>
<div
class="MuiFormControl-root MuiFormControl-marginNormal css-e5cteh-MuiFormControl-root"
>
<div
class="MuiFormGroup-root MuiFormGroup-row css-qfz70r-MuiFormGroup-root"
role="radiogroup"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState } from 'react'
import { ComponentMeta, ComponentStory } from '@storybook/react'
import ProportionalToAttributesComponent, {
IProportionalToAttributesValue,
} from './ProportionalToAttributes'

export default {
title: 'Molecules/ProportionalToAttributesComponent',
component: ProportionalToAttributesComponent,
} as ComponentMeta<typeof ProportionalToAttributesComponent>

const Template: ComponentStory<typeof ProportionalToAttributesComponent> = (
args
) => {
const [value, setValue] = useState<IProportionalToAttributesValue>({
source_field_code: undefined,
boost_impact: undefined,
scale_factor: 1,
})
return (
<ProportionalToAttributesComponent
{...args}
value={value}
onChange={(value): void => setValue(value)}
/>
)
}

export const ProportionalToAttributes = Template.bind({})

ProportionalToAttributes.args = {
boostImpactOptions: [
{
label: 'Low',
value: 'Low',
},
{
label: 'Medium',
value: 'Medium',
},
{
label: 'High',
value: 'High',
},
],
sourceFields: [
{
label: 'test',
value: 'test',
},
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React from 'react'
import RadioGroup from '../../atoms/form/RadioGroup'
import InputText from '../../atoms/form/InputText'
import DropDown from '../../atoms/form/DropDown'
import { styled } from '@mui/material'
import { IOptions } from '@elastic-suite/gally-admin-shared'
import { useTranslation } from 'next-i18next'

const Container = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'column',
gap: theme.spacing(1),
}))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si possible mettre les styled components dans un fichier à part .styled.tsx (cf le composant CustomTable)
après on ne le fait peut-être pas systématiquement, je ne me souviens plus ce qu'on s'était dis


export interface IProportionalToAttributesValue {
source_field_code?: string
boost_impact?: string
scale_factor: number
}

export interface IProportionalToAttributesProps {
sourceFields: IOptions<string>
boostImpactOptions: IOptions<string>
value: IProportionalToAttributesValue
showError?: boolean
onChange: (value: IProportionalToAttributesValue) => void
}

function ProportionalToAttributes({
value,
sourceFields,
boostImpactOptions,
showError,
onChange,
}: IProportionalToAttributesProps): JSX.Element {
const {
source_field_code: sourceFieldCode,
boost_impact: boostImpact,
scale_factor: scaleFactor,
} = value
const { t } = useTranslation('boost')

function handleChange(
name: keyof IProportionalToAttributesValue,
newValue: string | number
): void {
onChange({
...value,
[name]: newValue,
})
}

return (
<Container>
<DropDown
options={sourceFields}
label={t('boostConfig.sourceField')}
value={sourceFieldCode}
onChange={(value: string): void =>
handleChange('source_field_code', value)
}
required
showError={showError}
sx={{ minWidth: '230px' }}
placeholder={t('boostConfig.sourceField.placeholder')}
/>

<RadioGroup
options={boostImpactOptions}
// todo: pourquoi set la defaultValue et non la value ?
defaultValue={boostImpact}
value={boostImpact}
onChange={(value: string): void => handleChange('boost_impact', value)}
label={t('boostConfig.boostImpact')}
required
showError={showError}
/>

<InputText
type="number"
label={t('boostConfig.scaleFactor')}
required
inputProps={{
min: 1,
}}
value={scaleFactor}
onChange={(value: number): void => handleChange('scale_factor', value)}
showError={showError}
/>
</Container>
)
}

export default ProportionalToAttributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useMemo } from 'react'
import { useApiList, useResource } from '../../../hooks'
import ProportionalToAttributes, {
IProportionalToAttributesProps,
} from './ProportionalToAttributes'
import { IOption } from '@elastic-suite/gally-admin-shared'

function ProportionalToAttributesManager(
props: Omit<
IProportionalToAttributesProps,
'sourceFields' | 'boostImpactOptions'
>
): JSX.Element {
const rowsPerPage = 200
const sourceFieldFixedFilters = useMemo(
() => ({
'metadata.entity': 'product',
}),
[]
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si ça ne change jamais, tu peux déclarer ces variables à l'extérieur du composant (et du coup sans le useMemo)


const boostImpactOptionResource = useResource('BoostImpactOption')
const boostAttributeValueFieldOptionsResource = useResource(
'BoostAttributeValueFieldOption'
)

const [boostImpactOptionsResponse] = useApiList<IOption<string>>(
boostImpactOptionResource,
false,
rowsPerPage,
undefined,
undefined,
false,
true
)

const [boostAttributeValueFieldOptionsResponse] = useApiList<IOption<string>>(
boostAttributeValueFieldOptionsResource,
false,
rowsPerPage,
sourceFieldFixedFilters,
undefined,
false,
true
)

if (
!boostImpactOptionsResponse?.data ||
!boostAttributeValueFieldOptionsResponse?.data
) {
return null
}

return (
<ProportionalToAttributes
sourceFields={
boostAttributeValueFieldOptionsResponse.data['hydra:member']
}
boostImpactOptions={boostImpactOptionsResponse.data['hydra:member']}
{...props}
/>
)
}

export default ProportionalToAttributesManager
Loading
Loading