-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.html
176 lines (142 loc) · 6.81 KB
/
try.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Today News</title>
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@200;300;400&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="/img/icon.png" type="image/x-icon">
<link rel="stylesheet" href="css/styleNews.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
</head>
<body>
<!--Menu-->
<div class="ui large secondary inverted pointing fixedBar fixed menu">
<a class="toc item">
<i class="sidebar icon"></i>
</a>
<div class="sc-logo">News</div>
<div class="right item">
<a class="item" href="index.html">All</a>
<a class="item" href="general.html">General</a>
<a class="item" href="business.html">Business</a>
<a class="item" href="technology.html">Technology</a>
<a class="item" href="entertainment.html">Entertainment</a>
<a class="item" href="health.html">Health</a>
<a class="item" href="science.html">science</a>
<a class="item active item" href="try.html">sports</a>
</div>
</div>
<!--Weather & Time Section-->
<section class="WeatherCal-sports-section">
<div class="ui equal width center aligned stakable padded grid">
<div class="row">
<div class="sixsteen wide mobile sixsteen wide tablet eight wide computer column cc">
<div class="date-container">
<div class="time" id="time">
<span id="am-pm">pm</span>
</div>
<div class="date" id="date"></div>
</div>
</div>
<div class="sixsteen wide mobile sixsteen wide tablet eight wide computer column cc">
<div class="card cardWeather">
<div class="search">
<input type="text" class="searchBar" placeholder="Search">
<button><i class="search link icon"></i></button>
</div>
<div class="weather loading">
<h3 class="city">weather in Buraydah</h3>
<h1 class="temp">16 °C</h1>
<img src="https://www.weatherbit.io/static/img/icons/t05d.png" alt="" class="iconWeather" />
<div class="description">mostly clear</div>
<div class="humidity">humidity</div>
<div class="wind">14 km/h</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!--Search, dropdown Row and Cards fetch API row-->
<section id="contents" class="sc-section">
<div class="ui stakable grid container">
<div class="four column row">
<div class="right floated column">
<div class="ui container" id="seachCountry">
<span>
<div class="ui icon button" data-tooltip="us= United State,sa= Saudi arabia" data-position="top left" data-inverted="">
<i>!</i>
</div>
</span>
</div>
</div>
</div>
<div class="row">
<div class="ui cards" id="output">
</div>
</div>
</div>
</section>
<script>
const apikey = 'https://newsapi.org/v2/top-headlines/sources?category=sports&apiKey=8c06a7999aa843788ba579a121cc37d5';
async function getNews(){
const response = await fetch(apikey);
const data = await response.json();
// const prin = data.map(m => m.data.sources)
// const filter = data.sources.find(item => item.country === 'us' && 'gb');
console.log(data.sources)
// const countries = data.sources.map(cn => cn.country);
// console.log(countries)
// const distinctCountries = countries.filter((country, index) => countries.indexOf(country) === index);
// console.log(distinctCountries);
// const uni = data.sources.reduce((map, obj) => map.set(obj.country, obj) , new Map());
// console.log(uni)
printCards(data)
}
function printCards(data) {
const header = document.querySelector('#seachCountry');
const countries = data.sources.map(countryName => countryName.country);
console.log(countries)
const distinctCountries = countries.filter((country, index) => countries.indexOf(country) === index);
console.log(distinctCountries)
header.innerHTML += `
<select class='ui fluid selection dropdown select' onchange="getCountry(this.value)">
<option>Select The Country</option>
${distinctCountries.map(country => `<option>${country}</option>`)}
</select>`;
}
async function getCountry(e){
if ( e !== 'Select The Country'){
// console.log(e)
//Another Fetch based on user choice
const response = await fetch(`${apikey}&country=${e}`)
const data = await response.json();
console.log(data.sources)
//It only shows the information of the country in which the user is interested
document.getElementById("output").innerHTML = data.sources.map(countryName =>
`<div class="ui fluid container card cardContainer">
<div class="content">
<h4 class="header">${countryName.name}</h4>
</div>
<div class="content">
<div class="desc">${countryName.description}</div>
</div>
<div class="extra content">
<span>
<a href=${countryName.url} target="_blank" class="ui button btn-read">Read more</a>
</span>
</div>
</div>`).join('');
}
}
getNews();
</script>
<script src="js/weather.js" defer></script>
<script src="js/script.js"></script>
</body>
</html>
<!-- ${data.sources.reduce((map, obj) => `<option>${data.map.set(obj.country, obj) , new Map()}</option>`)} -->