Skip to content

Commit

Permalink
[FEATURE] Add update mechanism
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
on3iro committed Mar 4, 2019
1 parent 78dd183 commit 8b5eb9b
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 76 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ jspm_packages
# Yarn Integrity file
.yarn-integrity

# https://raw.githubusercontent.com/github/gitignore/master/Global/VisualStudioCode.gitignore
# ======================================================================================================================
# Logs
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# Project
# ======================================================================================================================
src/js/main.bundle.*
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"dependencies": {
"@material-ui/core": "^3.9.2",
"@material-ui/icons": "^3.0.2",
"@types/axios": "^0.14.0",
"@types/jest": "^24.0.9",
"@types/node": "^11.9.5",
"@types/react": "^16.8.5",
"@types/react-dom": "^16.8.2",
"axios": "^0.18.0",
"classnames": "^2.2.6",
"cross-env": "^5.2.0",
"gh-pages": "^2.0.1",
Expand Down
84 changes: 84 additions & 0 deletions src/components/App/MainApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { useState, useEffect } from 'react'

import { get as getFromDb } from 'idb-keyval'
import classNames from 'classnames'

import { ROUTES } from '../../routes'
import config from '../../config'
import { SetConfigurationContext } from '../../globalContexts'

import Content from '../Content'

import TopBar from './TopBar'
import DrawerMenu from './DrawerMenu'

const MainApp = ({ classes }: { classes: any }) => {
const [ currentLocation, setCurrentLocation ] = useState(ROUTES.nemeses)
const moveTo = (route: string) => {
toggleDrawer()
setCurrentLocation(route)
}

const [ isLoading, setLoading ] = useState<boolean>(true)

const [ drawerIsOpen, setDrawerIsOpen ] = useState(false)
const toggleDrawer = () => setDrawerIsOpen(!drawerIsOpen)

const setsAndPromos = config.EXPANSIONS
const defaultSets = setsAndPromos.reduce(
(acc, set) => ({ ...acc, [set]: false }) , {}
)
const [ configurationOfSets, setSets ] = useState<{[key: string]: boolean}>(defaultSets)

// Get sets from indexedDB
useEffect(() => {
getFromDb('sets')
.then(sets => {
if (sets !== undefined) {
setSets((sets as {[key: string]: boolean}))
}
setLoading(false)
}).catch(() => {
setLoading(false)
})

// Effect clean up, used if the component unmounts before the effect is
// fully resolved. In this case just ignore the incoming promise result.
return () => { return /* NoOp */ }
}, [])

return (
<React.Fragment>
<TopBar
classes={classes}
drawerIsOpen={drawerIsOpen}
currentLocation={currentLocation}
toggleDrawer={toggleDrawer}
/>
<DrawerMenu
drawerIsOpen={drawerIsOpen}
toggleDrawer={toggleDrawer}
classes={classes}
moveTo={moveTo}
/>
<SetConfigurationContext.Provider
value={{ configurationOfSets, setSets, sets: setsAndPromos }}
>
<Content
isLoading={isLoading}
route={currentLocation}
classes={classes}
className={
classNames(
classes.content,
{ [classes.contentShift]: drawerIsOpen },
{ [classes.loading]: isLoading }
)
}
/>
</SetConfigurationContext.Provider>
</React.Fragment>
)
}

export default MainApp
70 changes: 70 additions & 0 deletions src/components/App/UpdateScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react'

import styled from 'styled-components/macro'

import Button from '@material-ui/core/Button'
import Typography from '@material-ui/core/Typography'
import Link from '@material-ui/core/Link'
import Card from '@material-ui/core/Card'
import CardContent from '@material-ui/core/CardContent'

const Wrapper = styled('div')`
display: flex;
justify-content: center;
flex-flow: column nowrap;
width: 100%;
`

const ButtonWrapper = styled('div')`
text-align: center;
width: 100%;
.update-button {
margin-top: 24px;
}
`

const StyledHeadline = styled(Typography)`
padding-bottom: 16px;
`

const StyledCard = styled(Card)`
width: auto;
max-width: 320px;
margin: 56px auto 0;
text-align: center;
`

const UpdateScreen = React.memo(({ newVersion }: {
newVersion: string
}) => {
return (
<Wrapper>
<StyledCard>
<CardContent>
<StyledHeadline variant="h6" component="h1">
New Version: { newVersion }
</StyledHeadline>
<Typography paragraph>
Good news, a new Version of Aeons End Randomizer is available :)
</Typography>
<Typography paragraph>
<Link href="https://github.com/on3iro/aeons-end-randomizer/releases" target="_blank">Changelog</Link>
</Typography>
<ButtonWrapper>
<Button
onClick={() => window.location.reload(true)}
variant="contained"
color="secondary"
className="update-button"
>
Update Now!
</Button>
</ButtonWrapper>
</CardContent>
</StyledCard>
</Wrapper>
)
})

export default UpdateScreen
1 change: 1 addition & 0 deletions src/components/App/appStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const drawerWidth = 240;

