Skip to content

Commit 2cefd77

Browse files
committed
Show loading icon when making request
1 parent 41e8a0c commit 2cefd77

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ <h1>ChatVenture</h1>
2525
<p>Hello PLAYER! Welcome to the text adventure game where the story is made up by GPT :)</p>
2626
</header>
2727

28+
<div class="loading hidden">
29+
<img src="./images/loader.gif" alt="loading icon">
30+
</div>
31+
2832
<div class="genres">
2933
<button class="genre" data-genre="horror">👻</button>
3034
<button class="genre" data-genre="spy film">🕵🏻‍</button>

main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ const makeRequest = async (url, data) => {
1515
return response.json();
1616
}
1717

18+
const showLoadingAnimation = (isLoading) => {
19+
const loadingScreen = document.querySelector('.loading');
20+
if(isLoading) {
21+
loadingScreen.classList.remove('hidden');
22+
} else {
23+
loadingScreen.classList.add('hidden');
24+
}
25+
}
26+
1827
const startGame = async (genre) => {
1928
// Message to send to ChatGPT to start the game
2029
chatGptMessages.push({
@@ -26,6 +35,8 @@ const startGame = async (genre) => {
2635
'Your responses are just in JSON format like this example:\n\n###\n\n {"setting":"setting description","actions":["action 1", "action 2", "action 3"]}###'
2736
});
2837

38+
showLoadingAnimation(true);
39+
2940
// Send request to ChatGPT Chat Completion API
3041
// https://platform.openai.com/docs/api-reference/chat/create
3142
const chatJSON = await makeRequest(
@@ -45,6 +56,8 @@ const startGame = async (genre) => {
4556
const { setting, actions } = content;
4657
console.log('SETTING:', setting);
4758
console.log('ACTIONS:', actions);
59+
60+
showLoadingAnimation(false);
4861
}
4962

5063
const init = () => {

style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ header {
2929
padding: 32px;
3030
}
3131

32+
.loading {
33+
display: flex;
34+
justify-content: center;
35+
align-items: center;
36+
height: 512px;
37+
}
38+
3239
.genres {
3340
display: grid;
3441
grid-template-columns: repeat(3, 100px);
@@ -52,3 +59,7 @@ header {
5259
background-color: rgba(255,255,255,0.7);
5360
border: 4px solid #f1c82d;
5461
}
62+
63+
.hidden {
64+
display: none;
65+
}

0 commit comments

Comments
 (0)