Skip to content

Instantly share code, notes, and snippets.

View brakmic's full-sized avatar
🏠

Harris Brakmić brakmic

🏠
View GitHub Profile
@brakmic
brakmic / vr_module_reducer.ts
Created March 14, 2017 14:56
vr module reducer
const vrModuleReducer: ActionReducer<IVrModule[]> = (state: IVrModule[] =
initialState, action: Action) => {
switch (action.type) {
case VR_MODULE_REMOVED:
return _.filter(state, (mod) => mod.id === action.payload.id);
case VR_MODULE_ADDED:
return _.concat(state, action.payload);
default:
return state;
}
@brakmic
brakmic / provide_store.ts
Created March 14, 2017 15:15
provide a reducer store
const appStore = provideStore(
{
vrModule: vrModuleReducer
},
{
vrModule: this.vrModule
}
);
@brakmic
brakmic / state_management_with_redux.ts
Last active March 14, 2017 15:19
state management
/**
* Initialize subscriptions and get app state via @ngrx (Redux implementation for Angular2)
*/
public ngOnInit() {
[....snip...]
this.availableModules = <Observable<IVrModule[]>> this.store.select('vrModule');
[....snip...]
}
@brakmic
brakmic / declare_actions.ts
Created April 11, 2017 07:52
declare a set of actions
export interface ICustomerActions {
INIT: string;
INIT_FAILED: string;
INITIALIZED: string;
SELECTED: string;
CHANGED: string;
SAVED: string;
DELETED: string;
}
@brakmic
brakmic / define_action_classes.ts
Last active April 11, 2017 08:10
define action classes
export class InitCustomerAction implements Action {
type = CustomerActionTypes.INIT;
payload: ICustomer = null;
}
export class InitializedCustomerAction implements Action {
type = CustomerActionTypes.INITIALIZED;
constructor(public payload: ICustomer) { }
}
@brakmic
brakmic / define_union_type_for_actions.ts
Created April 11, 2017 08:16
group thematically related actions together
export type CustomerAction
= InitCustomerAction
| InitializedCustomerAction
| InitFailedCustomerAction
| CustomerSelectedAction
| CustomerChangedAction
| CustomerDeletedAction
| CustomerSavedAction;
@brakmic
brakmic / customer_action_creator.ts
Created April 11, 2017 08:41
action creator for customer actions
import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { ICustomer } from '../../interfaces';
import * as customer from '../customer.actions';
@Injectable()
export class CustomerActions {
public customerInit() {
return new customer.InitCustomerAction();
}
@brakmic
brakmic / customer_reducer.ts
Created April 11, 2017 09:07
reducer for handling customer actions
import { ICustomerState, initialCustomerState } from '../states/sub-states';
import { CustomerAction, CustomerActionTypes } from '../actions';
export function customerReducer(
state: ICustomerState = initialCustomerState,
action: CustomerAction
): ICustomerState {
switch (action.type) {
case CustomerActionTypes.INITIALIZED:
return (<any>Object).assign({}, state, {
@brakmic
brakmic / modify_part_of_a_state.ts
Created April 11, 2017 09:16
modifying part of a state
return (<any>Object).assign({}, state, {
customer: action.payload
});
@brakmic
brakmic / app_state.ts
Last active April 11, 2017 10:14
application state
// Application state is like a database.
// Currently our 'database' comprises of a single table
// that can be modified by a certain reducer.
// The sub-state's name in IAppState corresponds to
// reducer's name in reducers object.
export interface IAppState {
customerState: fromSubstates.ICustomerState
};
// and we have only a single reducer here