export const styles = (theme: any) => createStyles({
root: {
height: '100%',
display: 'flex',
paddingBottom: 72
},
Expand Down
116 changes: 41 additions & 75 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,103 +1,69 @@
import React, { useState, useEffect } from 'react';

import { get as getFromDb } from 'idb-keyval'

import classNames from 'classnames'
import 'rpg-awesome/css/rpg-awesome.min.css'

import axios from 'axios'

import CssBaseline from '@material-ui/core/CssBaseline'
import blue from '@material-ui/core/colors/blue'
import pink from '@material-ui/core/colors/pink'
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles'
import blue from '@material-ui/core/colors/blue';
import pink from '@material-ui/core/colors/pink';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { withStyles } from '@material-ui/core/styles'

import { styles } from './appStyles'
import { ROUTES } from '../../routes'
import config from '../../config'
import { SetConfigurationContext } from '../../globalContexts'

import Content from '../Content'

import TopBar from './TopBar'
import DrawerMenu from './DrawerMenu'

import UpdateScreen from './UpdateScreen'
import MainApp from './MainApp'

const App = ({ classes }: { classes: any }) => {
const [ drawerIsOpen, setDrawerIsOpen ] = useState(false)
const toggleDrawer = () => setDrawerIsOpen(!drawerIsOpen)

const [ currentLocation, setCurrentLocation ] = useState(ROUTES.nemeses)
const moveTo = (route: string) => {
toggleDrawer()
setCurrentLocation(route)
}
const theme = createMuiTheme({
palette: {
primary: blue,
secondary: pink,
},
typography: {
useNextVariants: true,
},
});

const [ isLoading, setLoading ] = useState<boolean>(true)

const setsAndPromos = config.EXPANSIONS
const defaultExpansionConfig = setsAndPromos.reduce(
(acc, set) => ({ ...acc, [set]: false }) , {}
)
const [ configurationOfSets, setSets ] = useState<{[key: string]: boolean}>(defaultExpansionConfig)
type UpdateInformation = {
updateAvailable: boolean,
newVersion: string
}

const theme = createMuiTheme({
palette: {
primary: blue,
secondary: pink,
},
typography: {
useNextVariants: true,
},
});
const App = React.memo(({ classes }: { classes: any }) => {
const [ updateInformation, setUpdateAvailable ] = useState<UpdateInformation>({
updateAvailable: false,
newVersion: ''
})

// Get sets from indexedDB
useEffect(() => {
getFromDb('sets')
.then(sets => {
if (sets !== undefined) {
setSets((sets as {[key: string]: boolean}))
axios.get('https://api.github.com/repos/on3iro/aeons-end-randomizer/releases')
.then(response => {
const newestRelease = response.data[0]
const localAppVersion = process.env.REACT_APP_VERSION
const newVersion = newestRelease['tag_name']
if (newVersion !== `v${localAppVersion}`) {
setUpdateAvailable({
updateAvailable: true,
newVersion
})
}
setLoading(false)
}).catch(() => {
setLoading(false)
})
}, [])

return (
<MuiThemeProvider theme={theme}>
<div className={classes.root}>
<CssBaseline />
<TopBar
classes={classes}
drawerIsOpen={drawerIsOpen}
currentLocation={currentLocation}
toggleDrawer={toggleDrawer}
/>
<DrawerMenu
drawerIsOpen={drawerIsOpen}
toggleDrawer={toggleDrawer}
classes={classes}
moveTo={moveTo}
/>
<SetConfigurationContext.Provider
value={{ configurationOfSets, setSets, sets: setsAndPromos }}
>
<Content
isLoading={isLoading}
route={currentLocation}
classes={classes}
className={
classNames(
classes.content,
{ [classes.contentShift]: drawerIsOpen },
{ [classes.loading]: isLoading }
)
}
/>
</SetConfigurationContext.Provider>
{
updateInformation.updateAvailable
? <UpdateScreen newVersion={updateInformation.newVersion} />
: <MainApp classes={classes} />
}
</div>
</MuiThemeProvider>
);
}
})

export default withStyles(styles, { withTheme: true })(App)
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,13 @@
"@svgr/plugin-svgo" "^4.0.3"
loader-utils "^1.1.0"

"@types/axios@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@types/axios/-/axios-0.14.0.tgz#ec2300fbe7d7dddd7eb9d3abf87999964cafce46"
integrity sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=
dependencies:
axios "*"

"@types/classnames@^2.2.7":
version "2.2.7"
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.7.tgz#fb68cc9be8487e6ea5b13700e759bfbab7e0fefd"
Expand Down Expand Up @@ -1618,6 +1625,14 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==

axios@*, axios@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
integrity sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=
dependencies:
follow-redirects "^1.3.0"
is-buffer "^1.1.5"

axobject-query@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.0.2.tgz#ea187abe5b9002b377f925d8bf7d1c561adf38f9"
Expand Down Expand Up @@ -4175,7 +4190,7 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

follow-redirects@^1.0.0:
follow-redirects@^1.0.0, follow-redirects@^1.3.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.7.0.tgz#489ebc198dc0e7f64167bd23b03c4c19b5784c76"
integrity sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==
Expand Down

0 comments on commit 8b5eb9b

Please sign in to comment.