Skip to content

Commit

Permalink
Review corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgrundas committed Apr 19, 2021
1 parent 8426316 commit f2e1b80
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 233 deletions.
90 changes: 45 additions & 45 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -853,57 +853,17 @@
"context": "dialog content",
"string": "Are you sure you want to delete {attributeName}?"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_125992234": {
"context": "numeric attribute unit",
"string": "Unit"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_1390397602": {
"context": "numeric attribute unit system",
"string": "System"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_157021805": {
"context": "numeric attribute units of",
"string": "Units of"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_1576912740": {
"context": "area units type",
"string": "Area"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_1963548912": {
"context": "imperial unit system",
"string": "Imperial"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_2262354018": {
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_acreFt": {
"context": "acre-ft unit",
"string": "acre-ft"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_2947839983": {
"context": "metric unit system",
"string": "Metric"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_3595883383": {
"context": "volume units types",
"string": "Volume"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_3723486670": {
"context": "pint unit",
"string": "pint"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_3863978694": {
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_acreInch": {
"context": "acre-inch unit",
"string": "acre-inch"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_3889193046": {
"context": "distance units type",
"string": "Distance"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_746695941": {
"context": "weight units type",
"string": "Weight"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_935262293": {
"context": "check to require numeric attribute unit",
"string": "Select unit"
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_area": {
"context": "area units type",
"string": "Area"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_attributeLabel": {
"context": "attribute's label",
Expand All @@ -917,6 +877,10 @@
"context": "attribute slug input field helper text",
"string": "This is used internally. Make sure you don’t use spaces"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_distance": {
"context": "distance units type",
"string": "Distance"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_dropdown": {
"context": "product attribute type",
"string": "Dropdown"
Expand All @@ -929,10 +893,18 @@
"context": "file attribute type",
"string": "File"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_imperial": {
"context": "imperial unit system",
"string": "Imperial"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_inputType": {
"context": "attribute's editor component",
"string": "Catalog Input type for Store Owner"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_metric": {
"context": "metric unit system",
"string": "Metric"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_multiselect": {
"context": "product attribute type",
"string": "Multiple Select"
Expand All @@ -945,6 +917,10 @@
"context": "page attribute entity type",
"string": "Pages"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_pint": {
"context": "pint unit",
"string": "pint"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_product": {
"context": "product attribute entity type",
"string": "Products"
Expand All @@ -953,14 +929,38 @@
"context": "references attribute type",
"string": "References"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_selectUnit": {
"context": "check to require numeric attribute unit",
"string": "Select unit"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_text": {
"context": "text attribute type",
"string": "Text"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_unit": {
"context": "numeric attribute unit",
"string": "Unit"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_unitOf": {
"context": "numeric attribute units of",
"string": "Units of"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_unitSystem": {
"context": "numeric attribute unit system",
"string": "System"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_valueRequired": {
"context": "check to require attribute to have value",
"string": "Value Required"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_volume": {
"context": "volume units types",
"string": "Volume"
},
"src_dot_attributes_dot_components_dot_AttributeDetails_dot_weight": {
"context": "weight units type",
"string": "Weight"
},
"src_dot_attributes_dot_components_dot_AttributeListPage_dot_2417065806": {
"context": "tab name",
"string": "All Attributes"
Expand Down
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4215,10 +4215,10 @@ enum ProductFieldEnum {
PRODUCT_WEIGHT
COLLECTIONS
CHARGE_TAXES
PRODUCT_IMAGES
PRODUCT_MEDIA
VARIANT_SKU
VARIANT_WEIGHT
VARIANT_IMAGES
VARIANT_MEDIA
}

input ProductFilterInput {
Expand Down
101 changes: 67 additions & 34 deletions src/attributes/components/AttributeDetails/NumericUnits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { AttributePageFormData } from "@saleor/attributes/components/AttributePa
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
import SingleSelectField from "@saleor/components/SingleSelectField";
import { UseFormResult } from "@saleor/hooks/useForm";
import { commonMessages } from "@saleor/intl";
import { makeStyles } from "@saleor/theme";
import { MeasurementUnitsEnum } from "@saleor/types/globalTypes";
import React, { useEffect, useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import { useIntl } from "react-intl";

import * as M from "./messages";
import {
unitChoices,
getUnitChoices,
unitMapping,
UnitSystem,
unitSystemChoices,
Expand All @@ -19,11 +21,11 @@ import {
const useStyles = makeStyles(
theme => ({
unitsRow: {
columnGap: theme.spacing(2) + "px",
columnGap: theme.spacing(2),
display: "flex",
[theme.breakpoints.down("sm")]: {
flexFlow: "wrap",
rowGap: theme.spacing(3) + "px"
rowGap: theme.spacing(3)
}
},
hr: {
Expand All @@ -38,9 +40,9 @@ const useStyles = makeStyles(
);

interface UnitData {
unit: MeasurementUnitsEnum;
system: UnitSystem;
type: UnitType;
unit?: MeasurementUnitsEnum;
system?: UnitSystem;
type?: UnitType;
}

interface NumericUnitsProps
Expand All @@ -59,40 +61,63 @@ export const NumericUnits: React.FC<NumericUnitsProps> = ({
setError,
clearErrors
}) => {
const { formatMessage } = useIntl();
const classes = useStyles();
const [unitData, setUnitData] = useState<Partial<UnitData>>({
const [unitData, setUnitData] = useState<UnitData>({
unit: data.unit
});

const { unit, system, type } = unitData;
const errorProps = { error: !!errors.unit, hint: M.required };
const errorProps = {
error: !!errors.unit,
hint: formatMessage(commonMessages.requiredField)
};
const [typeChoices, systemChoices, unitChoices] = useMemo(
() => [
unitTypeChoices.map(choice => ({
...choice,
label: formatMessage(choice.label)
})),
unitSystemChoices.map(choice => ({
...choice,
label: formatMessage(choice.label)
})),
getUnitChoices(formatMessage)
],
[]
);

useEffect(() => set({ unit }), [unit]);

useEffect(() => {
if (data.unit) {
const initialData = { unit: data.unit } as UnitData;
const selectInitialUnitData = () => {
const initialData: UnitData = { unit: data.unit };

Object.entries(unitChoices).find(([system, types]) => {
const systemMatch = Object.entries(types).find(([type, units]) => {
const unitMatch = units.find(({ value }) => value === data.unit);
if (unitMatch) {
initialData.type = type as UnitType;
Object.entries(unitChoices).some(([system, types]) => {
const systemMatch = Object.entries(types).find(([type, units]) => {
const unitMatch = units.some(({ value }) => value === data.unit);
if (unitMatch) {
initialData.type = type as UnitType;
}
return unitMatch;
});
if (systemMatch) {
initialData.system = system as UnitSystem;
}
return unitMatch;
return systemMatch;
});
if (systemMatch) {
initialData.system = system as UnitSystem;
}
return systemMatch;
});
setUnitData(initialData);

return initialData;
};

setUnitData(selectInitialUnitData());
}
}, []);

useEffect(() => {
if (unit === undefined && !errors.unit) {
setError("unit", M.required);
setError("unit", formatMessage(commonMessages.requiredField));
}
if (errors.unit && (unit || unit === null)) {
clearErrors("unit");
Expand All @@ -105,7 +130,7 @@ export const NumericUnits: React.FC<NumericUnitsProps> = ({
<ControlledCheckbox
data-test="numeric-with-unit"
name="selectUnit"
label={M.selectUnit}
label={formatMessage(M.messages.selectUnit)}
checked={data.unit !== null}
onChange={({ target }) =>
setUnitData({ unit: target.value ? undefined : null })
Expand All @@ -117,30 +142,38 @@ export const NumericUnits: React.FC<NumericUnitsProps> = ({
<SingleSelectField
{...(!system && errorProps)}
testId="unit-system"
label={M.unitSystem}
choices={unitSystemChoices}
onChange={({ target }) => setUnitData({ system: target.value })}
label={formatMessage(M.messages.unitSystem)}
choices={systemChoices}
onChange={({ target }: React.ChangeEvent<HTMLSelectElement>) =>
setUnitData({ system: target.value as UnitSystem })
}
value={system}
disabled={disabled}
/>
<SingleSelectField
{...(system && !type && errorProps)}
testId="unit-of"
label={M.unitOf}
choices={unitTypeChoices}
onChange={({ target }) =>
setUnitData(({ system }) => ({ system, type: target.value }))
label={formatMessage(M.messages.unitOf)}
choices={typeChoices}
onChange={({ target }: React.ChangeEvent<HTMLSelectElement>) =>
setUnitData(({ system }) => ({
system,
type: target.value as UnitType
}))
}
disabled={!system || disabled}
value={type}
/>
<SingleSelectField
{...(type && !unit && errorProps)}
testId="unit"
label={M.unit}
label={formatMessage(M.messages.unit)}
choices={type ? unitChoices[system][type] : []}
onChange={({ target }) =>
setUnitData(p => ({ ...p, unit: target.value }))
onChange={({ target }: React.ChangeEvent<HTMLSelectElement>) =>
setUnitData(data => ({
...data,
unit: target.value as MeasurementUnitsEnum
}))
}
disabled={!type || disabled}
value={
Expand Down
Loading

0 comments on commit f2e1b80

Please sign in to comment.