Skip to content

Commit

Permalink
feat(product): ensure that although we now fetch all products regardl…
Browse files Browse the repository at this point in the history
…ess of state, that inactive products cannot be selected
  • Loading branch information
dleard committed Apr 27, 2020
1 parent e459042 commit d43479d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/components/Forms/SearchDropdownWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ const SearchDropdownWidget: React.FunctionComponent<WidgetProps> = ({
placeholder,
value
}) => {
// Get indices of all non-active products (deprecated, archived)
const getAllIndexes = (array, value) => {
const indexArray = [];
array.forEach((item, index) => {
if (item !== value) indexArray.push(index);
});
return indexArray;
};

const inactiveIndexes = getAllIndexes((schema as any).state, 'active');

// Remove all enums & enumNames that correspond to a non-active product from the options
for (let i = inactiveIndexes.length - 1; i >= 0; i--) {
schema.enum.splice(inactiveIndexes[i], 1);
(schema as any).enumNames.splice(inactiveIndexes[i], 1);
}

const getOptions = useCallback(
() =>
schema.enum.map((e: string, index) => ({
Expand All @@ -21,7 +38,6 @@ const SearchDropdownWidget: React.FunctionComponent<WidgetProps> = ({

const getSelected = useCallback(() => {
if (value === null || value === undefined) return undefined;
if ((schema as any)?.state?.indexOf(value) !== 'active') return undefined;
const index = schema.enum.indexOf(value);
return [
{
Expand Down

0 comments on commit d43479d

Please sign in to comment.