-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.html
More file actions
177 lines (165 loc) · 6.95 KB
/
Copy pathupdate.html
File metadata and controls
177 lines (165 loc) · 6.95 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PhoneModelAPI</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="client_put.js"></script>
</head>
<style>
label {
margin-bottom: 6px;
}
</style>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">PhoneModelAPI</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/get">Get</a></li>
<li><a href="/post">Post</a></li>
<li><a href="#">Update</a></li>
<li><a href="/delete">Delete</a></li>
</ul>
</div>
</nav>
<div class="container-fluid">
<iframe name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>
<!-- -->
<div class="col-sm-5" style="background-color:lavender;">
<label>Get manufacturer</label>
<input type="text" id="getMfrCall" name="getMfrCall" placeholder="e.g. samsung">
<input type="button" id="getMfrCallBtn" value="GET" onclick="getMfrCall()">
<input type="button" id="updateMfrBtn" value="UPDATE" onclick="updateManufacturer()">
</div>
<div class="col-sm-7" style="background-color:lavender;">
<label>Get phone</label>
<input type="text" id="getCall" name="getCall" placeholder="e.g. samsung/1">
<input type="button" id="getCallBtn" value="GET" onclick="getCall()">
<input type="button" id="updateBtn" value="UPDATE" onclick="updateProduct()">
</div>
<!-- -->
<div class="col-sm-3" style="background-color:lavenderblush;">
<form action="/api/v1/manufacturers/" method="post" target="hiddenFrame" id="postFormMfr">
<label for="mfr">Manufacturer</label> <br>
<input type="text" id="trueMfr" name="trueMfr" value=""><br>
</form>
</div>
<div class="col-sm-9" style="background-color:lavenderblush;">
<div class="col-sm-3">
</div>
<div class="col-sm-7">
</div>
<form action="/api/v1/manufacturers/" method="post" target="hiddenFrame" id="postForm">
<div class="col-sm-3">
<label for="mfr">Manufacturer</label> <br>
<label for="model_id">Model id</label> <br>
<label for="model_name">Model name</label> <br>
<label for="release_date">Release date</label><br>
<label for="weight_g">Weight (gram)</label><br>
<label for="display_size_inch">Display size (inch)</label><br>
<label for="resolution">Resolution</label><br>
<label for="camera">Camera (MP)</label><br>
<label for="battery_capacity">Battery Capacity (mAh)</label><br>
<label for="operating_system">Operating System</label><br>
<label for="os_version">OS Version</label><br>
<label for="category">Category</label><br>
</div>
<div class="col-sm-7" id="textFields">
<input type="text" id="mfr" name="mfr" disabled value=""><br>
<input type="text" id="model_id" name="model_id" disabled value=""><br>
<input type="text" id="model_name" name="model_name" value=""><br>
<input type="date" id="release_date" name="release_date" value=""><br>
<input type="text" id="weight_g" name="weight_g" value=""><br>
<input type="text" id="display_size_inch" name="display_size_inch" value=""><br>
<input type="text" id="resolution" name="resolution" value=""><br>
<input type="text" id="camera" name="camera" value=""><br>
<input type="text" id="battery_capacity" name="battery_capacity" value=""><br>
<input type="text" id="operating_system" name="operating_system" value=""><br>
<input type="text" id="os_version" name="os_version" value=""><br>
<input type="text" id="category" name="category" value=""><br>
</div>
</form>
</div>
</div>
<script>
function getCall() {
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let json = JSON.parse(xmlhttp.responseText);
if (json.length > 0){ // something found
console.table(json);
json = Object.values(json[0]);
console.log(json);
addTextToFields(json);
}
else {
alert('No phone found')
}
}
};
xmlhttp.open("GET", "/api/v1/manufacturers/" + document.getElementById('getCall').value ,true);
xmlhttp.send();
}
function addTextToFields(json) {
let x = document.getElementById('textFields').children;
let mfr = document.getElementById('getCall').value.split('/')[0];
x[0].value = mfr;
let j = 0;
for (let i = 2; i < x.length; i+=2) {
x[i].value = json[j];
j++;
}
}
function getMfrCall() {
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
let json = JSON.parse(xmlhttp.responseText);
if (json.length > 0){ // something found
addTextToMfrField(json, document.getElementById('getMfrCall').value);
//console.table(json);
}
else {
alert('No manufacturer found')
}
}
};
xmlhttp.open("GET", "/api/v1/manufacturers",true);
xmlhttp.send();
}
/**
* Checks if the given mfr exists in the provided json. If so inserts the
* found string to the trueMFR field, else inserts 'Manufacturer not found'
* @param json
* @param mfr
*/
function addTextToMfrField(json, mfr) {
let foundMFR = false;
let trueMfr = document.getElementById('trueMfr');
for(let i in json){
//if the mfr is found set values
if(json[i] === mfr){
trueMfr.value = json[i];
foundMFR = true;
break;
}
}
//if no mfr was found set value
if(!foundMFR){
trueMfr.value = "Manufacturer not found"
}
}
</script>
</body>
</html>