-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
190 lines (182 loc) · 5.93 KB
/
script.js
File metadata and controls
190 lines (182 loc) · 5.93 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
178
179
180
181
182
183
184
185
186
187
188
189
190
// Part 5: Using Promise and Fetch to get JSON data
// CREATE, READ, UPDATE, DELETE, SEARCH, SORT
var countryList = document.getElementById('countries');
let countries = new Promise(resolve => {
fetch("countries.json")
.then(res => {
if(res.status !== 200) {
throw new Error('Cannot fetch the countries');
}
return res.json()
})
.then(data => resolve(data))
.catch(err => alert(err.message))
});
// Counter: Number of countries in the array
countCountries = data => {
var count = document.getElementById('counter');
if (data) {
count.innerHTML = 'There are a total of ' + data + ' countries';
// Show the heading text for the table
document.getElementById('name').style.display = 'block';
} else {
count.innerHTML = 'No country';
// Hide the heading text for the table
document.getElementById('name').style.display = 'none';
document.getElementById('continent').style.display = 'none';
}
};
// Read: GET
getCountries = () => {
countries.then(allCountries => {
var data = '';
if (allCountries.length === 0) {
throw new Error("There is no countries to display")
}
for (i = 0; i < allCountries.length; i++) {
data += '<tr>';
data += '<td>' + allCountries[i].name + '</td>';
data += '<td>' + allCountries[i].continent + '</td>';
data += '<td><button onclick="editCountry(' + i + ')">Edit</button></td>';
data += '<td><button onclick="deleteCountry(' + i + ')">Delete</button></td>';
data += '</tr>';
}
countCountries(allCountries.length);
return countryList.innerHTML = data;
})
.catch(err => alert(err.message));
};
// Create: POST
addCountry = () => {
var countryAdded = document.getElementById('add-country').value.trim();
var continentAdded = document.getElementById('add-continent').value.trim();
countries.then(allCountries => {
if(!countryAdded || !continentAdded) {
throw new Error('You have not inserted a value in one of the input fields');
}
let foundCountry = allCountries.find(country => country.name.toLowerCase().includes(countryAdded.toLowerCase()));
if(foundCountry) {
throw new Error('You are adding a duplicate value');
}
// Get the value
var countryDetails = {
name: countryAdded,
continent: continentAdded
}
if (countryDetails) {
// addCountry the new value
allCountries.push(countryDetails);
// Reset input value
countryAdded.value = '';
// Dislay the new list
getCountries();
}
}).catch(err => alert(err.message));
};
// Update: PUT
editCountry = item => {
var editCountry = document.getElementById('edit-country');
var editContinent = document.getElementById('edit-continent');
countries.then(allCountries => {
// Display value in the field
editCountry.value = allCountries[item].name;
editContinent.value = allCountries[item].continent;
// Display fields
document.getElementById('editForm').style.display = 'block';
// When the form is submitted
document.getElementById('saveEdit').onsubmit = () => {
if(!editCountry.value.trim() || !editContinent.value.trim()) {
throw new Error('You have not inserted a value in one of the input fields');
}
// Get value
var countryDetails = {
name: editCountry.value,
continent: editContinent.value
};
if (countryDetails) {
// editCountry value
allCountries.splice(item, 1, countryDetails);
// Display the new list
getCountries();
// Hide fields
closeInput();
}
}
}).catch(err => alert(err.message));
};
// Delete: Delete
deleteCountry = item => {
countries.then(allCountries => {
// deleteCountry the current row
allCountries.splice(item, 1);
// Display the new list
getCountries();
}).catch(err => alert(err.message));
};
// Search: Country Search
searchbar = () => {
var searchedCountry = document.getElementById('search').value.trim();
countries.then(allCountries => {
if (!searchedCountry) {
throw new Error('Nothing was entered in the search bar');
}
// Filter all the countries in the array with value typed into the input field
let countriesFound = allCountries.filter(country => country.name.toLowerCase().includes(searchedCountry.toLowerCase()));
if(countriesFound.length === 0) {
throw new Error('No countries were found');
}
var data = '';
for (i = 0; i < countriesFound.length; i++) {
data += '<tr>';
data += '<td>' + countriesFound[i].name + '</td>';
data += '<td>' + countriesFound[i].continent + '</td>';
data += '<td><button onclick="editCountry(' + i + ')">Edit</button></td>';
data += '<td><button onclick="deleteCountry(' + i + ')">Delete</button></td>';
data += '</tr>';
}
countCountries(countriesFound.length);
return countryList.innerHTML = data;
}).catch(err => alert(err.message));
};
// Sort: Sort countries alphabetically
sortCountries = () => {
countries.then(allCountries => {
// Sorting alphabetically in decending order
allCountries.sort((a, b) => {
let fa = a.name.toLowerCase(),
fb = b.name.toLowerCase();
if (fa < fb) {
return 1;
}
if (fa > fb) {
return -1;
}
return 0;
});
getCountries();
}).catch(err => alert(err.message));
}
// Sort: Sort continents alphabetically
sortContinent = () => {
countries.then(allCountries => {
// Sorting alphabetically in decending order
allCountries.sort((a, b) => {
let fa = a.continent.toLowerCase(),
fb = b.continent.toLowerCase();
if (fa < fb) {
return 1;
}
if (fa > fb) {
return -1;
}
return 0;
});
getCountries();
}).catch(err => alert(err.message));
}
// Where the script starts. This executes when the file loads on the browser
getCountries();
// Close Edit form
closeInput = () => {
document.getElementById('editForm').style.display = 'none';
}