910a095ad0b6f5d29a1b10cc1f9fdd4215f5b8ae
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-value-field.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps, Field, formValues } from 'redux-form';
7 import { compose } from 'redux';
8 import { Autocomplete } from '~/components/autocomplete/autocomplete';
9 import { Vocabulary } from '~/models/vocabulary';
10 import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
11 import { VocabularyProp, connectVocabulary, buildProps, PropFieldSuggestion } from '~/views-components/resource-properties-form/property-field-common';
12 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
13 import { COLLECTION_TAG_FORM_NAME } from '~/store/collection-panel/collection-panel-action';
14
15 interface PropertyKeyProp {
16     propertyKey: string;
17 }
18
19 export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
20
21 export const PROPERTY_VALUE_FIELD_NAME = 'value';
22 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
23
24 export const PropertyValueField = compose(
25     connectVocabulary,
26     formValues({ propertyKey: PROPERTY_KEY_FIELD_ID })
27 )(
28     (props: PropertyValueFieldProps) =>
29         <div>
30             <Field
31                 name={PROPERTY_VALUE_FIELD_NAME}
32                 component={PropertyValueInput}
33                 validate={getValidation(props)}
34                 {...props} />
35             <Field
36                 name={PROPERTY_VALUE_FIELD_ID}
37                 type='hidden'
38                 component='input' />
39         </div>
40 );
41
42 export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
43     <Autocomplete
44         label='Value'
45         suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
46         onSelect={handleSelect(props.input, props.meta)}
47         {...buildProps(props)}
48         onBlur={handleBlur(props.meta, props.input, vocabulary, propertyKey)}
49     />;
50
51 const getValidation = (props: PropertyValueFieldProps) =>
52     isStrictTag(props.propertyKey, props.vocabulary)
53         ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
54         : TAG_VALUE_VALIDATION;
55
56 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
57     (value: string) =>
58         getTagValues(propertyKey, vocabulary).find(v => v.label === value)
59             ? undefined
60             : 'Incorrect value';
61
62 const getSuggestions = (value: string, tagKey: string, vocabulary: Vocabulary) =>
63     getTagValues(tagKey, vocabulary).filter(v => v.label.toLowerCase().includes(value.toLowerCase()));
64
65 const isStrictTag = (tagKey: string, vocabulary: Vocabulary) => {
66     const tag = vocabulary.tags[tagKey];
67     return tag ? tag.strict : false;
68 };
69
70 const getTagValues = (tagKey: string, vocabulary: Vocabulary) => {
71     const tag = vocabulary.tags[tagKey];
72     const ret = tag && tag.values
73         ? Object.keys(tag.values).map(
74             tagValueID => tag.values![tagValueID].labels
75                 ? {"id": tagValueID, "label": tag.values![tagValueID].labels[0].label}
76                 : {"id": tagValueID, "label": tagValueID})
77         : [];
78     return ret;
79 };
80
81 const getTagValueID = (tagKeyID:string, tagValueLabel:string, vocabulary: Vocabulary) =>
82     (tagKeyID && vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].values)
83     ? Object.keys(vocabulary.tags[tagKeyID].values!).find(
84         k => vocabulary.tags[tagKeyID].values![k].labels.find(
85             l => l.label === tagValueLabel) !== undefined) || ''
86     : '';
87
88 // Attempts to match a manually typed value label with a value ID, when the user
89 // doesn't select the value from the suggestions list.
90 const handleBlur = (
91     { dispatch }: WrappedFieldMetaProps,
92     { onBlur, value }: WrappedFieldInputProps,
93     vocabulary: Vocabulary,
94     tagKeyID: string) =>
95     () => {
96         dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, getTagValueID(tagKeyID, value, vocabulary)));
97         onBlur(value);
98     };
99
100 // When selecting a property value, save its ID for later usage.
101 const handleSelect = (
102     { onChange }: WrappedFieldInputProps,
103     { dispatch }: WrappedFieldMetaProps) => {
104         return (item:PropFieldSuggestion) => {
105             onChange(item.label);
106             dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, item.id));
107     };
108 };