Skip to content

Instantly share code, notes, and snippets.

@scrapcupcake
Last active January 23, 2016 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrapcupcake/81220eee0652805771c9 to your computer and use it in GitHub Desktop.
Save scrapcupcake/81220eee0652805771c9 to your computer and use it in GitHub Desktop.
import {createStore} from 'redux';
import React from 'react';
export default class Need {
static PropTypes = {
exampleImage : React.PropTypes.string.isRequired,
name : React.PropTypes.string.isRequired,
description : React.PropTypes.string.isRequired,
exampleUrl : React.PropTypes.string.isRequired,
examplePrice : React.PropTypes.number.isRequired
}
constructor(input){
Object.assign(this, Need.filterProps(input));
}
static default(args={
name: "",
description: "",
exampleUrl: "",
exampleImage: "",
examplePrice: 2000
}){
return new Need(Need.filterProps(args));
}
static filterProps(input){
let filter = function(k){ if(input.hasOwnProperty(k)){ return input[k] }};
return Need.PropTypes.keys.reduce(filter, {});
}
get id(){
return this.name;
}
///List Actions
//Add
static AddAction = "ADD_NEED";
static addAction(need){
return {
type: Need.AddAction,
need
}
}
addAction()
{
return Need.addAction(this)
}
//Remove
static RemoveAction = "REMOVE_NEED";
static removeAction(need){
return {
type: Need.RemoveAction,
need
}
}
removeAction(){
return Need.removeAction(this)
}
//Update
static UpdateAction = "UPDATE_NEED";
static updateNeed(original_need, need_changes){
return {
type: Need.UpdateAction,
original: original_need,
need: need_changes
}
}
updateNeed(need_change){
return Need.updateNeed(this, need_change)
}
static listReducer( state = [Need.default()], action){
switch(action.type){
case Need.AddAction:
if(action.need){
return [...state, new Need(action.need)]
}else{
return [...state, new Need()]
}
case Need.RemoveAction:
if(action.need){
let index = state.indexOf(action.need)
if(index > -1){
return [...state.slice(0,index),...state.slice(index+1)]
}
}
return state;
case Need.UpdateAction:
if(action.original){
let index = state.indexOf(action.original)
if(index > -1){
return [...state.slice(0,index), new Need(action.need), ...state.slice(index+1)]
}
}
return state;
default:
return state;
}
}
static store(){
return createStore(Need.listReducer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment