Files
advcal/context/appProvider.jsx
Thomas Ruoff 9c77305e72 simplify debug
2020-11-28 23:10:39 +01:00

51 lines
1.0 KiB
JavaScript

import React, { useEffect, useState } from "react";
import AppContext from "./app";
const AppProvider = ({ debug = false, children }) => {
const [songs, setSongs] = useState(null);
const [openSong, setOpenSong] = useState(null);
const URL = `/api/songs?unlock=${debug}`
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;