15067: Updates properties form to handle vocabulary's new format (WIP)
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-field-common.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from 'react-redux';
6 import { WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps } from 'redux-form';
7 import { Vocabulary } from '~/models/vocabulary';
8 import { RootState } from '~/store/store';
9 import { getVocabulary } from '~/store/vocabulary/vocabulary-selectors';
10
11 export interface VocabularyProp {
12     vocabulary: Vocabulary;
13 }
14
15 export const mapStateToProps = (state: RootState): VocabularyProp => ({
16     vocabulary: getVocabulary(state.properties),
17 });
18
19 export const connectVocabulary = connect(mapStateToProps);
20
21 export const ITEMS_PLACEHOLDER: string[] = [];
22
23 export const hasError = ({ touched, invalid }: WrappedFieldMetaProps) =>
24     touched && invalid;
25
26 export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
27     hasError(meta)
28         ? meta.error
29         : '';
30
31 export const handleBlur = ({ onBlur, value }: WrappedFieldInputProps) =>
32     () =>
33         onBlur(value);
34
35 export const handleSelect = ({ onChange }: WrappedFieldInputProps) => {
36     return (item:PropFieldSuggestion) => {
37         onChange(item.id);
38     };
39 };
40
41 export const buildProps = ({ input, meta }: WrappedFieldProps) => {
42     return {
43         value: input.value,
44         onChange: input.onChange,
45         onBlur: handleBlur(input),
46         items: ITEMS_PLACEHOLDER,
47         onSelect: handleSelect(input),
48         renderSuggestion: (item:PropFieldSuggestion) => item.label,
49         error: hasError(meta),
50         helperText: getErrorMsg(meta),
51     };
52 };
53
54 export interface PropFieldSuggestion {
55     "id": string;
56     "label": string;
57 }