-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Use context for ticker and settings
- Loading branch information
Showing
9 changed files
with
190 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,13 @@ | ||
import { FC, useEffect, useState } from 'react' | ||
import { Container, Dimmer, Loader } from 'semantic-ui-react' | ||
import { Settings, Ticker } from './lib/types' | ||
import { getInit } from './lib/api' | ||
import ErrorView from './views/ErrorView' | ||
import ActiveView from './views/ActiveView' | ||
import InactiveView from './views/InactiveView' | ||
import { FC } from 'react' | ||
import { TickerProvider } from './components/useTicker' | ||
import ViewRenderer from './ViewRenderer' | ||
|
||
const App: FC = () => { | ||
const [ticker, setTicker] = useState<Ticker | null>(null) | ||
const [settings, setSettings] = useState<Settings>() | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
const [isOffline, setIsOffline] = useState<boolean>(false) | ||
const [gotError, setGotError] = useState<boolean>(false) | ||
|
||
const fetchInit = () => { | ||
getInit() | ||
.then(response => { | ||
if (response.data.settings) { | ||
setSettings(response.data.settings) | ||
} | ||
|
||
if (response.data.ticker?.active) { | ||
setTicker(response.data.ticker) | ||
} | ||
|
||
setIsLoading(false) | ||
}) | ||
.catch(error => { | ||
if (error instanceof TypeError) { | ||
setIsOffline(true) | ||
} else { | ||
setGotError(true) | ||
} | ||
setIsLoading(false) | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
fetchInit() | ||
// This should only be executed once on load (~ componentDidMount) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (ticker?.title) { | ||
document.title = ticker.title | ||
} | ||
}, [ticker]) | ||
|
||
if (isLoading) { | ||
return ( | ||
<Container> | ||
<Dimmer active> | ||
<Loader content="Loading" size="large" /> | ||
</Dimmer> | ||
</Container> | ||
) | ||
} | ||
|
||
if (gotError) { | ||
return ( | ||
<ErrorView message="There seems to be a problem connecting to the server." /> | ||
) | ||
} | ||
|
||
if (isOffline) { | ||
return <ErrorView message="It seems that you are offline." /> | ||
} | ||
|
||
if (ticker?.active) { | ||
return ( | ||
<ActiveView | ||
refreshInterval={settings?.refresh_interval || 0} | ||
ticker={ticker} | ||
/> | ||
) | ||
} | ||
|
||
if (ticker === null && settings?.inactive_settings !== undefined) { | ||
return <InactiveView settings={settings.inactive_settings} /> | ||
} | ||
|
||
return <div>...</div> | ||
return ( | ||
<TickerProvider> | ||
<ViewRenderer /> | ||
</TickerProvider> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { FC, useEffect } from 'react' | ||
import { Container, Dimmer, Loader } from 'semantic-ui-react' | ||
import ErrorView from './views/ErrorView' | ||
import ActiveView from './views/ActiveView' | ||
import InactiveView from './views/InactiveView' | ||
import useTicker from './components/useTicker' | ||
|
||
const ViewRenderer: FC = () => { | ||
const { ticker, settings, isLoading, isOffline, hasError } = useTicker() | ||
|
||
useEffect(() => { | ||
if (ticker?.title) { | ||
document.title = ticker.title | ||
} | ||
}, [ticker]) | ||
|
||
if (isLoading) { | ||
return ( | ||
<Container> | ||
<Dimmer active> | ||
<Loader content="Loading" size="large" /> | ||
</Dimmer> | ||
</Container> | ||
) | ||
} | ||
|
||
if (hasError) { | ||
return ( | ||
<ErrorView message="There seems to be a problem connecting to the server." /> | ||
) | ||
} | ||
|
||
if (isOffline) { | ||
return <ErrorView message="It seems that you are offline." /> | ||
} | ||
|
||
if (ticker?.active) { | ||
return <ActiveView /> | ||
} | ||
|
||
if (ticker === null && settings?.inactive_settings !== undefined) { | ||
return <InactiveView settings={settings.inactive_settings} /> | ||
} | ||
|
||
return <div>...</div> | ||
} | ||
|
||
export default ViewRenderer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.