1.5.9 • Published 10 months ago

@sygnalgroup/react-sg-modules v1.5.9

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

react-sg-modules

Easy way to handle react-redux with redux-sagas and reduxsauce

With this package you can easily configure and use react-redux with redux-sagas, you can also use the pact without integrating your requests, it can only be used as a store

NPM

Example use in a crud

Crud with react-sg-modules

Install

npm i @sygnalgroup/react-sg-modules

Usage/Examples

If you will use async requests, can set the api base url from in setApiBaseUrl method.

import { setApiBaseUrl } from '@sygnalgroup/react-sg-modules';

setApiBaseUrl(BASE_URL_API);

Customize the api auth keys - this keys the lib auto persist in the headers and always update.

DEFAULT API AUTH KEYS - 'uid', 'access-token', 'expiry', 'client';

If you want costumize the headers keys, you need export authHeaders from modules/index.js in your project

const authHeaders = ['uid', 'access-token', 'expiry', 'client'];

export { authHeaders };

The lib use localStorage to save the keys and use in the request headers, if you want set manually the keys, use the method to persist in local storage

import { persistData, removeData, retrieveData } from '@sygnalgroup/react-sg-modules';

Provider

import { Provider } from '@sygnalgroup/react-sg-modules';

<Provider>
  <App />
</Provider>

MODULES

CREATE MODULE - TODO

todo/index.js

import api from 'core/api';

export const todoModule = 'todo';

const actions = {
  getTodoList: {
    api: () => api.get('/todo'),
    action: {
      error: ['error'],
      success: ['data'],
    },
    *sagas(Creators, { params }) { // OPTIONAL METHOD - THE DEFAULT CALL (SUCCESS OR ERROR)
      try {
        const resp = yield call(actions.getChannels.api);
        yield put(Creators.getTodoListSuccess(resp.data));
      } catch (error) {
        yield put(Creators.getTodoListError(getErrorMessage(error)));
      }
    },
    state: { // STATES TO CHANGE IN REDUCERS ACTIONS
      start: { loadingTodoList: true },
      error: { loadingTodoList: false },
      success: { loadingTodoList: false },
    },
    isTakeEvery: true, // IF YOU WANT ALWAYS EXECUTE THE REQUEST, OTHER WISE WILL CANCEL DUPLICATED REQUESTS, DEFAULT=FALSE
  },
};

export default {
  actions,
  state: { // ALL STATES FROM THE MODULE
    loadingTodoList: false,
    data: [],
  },
};


OR

import api from 'core/api';

export const todoModule = 'todo';

const actions = {
  getTodoList: {
    api: () => api.get('/todo'),
    action: {
      error: ['error'],
      success: ['data'],
    },
  },
};

export default {
  actions,
  state: {
    data: [],
  },
};

Create a file in src/modules/index.js and import the modules

modules/index.js

import todo from './todo/index';

const Modules = {
  todo,
};

export default Modules;

USAGE ACTIONS AND SELECTORS

import React, { useEffect } from 'react';
import Modules from 'modules';
import useActions from 'modules/map/useActions';
import useSelectors from 'modules/map/useSelectors';
import { todoModule } from 'modules/todo';

const TodoList = () => {
  const { dispatch } = useActions();
  const { data } = useSelectors(todoModule);
  const load = () => {
    dispatch({
      action: Modules.todo.actions.getTodoList,
      data: {},
      options: {
        onSuccess: () => {},
        onError: () => {},
      },
    });
  };

  useEffect(() => {
    load();
  }, []);

  return <div>{data && data.map((item) => <div>{item.name}</div>)}</div>;
};

export default TodoList;

EXPORT MODULES

export {
  Provider,
  history,
  useActions,
  useSelectors,
  ReducersProvider,
  api,
  axios,
  retrieveAuthHeaders,
  persistData,
  removeData,
  retrieveData,
  clearAuthHeaders,
  setApiBaseUrl,
  ReactReduxContext
}

If you want add moddlewares in redux store you can add this method storeMiddlewares in your modules.js, this method must return a array

the package will import this function from your project and add the middlewares in the store.

EXAMPLE - routerMiddleware from connected-react-router

export const storeMiddlewares = (history) => [routerMiddleware(history)];

USE MODULE WITHOUT REQUESTS - REDUX STORE MODULE

EXAMPLE app.js module

export const appModule = 'app';

const actions = {
  setTitle: {
    action: {
      success: ['title'],
    },
  },
};

const app = {
  actions,
  state: {
    title: 'My App',
  },
}

export default app;

USAGE

const { dispatch } = useActions();
const { title } = useSelectors(appModule);

useEffect(() => {
  dispatch({
    action: Modules.app.actions.setTitle,
    data: 'Posts Title'
  })
}, [dispatch]);

License

MIT © sygnalgroup

1.5.9

10 months ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.6

2 years ago

1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago