Skip to content

Instantly share code, notes, and snippets.

@KhalilZaidoun
Created February 23, 2017 07:47
Show Gist options
  • Save KhalilZaidoun/0e48a7142cdcabd9435485207953a680 to your computer and use it in GitHub Desktop.
Save KhalilZaidoun/0e48a7142cdcabd9435485207953a680 to your computer and use it in GitHub Desktop.
Reactive store helper In Angular2 [ From Angular-class]
import { Injectable } from '@angular/core';
import { Store } from '../store';
@Injectable()
export class StoreHelper {
constructor(private store: Store) {}
update(prop, state) {
const currentState = this.store.getState();
this.store.setState(Object.assign({}, currentState, { [prop]: state }));
}
add(prop, state) {
const currentState = this.store.getState();
const collection = currentState[prop];
this.store.setState(Object.assign({}, currentState, { [prop]: [state, ...collection] }));
}
findAndUpdate(prop, state) {
const currentState = this.store.getState();
const collection = currentState[prop];
this.store.setState(Object.assign({}, currentState, {[prop]: collection.map(item => {
if (item.id !== state.id) {
return item;
}
return Object.assign({}, item, state)
})}))
}
findAndDelete(prop, id) {
const currentState = this.store.getState();
const collection = currentState[prop];
this.store.setState(Object.assign({}, currentState, {[prop]: collection.filter(item => item.id !== id)}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment