Skip to content

Instantly share code, notes, and snippets.

View webmasterdevlin's full-sized avatar

Devlin Duldulao webmasterdevlin

View GitHub Profile
@webmasterdevlin
webmasterdevlin / Heroes.js
Created November 17, 2019 00:49
Component
import React, { useState, useEffect } from "react";
import NewItemForm from "../../shared/components/NewItemForm";
import { Link } from "@reach/router";
import HeroStore from "../hero-store";
export default function Heroes() {
/*part of the Easy-Peasy pattern*/
const { heroes, hero, isLoading } = HeroStore.useStoreState(
state => state
);
import { action, computed, createContextStore, thunk } from 'easy-peasy'
import { deleteHero, getHeroById, getHeroes, postHero, putHero } from './hero-service'
const HeroStore = createContextStore({
/*states here*/
/*actions thunk side effects here*/
/*actions here*/
/*computed values i.e. derived state*/
totalHeroes: computed(state => Object.values(state.heroes).length)
});
@webmasterdevlin
webmasterdevlin / hero-store.js
Created November 16, 2019 23:55
createContextStore
import { action, computed, createContextStore, thunk } from 'easy-peasy'
import { deleteHero, getHeroById, getHeroes, postHero, putHero } from './hero-service'
const HeroStore = createContextStore({
/*states here*/
/*actions thunk side effects here*/
/*actions here*/
/*computed values i.e. derived state here*/
});
@webmasterdevlin
webmasterdevlin / hero-store.js
Created November 16, 2019 23:48
Adding actions to your model
import { action, computed, createContextStore, thunk } from 'easy-peasy'
import { deleteHero, getHeroById, getHeroes, postHero, putHero } from './hero-service'
const HeroStore = createContextStore({
/*states here*/
/*actions thunk side effects here*/
/*actions*/
setHeroes: action((state, heroes) => {
state.heroes = heroes;
}),
@webmasterdevlin
webmasterdevlin / hero-store.js
Last active November 16, 2019 23:49
Add thunks to perform side effects
import { action, computed, createContextStore, thunk } from 'easy-peasy'
import { deleteHero, getHeroById, getHeroes, postHero, putHero } from './hero-service'
const HeroStore = createContextStore({
/*states here*/
/*actions thunk side effects*/
getHeroes: thunk(async actions => {
actions.setIsLoading();
try {
const { data } = await getHeroes();
@webmasterdevlin
webmasterdevlin / hero-store.js
Last active November 16, 2019 23:42
Easy Peasy State
import { action, computed, createContextStore, thunk } from 'easy-peasy'
import { deleteHero, getHeroById, getHeroes, postHero, putHero } from './hero-service'
const HeroStore = createContextStore({
/*states*/
heroes: [],
hero: {
id: "",
firstName: "",
lastName: "",
@webmasterdevlin
webmasterdevlin / heroes.component.ts
Created July 28, 2019 14:36
src/app/heroes/container/heroes.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HeroModel } from '../hero.model';
import { HeroesQuery } from '../heroes.query';
import { HeroesService } from '../heroes.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
@webmasterdevlin
webmasterdevlin / app.module.ts
Created July 28, 2019 14:16
Akita Module: src/app/app.module.ts
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderNavComponent } from './shared/components/header-nav.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';
import { environment } from '../environments/environment';
@webmasterdevlin
webmasterdevlin / heroes.service.ts
Created July 28, 2019 13:58
Akita Service: src/app/heroes/heroes.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { HeroModel } from './hero.model';
import { throwError } from 'rxjs';
import { BaseUrl } from '../shared/api.config';
import { HeroesStore } from './heroes.store';
import { catchError } from 'rxjs/operators';
import { ID } from '@datorama/akita';
@Injectable({
@webmasterdevlin
webmasterdevlin / heroes.query.ts
Created July 28, 2019 13:16
Akita Query: src/app/heroes/heroes.query.ts
import { Injectable } from '@angular/core';
import { QueryEntity } from '@datorama/akita';
import { HeroesStore } from './heroes.store';
import { HeroesState, HeroModel } from './hero.model';
@Injectable({ providedIn: 'root' })
export class HeroesQuery extends QueryEntity<HeroesState, HeroModel> {
constructor(protected heroesStore: HeroesStore) {
super(heroesStore);
}