This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from HaNdTriX/master
Updated redux example by @HaNdTriX
- Loading branch information
Showing
5 changed files
with
150 additions
and
175 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,26 +1,17 @@ | ||
/* global window */ | ||
import {createStore, applyMiddleware} from 'redux' | ||
import thunkMiddleware from 'redux-thunk' | ||
|
||
export const reducer = (state = {lastUpdate: 0, light: false}, action) => { | ||
export const reducer = (state = { lastUpdate: 0, light: false }, action) => { | ||
switch (action.type) { | ||
case 'TICK': return {lastUpdate: action.ts, light: Boolean(action.light)} | ||
case 'TICK': return { lastUpdate: action.ts, light: !!action.light } | ||
default: return state | ||
} | ||
} | ||
|
||
export const startClock = () => dispatch => { | ||
setInterval(() => dispatch({type: 'TICK', light: true, ts: Date.now()}), 800) | ||
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800) | ||
} | ||
|
||
export const initStore = (reducer, initialState, isServer) => { | ||
if (isServer && typeof window === 'undefined') { | ||
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) | ||
} | ||
|
||
if (!window.store) { | ||
window.store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) | ||
} | ||
|
||
return window.store | ||
export const initStore = (initialState) => { | ||
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) | ||
} |
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,22 +1,23 @@ | ||
import React from 'react' | ||
import {connect} from 'react-redux' | ||
import {style, merge} from 'next/css' | ||
const format = t => `${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}` | ||
|
||
const pad = n => n < 10 ? `0${n}` : n | ||
|
||
const format = t => `${pad(t.getHours())}:${pad(t.getMinutes())}:${pad(t.getSeconds())}` | ||
|
||
const styles = style({ | ||
padding: '15px', | ||
display: 'inline-block', | ||
color: '#82FA58', | ||
font: '50px menlo, monaco, monospace' | ||
}) | ||
|
||
export default connect(state => state)(({lastUpdate, light}) => { | ||
export default ({ lastUpdate, light }) => { | ||
return ( | ||
<div className={merge(styles, style({backgroundColor: light ? '#999' : '#000'}))}> | ||
<div className={light ? 'light' : ''}> | ||
{format(new Date(lastUpdate))} | ||
<style jsx>{` | ||
div { | ||
padding: 15px; | ||
display: inline-block; | ||
color: #82FA58; | ||
font: 50px menlo, monaco, monospace; | ||
background-color: #000; | ||
} | ||
.light { | ||
background-color: #999; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
}) | ||
} |
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.