Merge pull request #769 from mirumee/fix/redundant-code-and-custom-attribute-value

Fix custom attribute value
This commit is contained in:
Dominik Żegleń 2020-10-16 10:35:04 +02:00 committed by GitHub
commit 2d59e4e0df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 78 deletions

View file

@ -196,7 +196,9 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({
displayValue={ displayValue={
attribute.data.values.find( attribute.data.values.find(
value => value.slug === attribute.value[0] value => value.slug === attribute.value[0]
)?.name || "" )?.name ||
attribute.value[0] ||
""
} }
emptyOption={!attribute.data.isRequired} emptyOption={!attribute.data.isRequired}
error={!!error} error={!!error}

View file

@ -19,7 +19,6 @@ import { sectionNames } from "@saleor/intl";
import { import {
getAttributeInputFromProductType, getAttributeInputFromProductType,
getChoices, getChoices,
ProductAttributeValueChoices,
ProductType ProductType
} from "@saleor/products/utils/data"; } from "@saleor/products/utils/data";
import { SearchCategories_search_edges_node } from "@saleor/searches/types/SearchCategories"; import { SearchCategories_search_edges_node } from "@saleor/searches/types/SearchCategories";
@ -194,10 +193,6 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
}; };
// Display values // Display values
const [selectedAttributes, setSelectedAttributes] = useStateFromProps<
ProductAttributeValueChoices[]
>([]);
const [selectedCategory, setSelectedCategory] = useStateFromProps( const [selectedCategory, setSelectedCategory] = useStateFromProps(
initial?.category || "" initial?.category || ""
); );
@ -245,15 +240,10 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
); );
const handleAttributeChange = createAttributeChangeHandler( const handleAttributeChange = createAttributeChangeHandler(
changeAttributeData, changeAttributeData,
setSelectedAttributes,
selectedAttributes,
attributes,
triggerChange triggerChange
); );
const handleAttributeMultiChange = createAttributeMultiChangeHandler( const handleAttributeMultiChange = createAttributeMultiChangeHandler(
changeAttributeData, changeAttributeData,
setSelectedAttributes,
selectedAttributes,
attributes, attributes,
triggerChange triggerChange
); );
@ -261,7 +251,6 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
const handleProductTypeSelect = createProductTypeSelectHandler( const handleProductTypeSelect = createProductTypeSelectHandler(
change, change,
setAttributeData, setAttributeData,
setSelectedAttributes,
setProductType, setProductType,
productTypeChoiceList productTypeChoiceList
); );

View file

@ -37,9 +37,7 @@ import {
getAttributeInputFromProduct, getAttributeInputFromProduct,
getChoices, getChoices,
getProductUpdatePageFormData, getProductUpdatePageFormData,
getSelectedAttributesFromProduct,
getStockInputFromProduct, getStockInputFromProduct,
ProductAttributeValueChoices,
ProductUpdatePageFormData ProductUpdatePageFormData
} from "../../utils/data"; } from "../../utils/data";
import { import {
@ -155,10 +153,6 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
remove: removeStock remove: removeStock
} = useFormset(stockInput); } = useFormset(stockInput);
const [selectedAttributes, setSelectedAttributes] = useStateFromProps<
ProductAttributeValueChoices[]
>(getSelectedAttributesFromProduct(product));
const [selectedCategory, setSelectedCategory] = useStateFromProps( const [selectedCategory, setSelectedCategory] = useStateFromProps(
maybe(() => product.category.name, "") maybe(() => product.category.name, "")
); );
@ -248,15 +242,10 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
); );
const handleAttributeChange = createAttributeChangeHandler( const handleAttributeChange = createAttributeChangeHandler(
changeAttributeData, changeAttributeData,
setSelectedAttributes,
selectedAttributes,
attributes,
triggerChange triggerChange
); );
const handleAttributeMultiChange = createAttributeMultiChangeHandler( const handleAttributeMultiChange = createAttributeMultiChangeHandler(
changeAttributeData, changeAttributeData,
setSelectedAttributes,
selectedAttributes,
attributes, attributes,
triggerChange triggerChange
); );

View file

