first working version

This commit is contained in:
Thomas Ruoff
2020-11-28 00:28:38 +01:00
parent 8762aef219
commit a17153759e
22 changed files with 1592 additions and 214 deletions

5
context/app.js Normal file
View File

@@ -0,0 +1,5 @@
import React from "react";
const AppContext = React.createContext();
export default AppContext;

50
context/appProvider.js Normal file
View File

@@ -0,0 +1,50 @@
import React, { useEffect, useState } from "react";
import AppContext from "./app";
const AppProvider = ({ debug, children }) => {
const [songs, setSongs] = useState(null);
const [openSong, setOpenSong] = useState(null);
const URL = debug ? "/api/songs?unlock=true" : "/api/songs";
useEffect(() => {
async function getSongs() {
const response = await fetch(URL);
if (!response.ok) {
setError(`HTTP Status of youtube request: ${response.status}`);
return;
}
const { songs } = await response.json();
setSongs(songs);
}
getSongs();
}, []);
const openDoor = index => {
if (!Number.isInteger(index) || !songs[index]) {
setOpenSong(null);
}
setOpenSong(songs[index]);
};
return (
<AppContext.Provider
value={{
loading: !songs,
songs,
openSong,
openSongIndex: songs && songs.indexOf(openSong),
openDoor
}}
>
{children}
</AppContext.Provider>
);
};
export default AppProvider;