mirror of
https://github.com/tomru/advcal.git
synced 2026-03-03 06:27:17 +01:00
use jsx extension for react stuff
This commit is contained in:
50
context/appProvider.jsx
Normal file
50
context/appProvider.jsx
Normal file
@@ -0,0 +1,50 @@
|
||||
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 = 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;
|
||||
Reference in New Issue
Block a user