@ -1,46 +1,16 @@
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
import { FormChange } from "@saleor/hooks/useForm"; import { FormChange } from "@saleor/hooks/useForm";
import { FormsetChange, FormsetData } from "@saleor/hooks/useFormset"; import { FormsetChange, FormsetData } from "@saleor/hooks/useFormset";
import { maybe } from "@saleor/misc";
import { toggle } from "@saleor/utils/lists"; import { toggle } from "@saleor/utils/lists";
import { ProductAttributeInputData } from "../components/ProductAttributes"; import { ProductAttributeInputData } from "../components/ProductAttributes";
import { import { getAttributeInputFromProductType, ProductType } from "./data";
getAttributeInputFromProductType,
ProductAttributeValueChoices,
ProductType
} from "./data";
export function createAttributeChangeHandler( export function createAttributeChangeHandler(
changeAttributeData: FormsetChange<string[]>, changeAttributeData: FormsetChange<string[]>,
setSelectedAttributes: (data: ProductAttributeValueChoices[]) => void,
selectedAttributes: ProductAttributeValueChoices[],
attributes: FormsetData<ProductAttributeInputData>,
triggerChange: () => void triggerChange: () => void
): FormsetChange { ): FormsetChange {
return (attributeId: string, value: string) => { return (attributeId: string, value: string) => {
const attributeValue = attributes
.find(attribute => attribute.id === attributeId)
.data.values.find(attributeValue => attributeValue.slug === value);
const valueChoice = {
label: maybe(() => attributeValue.name, value),
value
};
const itemIndex = selectedAttributes.findIndex(
item => item.id === attributeId
);
const attribute = selectedAttributes[itemIndex];
setSelectedAttributes([
...selectedAttributes.slice(0, itemIndex),
{
...attribute,
values: [valueChoice]
},
...selectedAttributes.slice(itemIndex + 1)
]);
triggerChange(); triggerChange();
changeAttributeData(attributeId, value === "" ? [] : [value]); changeAttributeData(attributeId, value === "" ? [] : [value]);
}; };
@ -48,8 +18,6 @@ export function createAttributeChangeHandler(
export function createAttributeMultiChangeHandler( export function createAttributeMultiChangeHandler(
changeAttributeData: FormsetChange<string[]>, changeAttributeData: FormsetChange<string[]>,
setSelectedAttributes: (data: ProductAttributeValueChoices[]) => void,
selectedAttributes: ProductAttributeValueChoices[],
attributes: FormsetData<ProductAttributeInputData>, attributes: FormsetData<ProductAttributeInputData>,
triggerChange: () => void triggerChange: () => void
): FormsetChange { ): FormsetChange {
@ -63,10 +31,13 @@ export function createAttributeMultiChangeHandler(
value value
}; };
const itemIndex = selectedAttributes.findIndex( const itemIndex = attributes.findIndex(item => item.id === attributeId);
item => item.id === attributeId const attributeValues: MultiAutocompleteChoiceType[] = attributes[
); itemIndex
const attributeValues = selectedAttributes[itemIndex].values; ].data.values.map(value => ({
label: value.name,
value: value.id
}));
const newAttributeValues = toggle( const newAttributeValues = toggle(
valueChoice, valueChoice,
@ -74,16 +45,6 @@ export function createAttributeMultiChangeHandler(
(a, b) => a.value === b.value (a, b) => a.value === b.value
); );
const newSelectedAttributes = [
...selectedAttributes.slice(0, itemIndex),
{
...selectedAttributes[itemIndex],
values: newAttributeValues
},
...selectedAttributes.slice(itemIndex + 1)
];
setSelectedAttributes(newSelectedAttributes);
triggerChange(); triggerChange();
changeAttributeData( changeAttributeData(
attributeId, attributeId,
@ -95,7 +56,6 @@ export function createAttributeMultiChangeHandler(
export function createProductTypeSelectHandler( export function createProductTypeSelectHandler(
change: FormChange, change: FormChange,
setAttributes: (data: FormsetData<ProductAttributeInputData>) => void, setAttributes: (data: FormsetData<ProductAttributeInputData>) => void,
setSelectedAttributes: (data: ProductAttributeValueChoices[]) => void,
setProductType: (productType: ProductType) => void, setProductType: (productType: ProductType) => void,
productTypeChoiceList: ProductType[] productTypeChoiceList: ProductType[]
): FormChange { ): FormChange {
@ -108,12 +68,6 @@ export function createProductTypeSelectHandler(
change(event); change(event);
setAttributes(getAttributeInputFromProductType(selectedProductType)); setAttributes(getAttributeInputFromProductType(selectedProductType));
setSelectedAttributes(
selectedProductType.productAttributes.map(attribute => ({
id: attribute.id,
values: []
}))
);
}; };
} }