-
Notifications
You must be signed in to change notification settings - Fork 1
/
entertainment.php
159 lines (140 loc) · 5.37 KB
/
entertainment.php
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
<!-- entertainment section -->
<h2 class="page-header text-center" id="cat-header">Entertainment</h2>
<!-- get category from url -->
<?php
$category = $_GET['cat'];
$cats = ['jokes', 'quotes', 'riddles'];
// if (!in_array($category, $cats)) {
// include('404.php');
// }
?>
<div class="row ">
<div class="col-md-1"></div>
<div class="col-md-10" id="cat-div">
<!-- dynamic cat content -->
</div>
<div class="col-md-1"></div>
</div>
<!-- script -->
<script>
//modal content
const modal_content_ = `
<div class="d-flex justify-content-center">
<div class="spinner-border" style="width: 3rem; height: 3rem;" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
`
const base_url = 'https://api.api-ninjas.com/v1/';
const api_key = 'pVu+077nJZLR5aSU3rT4Uw==Mjk3lCw4PETJptdl';
const category = '<?php echo $category; ?>';
const cat_div = document.getElementById('cat-div');
const cat_header = document.getElementById('cat-header');
switch (category) {
case 'jokes':
getJokes();
break;
case 'quotes':
getQuotes();
setQoutesCategoryDiv();
//end
//get quotes
break;
case 'riddles':
getRiddles();
break;
default:
break;
}
// alert(category);
//get jokes
async function getJokes() {
cat_header.innerHTML = 'Jokes';
//show loading
cat_div.innerHTML = modal_content_;
//get jokes
const jokes = await fetch(base_url + 'jokes?limit=1', {
headers: { 'X-Api-Key': api_key },
})
.then(res => res.json())
.then(data => data[0]).catch(err => '');
//show jokes
const refresh_btn = `<button class="btn btn-primary" onclick="getJokes()">Refresh</button>`;
cat_div.innerHTML = `
${jokes.joke} <br>
${refresh_btn}
`;
}
async function getQuotes() {
cat_header.innerHTML = 'Quotes';
setQoutesCategoryDiv()
//get quotes
//url : https://api.api-ninjas.com/v1/quotes?category=
addEventListener('change', (async (e) => {
const category = e.target.value;//show loading
cat_div.innerHTML = modal_content_;
const quotes = await fetch(base_url + 'quotes?category=' + category, {
headers: { 'X-Api-Key': api_key },
})
.then(res => res.json())
.then(data => data[0]).catch(err => '');
console.log(quotes);
refresh_btn = `<button class="btn btn-primary" onclick="getQuotes()">Refresh</button>`;
cat_div.innerHTML = `
<blockquote class="blockquote">
<p class="text text-center">${quotes.quote} </p><br>
</blockquote>
~${quotes.author} <br>
<br>
${refresh_btn}
`;
}))
}
let riddles_ = {
question: '',
answer: '',
btn: '',
}
function setQoutesCategoryDiv(){
//set category dropdown
const categoryDropdown = [
"age", "alone", "amazing", "anger", "architecture", "art", "attitude", "beauty", "best", "birthday", "business", "car", "change", "communications", "computers", "cool", "courage", "dad", "dating", "death", "design", "dreams", "education", "environmental", "equality", "experience", "failure", "faith", "family", "famous", "fear", "fitness", "food", "forgiveness", "freedom", "friendship", "funny", "future", "god", "good", "government", "graduation", "great", "happiness", "health", "history", "home", "hope", "humor", "imagination", "inspirational", "intelligence", "jealousy", "knowledge", "leadership", "learning", "legal", "life", "love", "marriage", "medical", "men", "mom", "money", "morning", "movies", "success"
]
const categoryDropdown_ = categoryDropdown.map(cat => `<option value="${cat}">${cat}</option>`);
const categoryDropdownHtml = `
<select class="form-select" aria-label="Default select example">
<option selected>Open this select menu</option>
${categoryDropdown_}
</select>
`;
cat_div.innerHTML = categoryDropdownHtml;
}
async function getRiddles() {
//url : https://api.api-ninjas.com/v1/riddles
cat_header.innerHTML = 'Riddles';
//show loading
cat_div.innerHTML = modal_content_;
//get riddles
const riddles = await fetch(base_url + 'riddles', {
headers: { 'X-Api-Key': api_key },
}).then(res => res.json()).then(data => data[0]).catch(err => '');
riddles_ = { ...riddles };
const refresh_btn = `<button class="btn btn-primary" onclick="getRiddles()">Refresh</button>`;
riddles_.btn = refresh_btn;
const answer_btn = `<button class="btn btn-primary" onclick="showAnswer()">Show Answer</button>`
cat_div.innerHTML = `
${riddles.question} <br>
${answer_btn}
${refresh_btn}
`;
}
//showAnswer();
function showAnswer() {
console.log(riddles_);
cat_div.innerHTML = `
${riddles_.question} <br>
<p class="alert alert-success">${riddles_.answer} <br></p>
${riddles_.btn}
`;
}
</script>