Skip to content

Instantly share code, notes, and snippets.

Avatar

Pochen Lin nightspirit

  • Skupos Inc.
  • United States
View GitHub Profile
View tokenReducer.js
import { createReducer } from 'Reducers/helpers'
import merge from 'lodash-es/merge'
const initState = {
accessToken: null,
refreshToken: null
}
// handlers
function set (state, { accessToken, refreshToken }) {
View Bid, Leaderboard system
# Use ZSET (sorted set) to achieve ranking
###################################
$ ZADD item:1 100 user:1 210 user:2
$ ZADD item:1 300 user:3 350 user:4
$ ZINCRBY item:1 100 user:3
# user:3 400, user:4 350, user:2 210, user:1 100
$ ZREVRANGE item:1 0 0
View testcafe-example.js
import { Selector } from 'testcafe'
fixture `Login`
.page `http://localhost:3000/login`
test('Login with bad credential', async t => {
await t
.typeText('input[name="username"]', 'blah')
.typeText('input[name="password"]', 'blah')
.click('button[type="submit"]')
View Counter.js
import React from 'react'
import { Provider } from './context'
class Counter extends React.Component {
constructor (props) {
super(props)
this.state = {
counter: {
value: 0,
View CounterCard.js
import React from 'react'
import CounterDisplay from './CounterDisplay'
import CounterIncBtn from './CounterIncBtn'
import CounterDecBtn from './CounterDecBtn'
const CounterCard = () => (
<div className='Counter card m-3'>
<div className='card-body'>
<CounterDisplay/>
<div className='row'>
View CounterDisplay.js
import React from 'react'
import { withCounter } from './context'
const Display = ({ counter }) => <h5 className="card-title text-center">{counter.value}</h5>
const CounterDisplay = withCounter(Display)
export default CounterDisplay
@nightspirit
nightspirit / CounterIncBtn .js
Created Apr 15, 2018
react context example
View CounterIncBtn .js
import React from 'react'
import { withCounter } from './context'
const IncBtn = ({ counter }) => <button type='button' className='btn btn-primary btn-block' onClick={counter.inc}>+</button>
const CounterIncBtn = withCounter(IncBtn)
export default CounterIncBtn
@nightspirit
nightspirit / context.js
Created Apr 15, 2018
react context example
View context.js
import React from 'react'
const ctx = React.createContext()
export const {Provider, Consumer} = ctx
export const withCounter = Component => props => <Consumer>{value => <Component {...props} counter={value} />}</Consumer>
export default ctx
View componentDidUpdate.js
componentDidUpdate(prevProps, prevState) {
if (this.props.reqPending !== prevProps.reqPending && !this.props.reqPending) {
if(!this.props.error){
this.props.history.push('/next-page')
}
}
}
View getDerivedStateFromProps.js
// before
componentWillReceiveProps(nextProps) {
if (this.props.currentRow !== nextProps.currentRow) {
this.setState({
isScrollingDown: nextProps.currentRow > this.props.currentRow
})
}
}
// after