-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCreateTdDialog.jsx
174 lines (151 loc) · 6.59 KB
/
CreateTdDialog.jsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/********************************************************************************
* Copyright (c) 2018 - 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
import React, { forwardRef, useImperativeHandle, useContext } from 'react';
import ReactDOM from "react-dom";
import { DialogTemplate } from "./DialogTemplate";
import ediTDorContext from '../../context/ediTDorContext';
import { ChevronDown } from 'react-feather';
export const CreateTdDialog = forwardRef((props, ref) => {
const context = useContext(ediTDorContext);
const [display, setDisplay] = React.useState(() => { return false });
const [type, setType] = React.useState("TD"); // either TD or TM
useImperativeHandle(ref, () => {
return {
openModal: () => open(),
close: () => close()
}
});
const open = () => {
setDisplay(true)
};
const close = () => {
setDisplay(false);
};
const changeType = (e) => {
setType(e.target.value);
}
const getType = () => {
return type;
}
const content = buildForm(changeType, getType);
if (display) {
return ReactDOM.createPortal(
<DialogTemplate
onCancel={close}
onSubmit={() => {
let td=createNewTD(type)
let linkedTd={}
linkedTd[td["title"]]=td
context.updateLinkedTd(undefined)
context.addLinkedTd(linkedTd)
context.updateShowConvertBtn(type === "TM");
context.updateIsThingModel(type === "TM")
context.updateOfflineTD(JSON.stringify(td, null, "\t"),"AppHeader");
close();
}}
children={content}
submitText={type === "TD" ? "Create TD" : "Create TM"}
title={"Create a New TD/TM"}
description={"To quickly create a basis for your new Thing Description/Thing Model just fill out this little template and we'll get you going."}
/>,
document.getElementById("modal-root"));
}
return null;
});
const buildForm = (changeType, getType) => {
return <>
<label htmlFor="type" className="text-sm text-gray-400 font-medium pl-2">Type:</label>
<div className="relative">
<select className="block appearance-none w-full bg-gray-600 border-2 border-gray-600 text-white py-3 px-4 pr-8 rounded leading-tight focus:border-blue-500 focus:outline-none" id="type" onChange={changeType} value={getType()}>
<option value="TD">Thing Description</option>
<option value="TM">Thing Model</option>
</select>
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
<ChevronDown color="#cacaca"></ChevronDown>
</div>
</div>
{formField("ID", "urn:thing-id", "thing-id", "url", "autoFocus")}
{formField("Title", "Thing Title", "thing-title", "text")}
{formField("Base", "http://www.example.com/thing-path", "thing-base", "url")}
<label htmlFor="thing-description" className="text-sm text-gray-400 font-medium pl-2">Description:</label>
<textarea id="thing-description" rows="5"
className="bg-gray-600
sm:text-sm
appearance-none
border-2 border-gray-600 rounded w-full
p-2
text-white
leading-tight
focus:outline-none
focus:border-blue-500"
placeholder="A short description about this new Thing..." />
<label htmlFor="thing-security" className="text-sm text-gray-400 font-medium pl-2">Security:</label>
<div className="relative">
<select className="block appearance-none w-full bg-gray-600 border-2 border-gray-600 text-white py-3 px-4 pr-8 rounded leading-tight focus:border-blue-500 focus:outline-none" id="thing-security">
<option>nosec</option>
<option>basic</option>
<option>digest</option>
<option>bearer</option>
<option>psk</option>
<option>oauth2</option>
<option>apikey</option>
</select>
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
<ChevronDown color="#cacaca"></ChevronDown>
</div>
</div>
</>;
}
const formField = (label, placeholder, id, type, autoFocus) => {
return <div key={id} className="py-1">
<label htmlFor={id} className="text-sm text-gray-400 font-medium pl-2">{label}:</label>
<input
name={id}
id={id}
className="border-gray-600 bg-gray-600 w-full p-2 sm:text-sm border-2 text-white rounded-md focus:outline-none focus:border-blue-500"
placeholder={placeholder}
type={type}
autoFocus={autoFocus === 'autoFocus'}
/>
</div>;
}
const createNewTD = (type) => {
let id = document.getElementById('thing-id').value;
let title = document.getElementById('thing-title').value;
let base = document.getElementById('thing-base').value;
let tdDescription = document.getElementById('thing-description').value;
let tdSecurity = document.getElementById('thing-security').value;
var thing = {};
thing["@context"] = "https://www.w3.org/2019/wot/td/v1";
thing["title"] = title !== "" ? title : "ediTDor Thing";
if (type === "TM") {
thing["@type"] = "tm:ThingModel";
}
if (id !== "") {
thing["id"] = id !== "" ? id : "urn:editdor-thing-id";
}
if (base !== "") {
thing["base"] = base !== "" ? base : "/";
}
if (tdDescription !== "") {
thing["description"] = tdDescription;
}
let securityDefinitions = {};
securityDefinitions[`${tdSecurity}_sc`] = { scheme: tdSecurity };
thing["securityDefinitions"] = securityDefinitions;
thing["security"] = `${tdSecurity}_sc`;
thing["properties"] = {};
thing["actions"] = {};
thing["events"] = {};
return thing;
}