1.0.7 • Published 10 years ago

state-interface v1.0.7

Weekly downloads
9
License
MIT
Repository
github
Last release
10 years ago

state-interface

!Deprecated: Use reducer-interface instead.

Installation

$ npm install state-interface --save

Usage

import {
  ADD_USER,
  REMOVE_USER,
  SWITCH_USER
} from "actions/users"
import initialStateInterface from "interfaces/users"

export default function users (state = initialStateInterface, action) {
  switch (action.type) {
    case ADD_USER:
      return state.update((state) => {
        return state.addUser(action.user)
      })
    case REMOVE_USER:
      return state.update((state) => {
        return state.removeUser(action.id)
      })
    case SWITCH_USER:
      return state.update((state) => {
        return state
          .removeUser(action.id)
          .addUser(action.user)
      })
    default:
      return state
  }
}

Don't care about what returns in update, each state is unique.

Interfaces

import stateInterface from "state-interface"

const initialState = {
  users: [ ],
  count: 0
}

class UsersInterface {
  addUser (user) {
    this.users.push(user)

    return this.updateCount()
  }

  removeUser (id) {
    const userIndex = this.users.findIndex((user) => user.id === id)

    this.users.splice(userIndex, 1)

    return this.updateCount()
  }

  updateCount () {
    this.count = this.users.length

    return this
  }
}

export default stateInterface(UsersInterface, initialState)

See also

Red Hot Chili Peppers - Californication - Live at Slane Castle

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.4

10 years ago