Skip to content

Instantly share code, notes, and snippets.

@dilani14
dilani14 / employee.ts
Last active April 19, 2019 06:00
Model class for employee
export interface Employee {
id: number;
name: string;
age: string;
salary: string;
isPermanent: boolean;
}
@dilani14
dilani14 / state.ts
Last active May 4, 2019 11:45
app state
import { Employee } from '../models/employee';
import { EmployeeFilterOptions } from './actions/employee.action';
export interface IAppState {
employees: Employee[];
filterOption: EmployeeFilterOptions;
}
@dilani14
dilani14 / state.ts
Last active May 4, 2019 11:56
initial state
import { Employee } from '../models/employee';
import { EmployeeFilterOptions } from './actions/employee.action';
export interface IAppState {
employees: Employee[];
filterOption: EmployeeFilterOptions;
}
export const INITIAL_STATE: IAppState = {
employees: null,
@dilani14
dilani14 / app.reducer.ts
Last active May 4, 2019 12:56
define reducer
import { INITIAL_STATE } from '../app.state';
export function appReducer(state = INITIAL_STATE, action) {
return state;
};
@dilani14
dilani14 / employee.actions.ts
Last active May 18, 2019 10:41
Add Employee Action
import { Injectable } from '@angular/core';
import { AnyAction } from 'redux';
import { Employee } from '../models/employee';
@Injectable()
export class AppActions {
static ADD_EMPLOYEE = 'ADD_EMPLOYEE';
addEmployee(emp: Employee): AnyAction {
return { type: AppActions.ADD_EMPLOYEE, employee: emp };
@dilani14
dilani14 / employee.action.ts
Last active May 18, 2019 10:50
Delete employee action
import { Injectable } from '@angular/core';
import { AnyAction } from 'redux';
import { Employee } from '../models/employee';
@Injectable()
export class AppActions {
static ADD_EMPLOYEE = 'ADD_EMPLOYEE';
deleteEmployee(empId: number): AnyAction {
return { type: AppActions.DELETE_EMPLOYEE, id: empId };
@dilani14
dilani14 / employee.action.ts
Last active May 18, 2019 10:54
Final Employee Actions
import { Injectable } from '@angular/core';
import { AnyAction } from 'redux';
import { Employee } from '../models/employee';
@Injectable()
export class AppActions {
static SET_EMPLOYEE_FILTER = 'SET_EMPLOYEE_FILTER';
static ADD_EMPLOYEE = 'ADD_EMPLOYEE';
static DELETE_EMPLOYEE = 'DELETE_EMPLOYEE';
@dilani14
dilani14 / employee.action.ts
Last active May 18, 2019 10:55
Add employee filter action
import { Injectable } from '@angular/core';
import { AnyAction } from 'redux';
import { Employee } from '../models/employee';
@Injectable()
export class AppActions {
static SET_EMPLOYEE_FILTER = 'SET_EMPLOYEE_FILTER';
setEmployeeFilter(filter: string) {
return { type: AppActions.SET_EMPLOYEE_FILTER, option: filter };
@dilani14
dilani14 / app.reducer.ts
Last active May 18, 2019 10:59
Set filter option
import { Employee } from '../models/employee';
import { AppActions, EmployeeFilterOptions } from './app.actions';
export function appReducer(state = INITIAL_STATE, action) {
switch (action.type) {
case AppActions.SET_EMPLOYEE_FILTER:
return Object.assign({}, state, {
filterOption: action.option
} as IAppState);
@dilani14
dilani14 / app.store.ts
Created May 18, 2019 11:03
Add Employee reducer
import { Employee } from '../models/employee';
import { AppActions, EmployeeFilterOptions } from './app.actions';
export function appReducer(state = INITIAL_STATE, action) {
switch (action.type) {
case AppActions.ADD_EMPLOYEE:
action.employee.id = state.employees.length + 1;
return Object.assign({}, state, {
employees: state.employees.concat(Object.assign({}, action.employee))
} as IAppState);