-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add quantity value data object data types (#754)
- Loading branch information
1 parent
f3a5d75
commit 135e815
Showing
49 changed files
with
2,698 additions
and
1,866 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions
90
assets/js/src/core/modules/data-object/hooks/use-quantity-value-units.ts
This file contains 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,90 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import { | ||
useClassQuantityValueUnitConvertMutation, | ||
useClassQuantityValueUnitListQuery | ||
} from '@Pimcore/modules/class-definition/class-definition-slice.gen' | ||
import { type DefaultOptionType } from 'rc-select/lib/Select' | ||
import { useTranslation } from 'react-i18next' | ||
import _ from 'lodash' | ||
|
||
interface UseQuantityValueUnitsReturn { | ||
getSelectOptions: (validUnits?: string[]) => DefaultOptionType[] | ||
convertValue: (fromUnitId: string, toUnitId: string, value: number) => Promise<number | null> | ||
getAbbreviation: (unitId: string) => string | ||
} | ||
|
||
export const useQuantityValueUnits = (): UseQuantityValueUnitsReturn => { | ||
const { data: units } = useClassQuantityValueUnitListQuery() | ||
const [convertQuantityValue] = useClassQuantityValueUnitConvertMutation() | ||
const { t } = useTranslation() | ||
|
||
const getSelectOptions = (validUnits?: string[]): DefaultOptionType[] => { | ||
if (units?.items === undefined) { | ||
return [] | ||
} | ||
|
||
return units | ||
.items | ||
.filter(unit => validUnits === undefined || (unit.id !== null && validUnits.includes(unit.id))) | ||
.map(unit => ({ | ||
label: unit.abbreviation === null ? unit.id : t(unit.abbreviation), | ||
value: unit.id | ||
})) | ||
} | ||
|
||
const convertValue = async (fromUnitId: string, toUnitId: string, value: number): Promise<number | null> => { | ||
if (units?.items === undefined) { | ||
return null | ||
} | ||
const fromUnit = units.items.find(unit => unit.id === fromUnitId) | ||
const toUnit = units.items.find(unit => unit.id === toUnitId) | ||
if (fromUnit === undefined || toUnit === undefined) { | ||
return null | ||
} | ||
if (fromUnit.baseUnit === null || fromUnit.baseUnit !== toUnit.baseUnit) { | ||
return null | ||
} | ||
|
||
const { data } = await convertQuantityValue({ | ||
convertParameters: { | ||
fromUnitId, | ||
toUnitId, | ||
value | ||
} | ||
}).catch(() => { | ||
throw new Error('Error converting quantity value.') | ||
}) | ||
|
||
return data?.data ?? null | ||
} | ||
|
||
const getAbbreviation = (unitId: string): string => { | ||
if (units?.items === undefined) { | ||
return '' | ||
} | ||
|
||
const unit = units.items.find(unit => unit.id === unitId) | ||
if (typeof unit?.abbreviation === 'string' && !_.isEmpty(unit.abbreviation)) { | ||
return t(unit.abbreviation) | ||
} | ||
return unitId | ||
} | ||
|
||
return { | ||
getSelectOptions, | ||
convertValue, | ||
getAbbreviation | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ions/objects/data-related/components/input-quantity-value/input-quantity-value.styles.tsx
This file contains 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,38 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import { createStyles } from 'antd-style' | ||
|
||
export const useStyles = createStyles(({ css, token }) => { | ||
return { | ||
select: css` | ||
min-width: 100px; | ||
`, | ||
input: css` | ||
min-width: 80px; | ||
` | ||
} | ||
}) |
89 changes: 89 additions & 0 deletions
89
...intinitions/objects/data-related/components/input-quantity-value/input-quantity-value.tsx
This file contains 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,89 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import { Flex } from '@Pimcore/components/flex/flex' | ||
import { Input } from 'antd' | ||
import _ from 'lodash' | ||
import { useQuantityValueUnits } from '@Pimcore/modules/data-object/hooks/use-quantity-value-units' | ||
import { Select } from '@Pimcore/components/select/select' | ||
import { useTranslation } from 'react-i18next' | ||
import { toCssDimension } from '@Pimcore/utils/css' | ||
import { useStyles } from './input-quantity-value.styles' | ||
|
||
export interface InputQuantityValueProps { | ||
value?: InputQuantityValueValue | null | ||
className?: string | ||
onChange?: (value: InputQuantityValueValue | null) => void | ||
validUnits?: string[] | null | ||
width?: string | null | ||
} | ||
|
||
export interface InputQuantityValueValue { | ||
value: string | null | ||
unitId: string | null | ||
} | ||
|
||
export const InputQuantityValue = (props: InputQuantityValueProps): React.JSX.Element => { | ||
const [value, setValue] = useState<InputQuantityValueValue>(props.value ?? { value: null, unitId: null }) | ||
const { getSelectOptions } = useQuantityValueUnits() | ||
const { t } = useTranslation() | ||
const { styles } = useStyles() | ||
|
||
const onChangeInput = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
const newValue = e.target.value | ||
|
||
setValue({ | ||
...value, | ||
value: _.isEmpty(newValue) ? null : newValue | ||
}) | ||
} | ||
|
||
const onChangeSelect = (unitId?: string): void => { | ||
setValue({ | ||
...value, | ||
unitId: _.isEmpty(unitId) ? null : (unitId ?? null) | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
if (props.onChange !== undefined) { | ||
props.onChange(value.value === null && value.unitId === null ? null : value) | ||
} | ||
}, [value]) | ||
|
||
return ( | ||
<Flex | ||
align="center" | ||
className={ props.className } | ||
gap="small" | ||
> | ||
<Input | ||
className={ styles.input } | ||
onChange={ onChangeInput } | ||
style={ { maxWidth: toCssDimension(props.width, 150) } } | ||
value={ value?.value ?? undefined } | ||
/> | ||
<Select | ||
allowClear | ||
className={ styles.select } | ||
onChange={ onChangeSelect } | ||
optionFilterProp="label" | ||
options={ getSelectOptions(props.validUnits ?? undefined) } | ||
placeholder={ '(' + t('empty') + ')' } | ||
showSearch | ||
value={ value?.unitId ?? undefined } | ||
/> | ||
</Flex> | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
...ions/objects/data-related/components/quantity-value-range/quantity-value-range.styles.tsx
This file contains 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,38 @@ | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - Pimcore Open Core License (POCL) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL | ||
*/ | ||
|
||
import { createStyles } from 'antd-style' | ||
|
||
export const useStyles = createStyles(({ css, token }) => { | ||
return { | ||
select: css` | ||
min-width: 100px; | ||
`, | ||
input: css` | ||
min-width: 80px; | ||
` | ||
} | ||
}) |
Oops, something went wrong.