Skip to content

Instantly share code, notes, and snippets.

View baso53's full-sized avatar
🎃

Sebastijan Grabar baso53

🎃
  • Croatia
View GitHub Profile
@baso53
baso53 / reducer.js
Created October 22, 2018 13:32
implementing-sagas-reducer-1.js
import { combineReducers } from "redux";
const actionTypes = {
"FETCH_DATA_REQUEST": "FETCH_DATA_REQUEST",
"FETCH_DATA_SUCCESS": "FETCH_DATA_SUCCESS",
"FETCH_DATA_FAILED": "FETCH_DATA_FAILED"
};
const initialState = {
name: '',
@baso53
baso53 / store.js
Created October 22, 2018 13:33
implementing-sagas-store-1.js
import { createStore, applyMiddleware } from "redux";
import reducer from "./reducer";
const store = createStore(reducer, applyMiddleware());
export default store;
@baso53
baso53 / loggingMiddleware.js
Last active October 22, 2018 19:14
implementing-sagas-logging-middleware-1
const middleware = store => next => action => {
console.log(action.type);
}
@baso53
baso53 / middleware.js
Last active October 22, 2018 19:15
implementing-sagas-middleware-1
export default class MySaga {
constructor() {
this.actionWatchList = new Map();
}
registerAction(actionType, handler) {
this.actionWatchList.set(actionType, handler);
}
};
@baso53
baso53 / middleware.js
Last active October 22, 2018 19:15
implementing-sagas-middleware-2
...
middleware = store => next => action => {
const handler = action.type ? this.actionWatchList.get(action.type) : undefined;
if (handler) {
// do middleware work
}
next(action);
}
...
@baso53
baso53 / middleware.js
Last active October 22, 2018 19:15
implementing-sagas-middleware-3
...
if (handler) {
const handlerInstance = handler(action);
let yieldedValue = handlerInstance.next();
(async () => {
while (!yieldedValue.done) {
switch (yieldedValue.value.effect) {
case 'effect/CALL':
@baso53
baso53 / effects.js
Last active October 22, 2018 19:16
implementing-sagas-effects-1
export const call = (fn, ...args) => {
return {
effect: 'effect/CALL',
payload: fn,
args: args
};
}
export const put = (action) => {
return {
@baso53
baso53 / store.js
Created October 22, 2018 19:10
implementing-sagas-store-2
import { createStore, applyMiddleware } from "redux";
import reducer from "./reducer";
import MySaga from "./middleware";
import { ACTION_TYPES } from './reducer';
const sagaInstance = new MySaga();
sagaInstance.registerAction(ACTION_TYPES.FETCH_DATA_REQUEST, fetchDataWorker);
const store = createStore(reducer, applyMiddleware(sagaInstance.middleware));
@baso53
baso53 / sagas.js
Created October 22, 2018 19:10
implementing-sagas-sagas-1
import axios from 'axios';
import { call, put } from './effects';
const fetchData = URL => axios.get(URL);
export function* fetchDataWorker(action) {
try {
const response = yield call(fetchData, `https://uinames.com/api/?gender=${action.payload}`);
yield put({ type: 'FETCH_DATA_SUCCESS', payload: response.data });
@baso53
baso53 / App.jsx
Last active January 21, 2020 14:35
implementing-sagas-App-1
import React from 'react';
import { connect } from 'react-redux';
import { fetchDataRequest } from './actions';
import "./App.css";
const App = props => (
<React.Fragment>
<div className='button' onClick={() => props.fetchDataRequest('male')} >
Fetch a random male name!