Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions appState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const appState = {
shows: [],
episodes: [],
episodesCache: new Map(),
selectedShow: '',
selectedEpisode: '',
episodeSearchTerm: '',
showSearchTerm: '',
currentView: 'shows',
};

// --------------------------
// API / FETCH FUNCTIONS
// --------------------------

async function getShows() {
const url = 'https://api.tvmaze.com/shows';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}

appState.shows = await response.json();
return true;
} catch (error) {
console.error(error.message);
return false;
}
}

async function getShowEpisodes(showId) {
if (appState.episodesCache.has(appState.selectedShow)) {
appState.episodes = appState.episodesCache.get(appState.selectedShow);
return true;
} else {
const url = `https://api.tvmaze.com/shows/${showId}/episodes`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
appState.episodes = await response.json();
appState.episodesCache.set(
appState.selectedShow,
appState.episodes
);
return true;
} catch (error) {
console.error(error.message);
return false;
}
}
}
Loading