update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / composer / src / src / components / NavigateDescriptorErrors.jsx
1
2 /*
3  * 
4  *   Copyright 2017 RIFT.IO Inc
5  *
6  *   Licensed under the Apache License, Version 2.0 (the "License");
7  *   you may not use this file except in compliance with the License.
8  *   You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *   Unless required by applicable law or agreed to in writing, software
13  *   distributed under the License is distributed on an "AS IS" BASIS,
14  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *   See the License for the specific language governing permissions and
16  *   limitations under the License.
17  *
18  */
19
20 import React from 'react'
21 import _keys from 'lodash/keys'
22 import _get from 'lodash/get'
23 import _isObject from 'lodash/isObject'
24 import _isArray from 'lodash/isArray'
25 import Button from './Button'
26 import PropertyCrumb from './model/PropertyCrumb'
27 import PropertyNavigate from './model/PropertyNavigate'
28 import DescriptorEditorActions from '../actions/DescriptorEditorActions'
29 import Select from 'react-select';
30 import 'react-select/dist/react-select.css'
31 import '../styles/DetailsPanelErrors.scss'
32
33 export default function (props) {
34         const { container, idMaker, style } = props;
35         const errors = container.uiState.error;
36         const errorOptions = _keys(errors).reduce((outer, k) => {
37                 function pieceOfPath(obj, key) {
38                         const node = obj[key];
39                         if (_isArray(node)) {
40                                 const items = node.reduce((inner, v, i) => {
41                                         if (v) {
42                                                 const paths = _keys(v).reduce((a, k) => {
43                                                         const paths = pieceOfPath(v, k);
44                                                         return a.concat(paths);
45                                                 }, []);
46                                                 // inner = paths.map(p => [i].concat(p));
47                                                 inner = paths.map(p => [i].concat(p));
48                                         }
49                                         return inner;
50                                 }, []);
51                                 return items.map(i => [key].concat(i));
52                         } else if (_isObject(node)) {
53                                 return _keys(node).reduce((inner, k) => {
54                                         const paths = pieceOfPath(node, k);
55                                         return inner.concat(paths);
56                                 }, []);
57                         } else {
58                                 return [key];
59                         }
60                 }
61                 const paths = pieceOfPath(errors, k);
62                 return outer.concat(paths);
63         }, []).reduce((a, path) => {
64                 // only add if there is an error message
65                 _get(errors, path) && a.push({ value: path, label: _isArray(path) ? path.join(' . ') : path})
66                 return a;
67         }, []);
68         return errorOptions.length ?
69                 (<PropertyNavigate container={container} idMaker={idMaker} options={errorOptions} 
70                         style={style} placeholder="Select error to correct" />)
71                 : <div />;
72 }
73