Extract PROPERTY_VALUE_FIELD_NAME
[arvados.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 { WrappedFieldProps, Field, formValues } from 'redux-form';
7 import { connect } from 'react-redux';
8 import { identity } from 'lodash';
9 import { compose } from 'redux';
10 import { RootState } from '~/store/store';
11 import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors';
12 import { Autocomplete } from '~/components/autocomplete/autocomplete';
13 import { Vocabulary } from '~/models/vocabulary';
14 import { require } from '~/validators/require';
15 import { PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field';
16
17 interface VocabularyProp {
18     vocabulary: Vocabulary;
19 }
20
21 interface PropertyKeyProp {
22     propertyKey: string;
23 }
24
25 type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
26
27 const mapStateToProps = (state: RootState): VocabularyProp => ({
28     vocabulary: getVocabulary(state.properties),
29 });
30
31 export const PROPERTY_VALUE_FIELD_NAME = 'value';
32
33 export const PropertyValueField = compose(
34     connect(mapStateToProps),
35     formValues({ propertyKey: PROPERTY_KEY_FIELD_NAME })
36 )(
37     (props: PropertyValueFieldProps) =>
38         <Field
39             name={PROPERTY_VALUE_FIELD_NAME}
40             component={PropertyValueInput}
41             validate={getValidation(props)}
42             {...props} />);
43
44 const PropertyValueInput = ({ input, meta, vocabulary, propertyKey }: WrappedFieldProps & PropertyValueFieldProps) =>
45     <Autocomplete
46         value={input.value}
47         onChange={input.onChange}
48         label='Value'
49         suggestions={getSuggestions(input.value, propertyKey, vocabulary)}
50         items={ITEMS_PLACEHOLDER}
51         onSelect={input.onChange}
52         renderSuggestion={identity}
53         error={meta.invalid}
54         helperText={meta.error}
55     />;
56
57 const getValidation = (props: PropertyValueFieldProps) =>
58     isStrictTag(props.propertyKey, props.vocabulary)
59         ? [require, matchTagValues(props)]
60         : [require];
61
62 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
63     (value: string) =>
64         getTagValues(propertyKey, vocabulary).find(v => v.includes(value))
65             ? undefined
66             : 'Incorrect value';
67
68 const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) =>
69     getTagValues(tagName, vocabulary).filter(v => v.includes(value) && v !== value);
70
71 const isStrictTag = (tagName: string, vocabulary: Vocabulary) => {
72     const tag = vocabulary.tags[tagName];
73     return tag ? tag.strict : false;
74 };
75
76 const getTagValues = (tagName: string, vocabulary: Vocabulary) => {
77     const tag = vocabulary.tags[tagName];
78     return tag ? tag.values : [];
79 };
80
81 const ITEMS_PLACEHOLDER: string[] = [];