Skip to content

Commit

Permalink
added support for local shici
Browse files Browse the repository at this point in the history
  • Loading branch information
unicar9 committed May 29, 2019
1 parent 4c9040d commit 141e85c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"dependencies": {
"evergreen-ui": "^4.15.0",
"html2canvas": "^1.0.0-rc.1",
"jinrishici": "^1.0.6",
"prop-types": "^15",
"react": "^16",
"react-dom": "^16",
Expand Down
38 changes: 29 additions & 9 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import ConfigMenu from './components/ConfigMenu'
import html2canvas from 'html2canvas'
import Storager from './utils/storager'
import { InlineAlert } from 'evergreen-ui'
import { load } from './utils/jinrishici'

let shici = require('./utils/shici.json')

class App extends Component {
constructor (props) {
Expand All @@ -23,20 +26,37 @@ class App extends Component {
this.state = {
isPlaying: true,
defaultPlayChecked: true,
colorStayChecked: false
colorStayChecked: false,
verses: {
content: '红豆生南国,春来发几枝。',
origin: {
author: '王维',
title: '相思'
}
},
errMessage: ''
}
}

componentDidMount () {
Storager.get(['selected', 'colorStayChecked', 'defaultPlayChecked'], res => {
load(result => {
Storager.set({ verses: result.data })
}, result => {
this.setState({ errMessage: result.errMessage })
const localShici = shici[Math.floor(Math.random() * shici.length)]
Storager.set({ verses: localShici })
})

Storager.get(['verses', 'selected', 'colorStayChecked', 'defaultPlayChecked'], res => {
const isColorStayCheckedUntouched = res.colorStayChecked === undefined
const isDefaultPlayCheckedUntouched = res.defaultPlayChecked === undefined

this.setState({
colorStayChecked: isColorStayCheckedUntouched ? false : res.colorStayChecked,
defaultPlayChecked: isDefaultPlayCheckedUntouched ? true : res.defaultPlayChecked,
isPlaying: isDefaultPlayCheckedUntouched ? true : res.defaultPlayChecked,
selected: res.selected || 'waves'
selected: res.selected || 'waves',
verses: res.verses
})
})
}
Expand Down Expand Up @@ -97,13 +117,13 @@ class App extends Component {
}

render () {
const { isPlaying, defaultPlayChecked, colorStayChecked, selected } = this.state
const { verses, isPlaying, defaultPlayChecked, colorStayChecked, selected, errMessage } = this.state
const sketches = { blobs: blobs, waves: waves }

return selected ? (
<div className='App' tabIndex='-1' onKeyPress={this.handleKeyPress}>
<div id='color-name' style={{ display: selected === 'blobs' ? 'none' : 'block' }} className={colorStayChecked ? '' : 'fadeout'} />
<LoadedVerses className={selected} />
<LoadedVerses className={selected} verses={verses} />
<P5Wrapper sketch={sketches[selected]} isPlaying={isPlaying} />
<ConfigMenu
onSaveSelect={this.onSaveSelect}
Expand All @@ -116,11 +136,11 @@ class App extends Component {
selected={selected}
onBgOptionChange={this.onBgOptionChange}
>
<div style={{ height: 30 }}>
<InlineAlert intent='warning' marginLeft={20}>
Changes will affect all warehouses
{errMessage && <div style={{ height: 30 }}>
<InlineAlert intent='warning' marginLeft={20} marginRight={20}>
{errMessage}
</InlineAlert>
</div>
</div>}
</ConfigMenu>
</div>
) : null
Expand Down
34 changes: 1 addition & 33 deletions src/components/LoadedVerses.jsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
import React, { Component } from 'react'
import { load } from '../utils/jinrishici'
import { Icon } from 'evergreen-ui'
import Storager from '../utils/storager'
let shici = require('../utils/shici.json')

export default class LoadedVerses extends Component {
constructor (props) {
super()
this.state = {
verses: {
content: '红豆生南国,春来发几枝。',
origin: {
author: '王维',
title: '相思'
}
}
}
}

componentDidMount () {
// fetch a verse from storage
Storager.get(['verses'], res => {
res.verses && this.setState({ verses: res.verses })
})
// fetch verse from jinrishici

load(result => {
Storager.set({ verses: result.data })
}, result => {
console.log(result)
const localShici = shici[Math.floor(Math.random() * shici.length)]
Storager.set({ verses: localShici })
})
}

render () {
const { content, origin } = this.state.verses
const { content, origin } = this.props.verses
return (
<div className={`${this.props.className} verses`}>
<div id='verses-content'>
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5141,11 +5141,6 @@ jest@^23:
import-local "^1.0.0"
jest-cli "^23.6.0"

jinrishici@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/jinrishici/-/jinrishici-1.0.6.tgz#1f3e1259de5e889ce1bab00f118248c7c346bd98"
integrity sha512-4Bk5VHo6xg9DspGS8y8I7D6kftA6DYy4rSvwpdtxFvh9DidXMgf+NSZKAXrcLTi8S5NjqmwkpAKYPHXRURH64Q==

js-levenshtein@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
Expand Down

0 comments on commit 141e85c

Please sign in to comment.