1+ import AdminTagPage , { AdminTagBC , AdminTagMenu } from "./AdminTagPage" ;
2+ import { GetAdminToken , getData , handleErrors , postData } from "./api" ;
3+ import { AdminBC , AdminMenu , AdminPage , RedirectToLogin } from "./AdminPage" ;
4+ import {
5+ BulmaControl ,
6+ Section ,
7+ BulmaLabel ,
8+ BulmaInput ,
9+ BulmaCheckbox ,
10+ BulmaRadio ,
11+ BulmaField ,
12+ BulmaSubmit , ErrorMessage
13+ } from "./BasePage" ;
14+ import React from "react" ;
15+ import { Redirect , withRouter } from "react-router-dom" ;
16+ import { AdminTagsBC } from "./AdminTagsListPage" ;
17+
18+
19+ class AdminTagsAddPage extends React . Component {
20+ constructor ( props ) {
21+ super ( props ) ;
22+
23+ GetAdminToken . call ( this ) ;
24+
25+ this . state = { serial : null , secretkey : null , description : null , fwversion : null , hwversion : null } ;
26+
27+ this . handleSubmit = this . handleSubmit . bind ( this ) ;
28+ this . handleChange = this . handleChange . bind ( this ) ;
29+ }
30+
31+ handleSubmit ( event ) {
32+ const admintoken = this . admintoken ;
33+ const bearertoken = `Bearer ${ admintoken } ` ;
34+ const extraheaders = { 'Authorization' : bearertoken } ;
35+ var payload = { } ;
36+
37+ if ( this . state . serial ) {
38+ payload . serial = this . state . serial ;
39+ }
40+
41+ if ( this . state . secretkey ) {
42+ payload . secretkey = this . state . secretkey ;
43+ }
44+
45+ if ( this . state . description ) {
46+ payload . description = this . state . description ;
47+ }
48+
49+ if ( this . state . fwversion ) {
50+ payload . fwversion = this . state . fwversion ;
51+ }
52+
53+ if ( this . state . hwversion ) {
54+ payload . hwversion = this . state . hwversion ;
55+ }
56+
57+ if ( event ) {
58+ event . preventDefault ( ) ;
59+ }
60+
61+ postData ( process . env . REACT_APP_WSB_ORIGIN + '/api/admin/tags' ,
62+ payload ,
63+ extraheaders )
64+ . then ( handleErrors )
65+ . then ( response => response . json ( ) )
66+ . then ( json => {
67+ } ,
68+ ( error ) => {
69+ if ( error ) {
70+ this . setState ( { error : error } ) ;
71+ }
72+ } ) ;
73+ }
74+
75+ handleChange ( event ) {
76+ this . setState ( { [ event . target . id ] : event . target . value } ) ;
77+ }
78+
79+ render ( ) {
80+ const activetab = 'Add' ;
81+ const error = this . state . error ;
82+ if ( error ) {
83+ if ( error . code === 401 ) {
84+ return < RedirectToLogin error = { error } />
85+ }
86+ }
87+ return (
88+ < AdminPage bc = { < AdminTagsAddBC /> } menu = { < AdminMenu activetab = { activetab } /> } >
89+ < ErrorMessage error = { error } handleDismiss = { this . handleDismiss } />
90+ < Section >
91+ < form onSubmit = { this . handleSubmit } autoComplete = "off" >
92+ < BulmaField >
93+ < BulmaControl >
94+ < BulmaLabel > Serial</ BulmaLabel >
95+ < BulmaInput id = "serial" type = "text" value = { this . state . serial || "" } changeHandler = { this . handleChange } maxLength = { 8 } />
96+ </ BulmaControl >
97+ </ BulmaField >
98+ < BulmaField >
99+ < BulmaControl >
100+ < BulmaLabel > Secret Key</ BulmaLabel >
101+ < BulmaInput id = "secretkey" type = "text" value = { this . state . secretkey || "" } changeHandler = { this . handleChange } maxLength = { 16 } />
102+ </ BulmaControl >
103+ </ BulmaField >
104+ < BulmaField >
105+ < BulmaControl >
106+ < BulmaLabel > Firmware Version</ BulmaLabel >
107+ < BulmaInput id = "fwversion" type = "text" value = { this . state . fwversion || "" } changeHandler = { this . handleChange } />
108+ </ BulmaControl >
109+ </ BulmaField >
110+ < BulmaField >
111+ < BulmaControl >
112+ < BulmaLabel > Hardware Version</ BulmaLabel >
113+ < BulmaInput id = "hwversion" type = "text" value = { this . state . hwversion || "" } changeHandler = { this . handleChange } />
114+ </ BulmaControl >
115+ </ BulmaField >
116+ < BulmaField >
117+ < BulmaControl >
118+ < BulmaLabel > Description</ BulmaLabel >
119+ < BulmaInput id = "description" type = "text" value = { this . state . description || "" } changeHandler = { this . handleChange } />
120+ </ BulmaControl >
121+ </ BulmaField >
122+ < BulmaSubmit />
123+ { /* https://jsfiddle.net/ndebellas/y4dLcqkx/ */ }
124+ </ form >
125+ </ Section >
126+ </ AdminPage > ) ;
127+
128+ }
129+ }
130+
131+ function AdminTagsAddBC ( props ) {
132+ return (
133+ < AdminBC >
134+ < li > < a href = "/admin/tags" aria-current = "page" > Tags</ a > </ li >
135+ < li className = "is-active" > < a href = "#" aria-current = "page" > Add</ a > </ li >
136+ </ AdminBC >
137+ ) ;
138+ }
139+
140+
141+ export default withRouter ( AdminTagsAddPage ) ;
0 commit comments