From 705b1f5e1242c4fb0e92d91f644a4adf915b44c6 Mon Sep 17 00:00:00 2001 From: Martyn Ranyard Date: Mon, 3 Aug 2020 00:32:33 +0200 Subject: [PATCH] Allow for reload in caching Signed-off-by: Martyn Ranyard --- .../src/Components/AppAdmin/AppAdmin.js | 51 ++++++++++-- .../src/Components/CacheDeets/CacheDeets.js | 77 ++++++++++++------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/build/react-frontend/src/Components/AppAdmin/AppAdmin.js b/build/react-frontend/src/Components/AppAdmin/AppAdmin.js index 8b99966..7f54de7 100644 --- a/build/react-frontend/src/Components/AppAdmin/AppAdmin.js +++ b/build/react-frontend/src/Components/AppAdmin/AppAdmin.js @@ -46,11 +46,40 @@ class AppAdmin extends React.Component { constructor(props) { super(props); this.setPage = this.setPage.bind(this); - this.state = {page: 0}; + this.reloadSongComponent = this.reloadSongComponent.bind(this); + this.reloadSingersComponent = this.reloadSingersComponent.bind(this); + this.reloadDuetComponent = this.reloadDuetComponent.bind(this); + this.state = { + page: 0, + reloadSongComponent: false, + reloadSingersComponent: false, + reloadDuetComponent: false, + }; } setPage(pageNum) { - this.setState({page: pageNum}) + let newState = this.state; + newState.page = pageNum; + this.setState(newState); + } + + reloadSongComponent(components) { + let newState = this.state; + newState.reloadComponents = components; + this.setState(newState); + } + + reloadSingersComponent(components) { + let newState = this.state; + newState.reloadComponents = components; + this.setState(newState); + } + + reloadDuetComponent(components) { + let newState = this.state; + newState.reloadDuetComponent = components; + console.log("I'm trying!") + this.setState(newState); } render() { @@ -72,6 +101,9 @@ class AppAdmin extends React.Component { const csvURL = "/csv/"+ channelName + "/" + adminToken const tsvURL = "/tsv/"+ channelName + "/" + adminToken const setPage = this.setPage + const reloadDuetComponent = this.state.reloadDuetComponent + const reloadSongComponent = this.state.reloadSongComponent + const reloadSingersComponent = this.state.reloadSingersComponent function handleChange(event, newValue) { setPage(newValue); @@ -95,13 +127,16 @@ class AppAdmin extends React.Component { - + - + - + @@ -132,7 +167,11 @@ class AppAdmin extends React.Component { - + diff --git a/build/react-frontend/src/Components/CacheDeets/CacheDeets.js b/build/react-frontend/src/Components/CacheDeets/CacheDeets.js index 8047b5f..93bec8f 100755 --- a/build/react-frontend/src/Components/CacheDeets/CacheDeets.js +++ b/build/react-frontend/src/Components/CacheDeets/CacheDeets.js @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react'; +import React from 'react'; import { Container, Typography, Button } from '@material-ui/core'; const channelName = window.location.href.split("/")[4] @@ -6,38 +6,63 @@ const adminToken = window.location.href.split("/")[5] const dataURL = "/cachedeets/"+ channelName + "/" + adminToken const forceURL = "/force/"+ channelName + "/" + adminToken -function CacheDeets() { - - function handleClick(e) { - setCacheState({cacheData: null, loading: true }) - fetch(forceURL).then((res) => res.json().then((data)=>{ - setCacheState({cacheData: data, loading: false }) - })); - console.log('The link was clicked.'); +class CacheDeets extends React.Component { + constructor(props) { + super(props); + this.state = { + cacheData: [], + loading: true + }; + this.setCacheData = this.setCacheData.bind(this); + this.setLoading = this.setLoading.bind(this); } - // render() { - const [cacheState, setCacheState] = React.useState({loading: true, cacheData: []}) + setCacheData(data) { + let newState = this.state + newState.cacheData = data + this.setState(newState) + } - useEffect(() => { - // We should only fetch once! - if (cacheState.loading) { - let actualURL = dataURL - if (window.location.href.split("/")[2] === "192.168.1.111:3000") { - //Frontend dev mode only - actualURL = "/sampleData/cache.json" - } - fetch(actualURL).then((res) => res.json().then((data)=>{ - setCacheState({cacheData: data, loading: false }) - })); + setLoading(data) { + let newState = this.state + newState.loading = data + this.setState(newState) + } + + componentDidMount() { + // We should only fetch once! + if (this.state.loading) { + let actualURL = dataURL + if (window.location.href.split("/")[2] === "192.168.1.111:3000") { + //Frontend dev mode only + actualURL = "/sampleData/cache.json" } - }, [setCacheState, cacheState.loading]); + fetch(actualURL).then((res) => res.json().then((data)=>{ + this.setLoading(false) + this.setCacheData(data) + })); + } + } - if (cacheState.loading) { + render() { + const loading = this.state.loading + const setLoading = this.setLoading + const cacheData = this.state.cacheData + + function handleClick(e) { + setLoading(true) + fetch(forceURL).then((data)=>{ + setLoading(false) + window.location.reload() + }) + console.log('The link was clicked.'); + } + + if (loading) { return

Sorry, still loading...

} - let dd = cacheState.cacheData + let dd = cacheData return ( @@ -51,6 +76,6 @@ function CacheDeets() { ); } -//} +} export default CacheDeets;