import React, {FC} from "react";
import {CustomAttributeOption, CustomVariation} from "./AttributeFields";
import {MultiSelect} from "../fields/MultiSelect";
import {__, CouponsPlus} from "../../globals";
import {MultiSelectSearch, SelectOption} from "../fields/MultiSelectSearch";
import {Tabs} from "../fields/tabs";
import {Button} from "./Button";
export type CustomVariationFieldProps = {
customVariation: CustomVariation,
onAttributeChange: (attributeIndex: number, attribute: CustomAttributeOption) => void,
onNewAttribute: () => void,
onRemoveAttribute: (attributeIndex: number) => void,
canCreateNewAttribute: boolean
}
const attributeTypeOptions = [
{id: 'allowed', label: __('Eligible'), value: __('Eligible')},
{id: 'excluded', label: __('Not eligible'), value: __('Not eligible')},
{id: 'any', label: __('Any'), value: __('Any')},
];
const plusSign =
export const CustomVariationField: FC = ({
customVariation,
onAttributeChange,
onNewAttribute,
onRemoveAttribute,
canCreateNewAttribute
}) => {
return
{customVariation.map((attribute, index) => {
const attributeOptions = CouponsPlus.attributes.map(option => ({...option, label: option.name, value: option.id})).filter(({id}) => {
return true
})
const attributeMeta = CouponsPlus.attributes.find(attr => attr.id === attribute.attribute.id);
const attributeValueOptions = attributeMeta?.values?.map?.(value => ({
...value,
value: value.id,
label: value.name,
} as SelectOption));
const isLastAttribute = index === customVariation.length - 1;
return <>
{
onAttributeChange(index, {
...attribute,
attribute: {
...attribute.attribute,
id
}
});
}}
/>
{
onAttributeChange(index, {
...attribute,
type: id as CustomAttributeOption['type']
});
}}
selectedTabId={attribute.type}
/>
{attribute.type === 'any' &&
{__('May have any *, but it needs to have a *.').replaceAll('*', attributeMeta?.name)}
}
{attribute.type !== 'any' &&
{
onAttributeChange(index, {
...attribute,
attribute: {
...attribute.attribute,
values: [
...attribute.attribute.values,
id
]
}
})
}}
onRemovedValue={({id}) => {
onAttributeChange(index, {
...attribute,
attribute: {
...attribute.attribute,
values: attribute.attribute.values.filter(valueId => valueId !== id)
}
})
}}
stateFirst
SelectStateProps={{
showDefaultEmptyState: false
}}
SelectSearchProps={{
size: 'small',
openMenuOnClick: true,
showMode: 'always'//'focus-only'
}}
/>}
{!isLastAttribute && plusSign}
>
})}
{__('The item (product) may have other attributes. This variation only ensures the attributes above match.')}
{plusSign}
{canCreateNewAttribute &&
}
}