-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
212 lines (163 loc) · 5.88 KB
/
index.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { server } from './utils.js'
// min date fixer for calender
var date_pick = document.getElementById("datePickerId")
if (date_pick)
{
datePickerId.min = new Date().toISOString().split("T")[0];
document.getElementById("datePickerId").valueAsDate = new Date()
}
// function display missing fields
function displayStatus2()
{
var from = document.getElementById("From").value;
var to = document.getElementById("To").value;
if (from == "" || to == "")
{
var status2 = document.getElementById('status2')
if (status2) {
const displayMessage = document.getElementById('status2')
displayMessage.parentNode.removeChild(displayMessage)
}
var status2 = "*Missing Fields"
const newNode = document.createElement('p')
newNode.id = 'status2'
newNode.classList.add('shake')
const displayText = document.createTextNode(status2)
newNode.appendChild(displayText)
const buttons = document.getElementById('search')
const parent = buttons.parentNode
parent.insertBefore(newNode, buttons)
return false
}
var status2 = document.getElementById('status2')
if (status2) {
const displayMessage = document.getElementById('status2')
displayMessage.parentNode.removeChild(displayMessage)
}
return true
}
// form to json serialize
function json_serialize()
{
let val = displayStatus2();
if (val)
{
let form_result = {}
form_result.trainfrom = document.getElementById("From").value;
form_result.trainto = document.getElementById("To").value;
form_result.type = document.getElementById("TicketType").value;
form_result.class = document.getElementById("TicketClass").value;
form_result.date = document.getElementById("datePickerId").value;
if (document.getElementById("AvailableTrain").checked)
{
form_result.trainavailable = document.getElementById("AvailableTrain").value;
}
else
{
form_result.AvailableTrain = "no";
}
// console.log(form_result);
let jsonStringObj = JSON.stringify(form_result);
// console.log(jsonStringObj);
// let url = "http://192.168.7.203:5000/"
const token = JSON.parse(localStorage.getItem('response')).token
fetch(server+'/user/home', {
method: 'POST', // or 'PUT'
headers: {
'Authorization':'Bearer '+token,
'Accept': 'application/json,text/plain,*/*',
'Content-Type': 'application/json',
},
body: jsonStringObj
})
.then((response) => response.json())
.then((data) => {
console.log(data)
if(data)
{
if (data.status) {
alert(data.status)
} else {
localStorage.setItem("trainDetails", JSON.stringify(data))
window.location.href = 'trains.html'
}
} else {
alert("something went wrong!")
}
})
}
}
// function showdata()
// {
// let data = localStorage.getItem("response")
// const newNode = createElement("p")
// newNode.id = "data"
// const displayText = document.createTextNode(data)
// }
function switch_inputs()
{
var from = document.getElementById("From").value;
var to = document.getElementById("To").value;
var temp = from;
from = to;
to = temp;
document.getElementById("From").value = from;
document.getElementById("To").value = to;
}
var switch_input = document.getElementById("switch_inputs")
if (switch_input)
{
document.getElementById("switch_inputs").addEventListener('click',switch_inputs)
}
var search_btn = document.getElementById("search")
if (search_btn)
{
document.getElementById("search").addEventListener('click',json_serialize)
}
function displayTrains(data)
{
data.forEach(ele => {
let train = document.createElement("div")
train.classList.add("train")
// trainname trainid duration starttime startdate trainfrom trainto enddate endtime
// top
let top = document.createElement("div")
top.classList.add("top")
let train_name = document.createElement("p")
train_name.innerHTML = ele["trainname"]
let train_id = document.createElement("p")
train_id.innerHTML = ele["trainid"]
top.appendChild(train_name)
top.appendChild(train_id)
// bottom
// start
let bottom = document.createElement("div")
top.classList.add("bottom")
let start_time = document.createElement("p")
start_time.innerHTML = ele["starttime"]
let train_from = document.createElement("p")
train_from.innerHTML = ele["trainfrom"]
let start_date = document.createElement("p")
start_date.innerHTML = ele["startdate"]
// duration
let duration = document.createElement("p")
duration.innerHTML = ele["duration"]
// to
let end_time = document.createElement("p")
train_from.innerHTML = ele["endtime"]
let train_to = document.createElement("p")
train_to.innerHTML = ele["trainto"]
let end_date = document.createElement("p")
train_from.innerHTML = ele["enddate"]
bottom.appendChild(start_time)
bottom.appendChild(train_from)
bottom.appendChild(start_date)
bottom.appendChild(duration)
bottom.appendChild(end_time)
bottom.appendChild(train_to)
bottom.appendChild(end_date)
train.appendChild(top)
train.appendChild(bottom)
document.querySelector("trains").appendChild(train)
});
}