Skip to content

Instantly share code, notes, and snippets.

View webmasterdevlin's full-sized avatar
👨‍💻
coding 24/7

Devlin Duldulao webmasterdevlin

👨‍💻
coding 24/7
View GitHub Profile
@webmasterdevlin
webmasterdevlin / types.js
Last active March 13, 2019 02:35
Vuex Types : src/store/types.js
// Getters
export const GETTERS_INIT_HEROES = "GETTERS_INIT_HEROES";
export const GETTERS_INIT_HERO = "GETTERS_INIT_HERO";
// Mutations
export const MUTATE_GET_HEROES = "MUTATE_GET_HEROES";
export const MUTATE_GET_HERO = "MUTATE_GET_HERO";
export const MUTATE_ADD_HERO = "MUTATE_ADD_HERO";
export const MUTATE_UPDATE_HERO = "MUTATE_UPDATE_HERO";
export const MUTATE_REMOVE_HERO = "MUTATE_REMOVE_HERO";
@webmasterdevlin
webmasterdevlin / mutations.js
Last active March 13, 2019 02:36
Vuex Mutations : src/store/modules/heroes/mutations.js
import * as types from "../../types";
const mutations = {
[types.MUTATE_GET_HEROES](state, heroes) {
state.heroes = heroes.reverse(); // reverse() reverses the order of the elements in an array
},
[types.MUTATE_GET_HERO](state, hero) {
state.hero = hero;
},
@webmasterdevlin
webmasterdevlin / actions.js
Created March 13, 2019 02:29
Vuex Actions: src/store/modules/heroes/actions.js
import * as types from "../../types";
import heroesService from "../../../services/heroes-services";
const actions = {
[types.ACTION_GET_HEROES]({ commit }) {
return new Promise(resolve => {
heroesService
.getHeroes() // getHeroes() is a method in my HeroesServices that sends get request to a backend service.
.then(data => {
commit(types.MUTATE_GET_HEROES, data); // commit does the changes in your state.
@webmasterdevlin
webmasterdevlin / state.js
Last active March 13, 2019 02:43
Vuex State : src/store/modules/heroes/state.js
const state = {
heroes: [], // this heroes array must be initialized with and empty array.
hero: {} // this hero object must be initialized with and empty object.
};
export default state;
@webmasterdevlin
webmasterdevlin / index.js
Created March 13, 2019 03:37
Vuex Store : src/store/index.js
import Vue from "vue"; // The Vue library
import Vuex from "vuex"; // The Vuex library
import heroes from "./modules/heroes"; // heroes module
import villains from "./modules/villains"; // villains is another module
Vue.use(Vuex); // using Vuex
export default new Vuex.Store({
modules: {
@webmasterdevlin
webmasterdevlin / getters.js
Created March 13, 2019 03:44
Vuex Getters : src/store/modules/heroes/getters.js
import * as types from "../../types";
const getters = {
[types.GETTERS_INIT_HEROES]: state => state.heroes, // retrieves the current heroes array from the state
[types.GETTERS_INIT_HERO]: state => state.hero // retrieves the current hero object from the state
};
export default getters;
@webmasterdevlin
webmasterdevlin / Heroes.vue
Last active March 13, 2019 04:47
Using mapGetters and mapActions in a Vue Component : src/views/Heroes.vue
<template>
<div class="container-fluid">
<NewItemForm
:isShowNewItemForm="isShowNewItemForm"
:newItem="newItem"
@handleSubmit="onSubmit"
@handleShowNewItemForm="showNewItemForm"
@handleCancelForm="cancelForm"
></NewItemForm>
@webmasterdevlin
webmasterdevlin / hero.store.ts
Last active March 13, 2019 09:04
Mobx Store : src/stores/hero.store.ts
import { decorate, observable, action } from "mobx";
import { Hero } from "../models/hero";
import {
addHero,
getHero,
getHeroes,
removeHero,
updateHero
} from "./hero-service";
@webmasterdevlin
webmasterdevlin / Heroes.tsx
Last active March 13, 2019 15:58
React Class Component : src/pages/heroes/Heroes.tsx
import * as React from "react";
import { Hero } from "../../models/hero";
import NewItemForm from "../../common-components/NewItemForm";
import heroStore from "./../../stores/hero.store";
import { NavLink, Link } from "react-router-dom";
import { inject, observer } from "mobx-react";
import { toJS } from "mobx";
import { render } from "react-dom";
@webmasterdevlin
webmasterdevlin / router.tsx
Created March 13, 2019 09:38
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 heroStore from "./stores/hero.store";