-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoomSelectorPopup.tsx
More file actions
120 lines (111 loc) · 4.47 KB
/
RoomSelectorPopup.tsx
File metadata and controls
120 lines (111 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*
* * Copyright (c) 2026 Board of Regents of the University of Wisconsin System
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
import * as React from 'react';
import { FC, useEffect, useState } from 'react';
import '../../cageui.scss';
import Select from 'react-select';
import { SelectRowsOptions } from '@labkey/api/dist/labkey/query/SelectRows';
import { Filter } from '@labkey/api';
import { labkeyActionSelectWithPromise } from '../../api/labkeyActions';
import { Room } from '../../types/typings';
import { Option } from '@labkey/components';
interface RoomSelectorPopup {
onConfirm: () => void;
onCancel: () => void;
setRoom: React.Dispatch<React.SetStateAction<Room>>;
template: boolean;
templateLoad?: boolean;
templateRename?: React.Dispatch<React.SetStateAction<string>>;
}
// For saving and loading in the layout editor, this is a room selector component
export const RoomSelectorPopup: FC<RoomSelectorPopup> = (props) => {
const {onConfirm, onCancel, setRoom, template, templateLoad, templateRename} = props;
const [selectedRoom, setSelectedRoom] = useState<string>(null);
const [options, setOptions] = useState<Option<number>[]>(null);
const [templateName, setTemplateName] = useState<string>('');
// Fetch room, if template only fetch template rooms, otherwise fill options with {label: row.room, value: row.rowid}
useEffect(() => {
const roomsConfig: SelectRowsOptions = {
schemaName: 'ehr_lookups',
queryName: 'rooms',
columns: ['room', 'rowid'],
filterArray: template ? [Filter.create('room', 'template', Filter.Types.CONTAINS)] : [Filter.create('room', 'template', Filter.Types.DOES_NOT_CONTAIN)]
};
labkeyActionSelectWithPromise(roomsConfig).then(result => {
if (result.rows.length !== 0) {
const rowOptions: Option<number>[] = [];
result.rows.forEach(row => {
rowOptions.push({label: row.room, value: row.rowid});
});
setOptions(rowOptions);
}
}).catch(err => {
console.log('Error fetching prev room', err);
});
}, []);
// Determines and updates if a template was renamed then saves room/template
const handleSaveRoom = () => {
if (selectedRoom === null) {
onCancel();
return;
}
if (templateName.length > 0) {
const newTemplateName = "template-" + templateName;
// if template, save old template name for later
setRoom(prevState => ({
...prevState,
name: newTemplateName
}));
templateRename(selectedRoom);
} else {
setRoom(prevState => ({
...prevState,
name: selectedRoom
}));
}
onConfirm();
};
return (
<div className="popup-overlay">
<div className="popup">
<div className={'popup-row'}>
<Select
options={options}
placeholder={'Select a room'}
onChange={(option) => setSelectedRoom(option.label)}
/>
</div>
{(template && !templateLoad) &&
<div className={'popup-row'}>
<label>
Rename template?
</label>
<input
value={templateName}
onChange={(e) => setTemplateName(e.target.value)}
/>
</div>
}
<div className="popup-buttons">
<button onClick={handleSaveRoom}>Confirm</button>
<button onClick={onCancel}>Cancel</button>
</div>
</div>
</div>
);
};