Commit 48794b7d authored by Zornitsa Gencheva's avatar Zornitsa Gencheva
Browse files

video

parent 3705ccec
No related merge requests found
Pipeline #1458 canceled with stages
Showing with 32 additions and 31 deletions
+32 -31
......@@ -3,7 +3,6 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="~/lib/fontawesome/css/all.css" asp-append-version="true" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<link rel="stylesheet" href="style/style.css">
<title>Countries</title>
......
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.showResult = exports.resetOptions = exports.renderCities = exports.renderStates = exports.renderCountries = exports.getJsonFromApi = exports.logAuthToken = void 0;
exports.showResult = exports.resetOptions = exports.renderCities = exports.renderStates = exports.renderCountries = exports.getJson = exports.logAuthToken = void 0;
async function logAuthToken() {
let headers = new Headers();
headers.set("Accept", "application/json");
......@@ -11,11 +11,14 @@ async function logAuthToken() {
headers: headers
});
let response = await fetch(request);
if (!response.ok) {
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`);
}
let responseAsJson = await response.json();
localStorage.setItem("auth_key", responseAsJson.auth_token);
}
exports.logAuthToken = logAuthToken;
async function getJsonFromApi(url) {
async function getJson(url) {
if (localStorage.getItem("auth_key") == null) {
await logAuthToken();
}
......@@ -26,22 +29,20 @@ async function getJsonFromApi(url) {
method: "GET",
headers: headers
});
let responseAsJson = await fetch(request)
.then(response => {
if (!response.ok) {
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`);
}
return response.json();
});
let response = await fetch(request);
if (!response.ok) {
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`);
}
let responseAsJson = await response.json();
return responseAsJson;
}
exports.getJsonFromApi = getJsonFromApi;
exports.getJson = getJson;
async function renderCountries() {
const countries = await getJsonFromApi("https://www.universal-tutorial.com/api/countries");
const countries = await getJson("https://www.universal-tutorial.com/api/countries");
let countriesElement = document.getElementById("countries");
for (const country of countries) {
let optionElement = document.createElement("option");
let optionElementValue = country["country_name"];
let optionElementValue = country.country_name;
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
countriesElement?.appendChild(optionElement);
......@@ -51,11 +52,11 @@ exports.renderCountries = renderCountries;
async function renderStates() {
resetOptions("states", "State");
let selectedCountry = document.getElementById('countries').value;
let statesAsJson = await getJsonFromApi("https://www.universal-tutorial.com/api/states/" + selectedCountry);
let statesAsJson = await getJson("https://www.universal-tutorial.com/api/states/" + selectedCountry);
let statesElement = document.getElementById("states");
for (const stateOption of statesAsJson) {
let optionElement = document.createElement("option");
let optionElementValue = String(stateOption["state_name"]);
let optionElementValue = String(stateOption.state_name);
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
statesElement?.appendChild(optionElement);
......@@ -65,11 +66,11 @@ exports.renderStates = renderStates;
async function renderCities() {
resetOptions("cities", "City");
let selectedState = document.getElementById('states').value;
let statesAsJson = await getJsonFromApi("https://www.universal-tutorial.com/api/cities/" + selectedState);
let statesAsJson = await getJson("https://www.universal-tutorial.com/api/cities/" + selectedState);
let citiesElement = document.getElementById("cities");
for (const cityOption of statesAsJson) {
let optionElement = document.createElement("option");
let optionElementValue = String(cityOption["city_name"]);
let optionElementValue = String(cityOption.city_name);
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
citiesElement?.appendChild(optionElement);
......
......@@ -11,11 +11,14 @@ export async function logAuthToken(){
}
)
let response = await fetch(request);
if(!response.ok){
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`)
}
let responseAsJson = await response.json();
localStorage.setItem("auth_key", responseAsJson.auth_token)
}
export async function getJsonFromApi(url:string) {
export async function getJson(url:string) {
if(localStorage.getItem("auth_key") == null){
await logAuthToken();
}
......@@ -31,23 +34,21 @@ export async function getJsonFromApi(url:string) {
headers : headers
}
)
let responseAsJson = await fetch(request)
.then(response => {
if(!response.ok){
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`)
}
return response.json();
});
let response = await fetch(request)
if(!response.ok){
throw new Error(`Error with status ${response.status} has occured: ${response.statusText}`)
}
let responseAsJson = await response.json();
return responseAsJson;
}
export async function renderCountries() {
const countries = await getJsonFromApi("https://www.universal-tutorial.com/api/countries");
const countries = await getJson("https://www.universal-tutorial.com/api/countries");
let countriesElement = document.getElementById("countries");
for (const country of countries) {
let optionElement = document.createElement("option");
let optionElementValue : string = country["country_name"];
let optionElementValue : string = country.country_name;
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
countriesElement?.appendChild(optionElement);
......@@ -57,11 +58,11 @@ export async function renderCountries() {
export async function renderStates(){
resetOptions("states", "State")
let selectedCountry = (<HTMLSelectElement>document.getElementById('countries')).value;
let statesAsJson = await getJsonFromApi("https://www.universal-tutorial.com/api/states/" + selectedCountry);
let statesAsJson = await getJson("https://www.universal-tutorial.com/api/states/" + selectedCountry);
let statesElement = document.getElementById("states");
for (const stateOption of statesAsJson) {
let optionElement = document.createElement("option");
let optionElementValue : string = String(stateOption["state_name"]);
let optionElementValue : string = String(stateOption.state_name);
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
statesElement?.appendChild(optionElement);
......@@ -71,11 +72,11 @@ export async function renderStates(){
export async function renderCities() {
resetOptions("cities", "City")
let selectedState = (<HTMLSelectElement>document.getElementById('states')).value;
let statesAsJson = await getJsonFromApi("https://www.universal-tutorial.com/api/cities/" + selectedState);
let statesAsJson = await getJson("https://www.universal-tutorial.com/api/cities/" + selectedState);
let citiesElement = document.getElementById("cities");
for (const cityOption of statesAsJson) {
let optionElement = document.createElement("option");
let optionElementValue : string = String(cityOption["city_name"]);
let optionElementValue : string = String(cityOption.city_name);
optionElement.value = optionElementValue;
optionElement.text = optionElementValue;
citiesElement?.appendChild(optionElement);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment