Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Paysafe Interns Valentin-Kaloyan
Typescript
zornitsa-gencheva-typescript-2023
Commits
48794b7d
Commit
48794b7d
authored
1 year ago
by
Zornitsa Gencheva
Browse files
Options
Download
Email Patches
Plain Diff
video
parent
3705ccec
main
No related merge requests found
Pipeline
#1458
canceled with stages
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/index.html
+0
-1
src/index.html
src/script/index.js
+17
-16
src/script/index.js
src/script/index.ts
+15
-14
src/script/index.ts
with
32 additions
and
31 deletions
+32
-31
src/index.html
+
0
-
1
View file @
48794b7d
...
...
@@ -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>
...
...
This diff is collapsed.
Click to expand it.
src/script/index.js
+
17
-
16
View file @
48794b7d
"
use strict
"
;
Object
.
defineProperty
(
exports
,
"
__esModule
"
,
{
value
:
true
});
exports
.
showResult
=
exports
.
resetOptions
=
exports
.
renderCities
=
exports
.
renderStates
=
exports
.
renderCountries
=
exports
.
getJson
FromApi
=
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
getJson
FromApi
(
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
.
getJson
FromApi
=
getJson
FromApi
;
exports
.
getJson
=
getJson
;
async
function
renderCountries
()
{
const
countries
=
await
getJson
FromApi
(
"
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
getJson
FromApi
(
"
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
getJson
FromApi
(
"
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
);
...
...
This diff is collapsed.
Click to expand it.
src/script/index.ts
+
15
-
14
View file @
48794b7d
...
...
@@ -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
getJson
FromApi
(
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
getJson
FromApi
(
"
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
getJson
FromApi
(
"
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
getJson
FromApi
(
"
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
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help