Skip to content

Instantly share code, notes, and snippets.

View webmasterdevlin's full-sized avatar

Devlin Duldulao webmasterdevlin

View GitHub Profile
@webmasterdevlin
webmasterdevlin / index.js
Last active March 14, 2019 23:56
NgRx Selectors and Store : src/app/reducers/index.ts
import { ActionReducerMap, createSelector, MetaReducer } from "@ngrx/store";
import { environment } from "../../environments/environment";
import { heroReducer, HeroState } from "./hero.reducer";
import { villainReducer, VillainState } from "./villain.reducer";
export interface State {
heroes: HeroState;
villains: VillainState;
}
@webmasterdevlin
webmasterdevlin / hero.effects.ts
Last active March 15, 2019 01:16
NgRx Effects : src/app/effects/hero.effects.ts
import { Injectable } from "@angular/core";
import { Actions, Effect, ofType } from "@ngrx/effects";
import { HeroService } from "../heroes/hero.service";
import { Observable, of } from "rxjs";
import { Action } from "@ngrx/store";
import * as heroActions from "../actions/hero.actions";
import {
catchError,
exhaustMap,
map,
@webmasterdevlin
webmasterdevlin / hero.reducer.ts
Last active March 14, 2019 08:58
NgRx Reducer : src/app/reducers/hero.reducer.ts
import { HeroActions, HeroActionTypes } from "../actions/hero.actions";
import { Hero } from "../models/hero.model";
export interface HeroState {
heroes: Hero[];
hero: Hero;
requesting: boolean;
error: string;
}
@webmasterdevlin
webmasterdevlin / hero.actions.ts
Last active March 14, 2019 08:23
NgRx Actions : src/app/actions/hero.actions.ts
import { Action } from "@ngrx/store";
import { Hero } from "../models/hero.model";
/*
* Action Types
*/
export enum HeroActionTypes {
LoadHeroes = "[Hero] Load Heroes",
LoadHeroesSuccess = "[Hero] Load Heroes Success",
LoadHeroesFail = "[Hero] Load Heroes Fail",
@webmasterdevlin
webmasterdevlin / index.js
Created March 14, 2019 02:42
React Class Component : src/index.js
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { Provider } from "react-redux"; // holds that store
import "bootstrap/dist/css/bootstrap.css";
import "@fortawesome/fontawesome-free/css/all.css";
@webmasterdevlin
webmasterdevlin / Heroes.js
Last active March 14, 2019 02:06
React Class Component : src/pages/heroes/Heroes.js
import React, { Component } from "react";
import NewItemForm from "../../common-components/NewItemForm";
import {
deleteHero,
loadHeroes,
postHero
} from "../../store/hero/hero-actions";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
@webmasterdevlin
webmasterdevlin / index.js
Last active March 13, 2019 23:48
Redux Store : src/store/index.js
import { combineReducers, createStore, applyMiddleware } from "redux";
import { heroReducer } from "./hero/hero-reducer";
import { villainReducer } from "./villain/villain-reducer";
import thunk from "redux-thunk"; // 3rd-party side-effects library.
import { composeWithDevTools } from "redux-devtools-extension"; // Redux devtools for time travel debugging.
/*
Merge all your reducers here. Name it rootReducer.
*/
@webmasterdevlin
webmasterdevlin / hero-reducer.js
Last active March 14, 2019 02:55
Redux Reducer : src/store/hero/hero-reducer.js
import * as types from "./hero-actions";
/*
initalize your state with default values
*/
let initialState = {
heroes: [],
fetching: false,
error: ""
};
@webmasterdevlin
webmasterdevlin / hero-actions.js
Last active March 14, 2019 00:58
Redux Actions : src/store/hero/hero-actions.js
import { addHero, getHeroes, removeHero, updateHero } from "./hero-service"; // services of hero module
/*
* action types
*/
export const LOAD_HEROES_REQUEST = "LOAD_HEROES_REQUEST";
export const LOAD_HEROES_SUCCESS = "LOAD_HEROES_SUCCESS";
export const LOAD_HEROES_FAIL = "LOAD_HEROES_FAIL";
export const CREATE_HERO_REQUEST = "CREATE_HERO_REQUEST";
@webmasterdevlin
webmasterdevlin / router.tsx
Last active March 13, 2019 17:15
React Router : src/router.tsx
import React from "react";
import { Redirect, Route, Switch } from "react-router";
import Heroes from "./pages/heroes/Heroes";
import EditHero from "./pages/heroes/EditHero";
import Villains from "./pages/villains/Villains";
import EditVillain from "./pages/villains/EditVillain";
import createBrowserHistory from "history/createBrowserHistory";
import { RouterStore, syncHistoryWithStore } from "mobx-react-router";
import { Provider } from "mobx-react";