2.24.0-beta.14 • Published 9 months ago

@payping/redux v2.24.0-beta.14

Weekly downloads
137
License
ISC
Repository
-
Last release
9 months ago

This document has been updated to version 2.0.0

What is it

  • this package is meant to build a redux store object with some default configs + payping redux actions & reducers

  • plus it comes with redux-persist integration as an optional feature

  • it uses redux-thunk & promise as default middlewares

    • promise middleware: it allows redux to use promisified actions
  • it also has redux-devtools & redux-logger enabled when NODE_ENV is not equal to "production" "test"

How to use

Initial work:
  1. create a directory called redux in your app root dir and actions, reducers & configs subdirectories in it.

  2. create index.ts file in redux/configs just like the following code:

// root/redux/configs/index.ts

import { SdkConfigureStore } from "@payping/redux/store";

export const reduxConfig: SdkConfigureStore = {};
  1. At the index.tsx file in your app root dir, create store in it by passing reduxConfig to the configureStore() method
// root/index.tsx

import { configureStore } from "@payping/redux";
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import { reduxConfig } from "./redux/configs";

export const { store } = configureStore(reduxConfig);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
  1. export all actions from package in redux/actions/index.ts file:
// root/redux/actions/index.ts

export * from "@payping/redux/actions";
Assumptions:

By combining some of the following assumptions, you can build the store object:

  1. I don't want to use redux-persist and I have no custom redux configs or additional actions & reducers.

  2. I want to use redux-persist.

  3. I have some custom redux configs.

  4. I have some additional actions or reducers.

Pick the instruction you need, from bellow:

Instructions:

whenever you needed to add some reducers to your redux, create a new interface in redux/configs/index.ts, called AppStoreStates which extends SdkStoreStates. then modify configureStore(reduxConfig) to configureStore<AppStoreStates>(reduxConfig). and istead of SdkStoreStates, use AppStoreStates wherever you needed that.

use SdkThunkDispatch type whenever you want to add type for a dispatcher

  • for assumption 1: - you don't need to do anything
  • for assumption 2:
  1. create persistConfig.ts file in redux/configs just like the following code:
// root/redux/configs/persistConfig.ts

import { SdkPersistConfig } from "@payping/redux/store/persistConfig";
// use localForage for web & desktop, and AsyncStorage for react-native
import localForage as storage from "localforage";
// import AsyncStorage as storage from '@react-native-community/async-storage';
import env from "../env";

const { storeVersion } = env;

const persistConfig: SdkPersistConfig = {
  storage,
  version: storeVersion,
};

export default { ...persistConfig };

by default, it create persisted reducer with stateReconciler: autoMergeLevel2 and version: 2 options

change version option, whenever you changed the reducers architecture

you can add any other redux-persist related config (check SdkPersistConfig interface) you want to it.

  1. import and add persistConfig to redux/configs/index.ts just like the following code:
// root/redux/configs/index.ts

import { SdkConfigureStore } from "@payping/redux/store";
import persistConfig from "./persistConfig";

export const reduxConfig: SdkConfigureStore = {
  appPersistConfig: persistConfig
};
  1. wrap your ReactDom.render() in a function called onRehydrated in order to render the app after persisted redux store, rehydrated from storage. it's for preventing any issue in redux related logics. then, pass the onRehydrated to the configureStore() method.
// root/index.tsx

import { configureStore } from "@payping/redux";
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import { PersistGate } from "redux-persist/integration/react";
import { reduxConfig } from "./redux/configs";

// eslint-disable-next-line @typescript-eslint/no-use-before-define
export const { store, persistor } = configureStore({ ...reduxConfig, onRehydrated });

const onRehydrated = (): void => {
  ReactDOM.render(
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <App />
      </PersistGate>
    </Provider>,
    document.getElementById("root")
  );
};
  • for assumption 3: * add your redux configs in redux/configs/index.ts. e.g. adding router middleware:
// root/redux/configs/index.ts

import { SdkStoreStates } from "@payping/redux/reducers";
import { SdkConfigureStore } from "@payping/redux/store";
import { RouterState, connectRouter, routerActions, routerMiddleware } from "connected-react-router";
import { LocationState, createHashHistory } from "history";
import { AnyAction, Reducer, StoreEnhancer } from "redux";

export interface AppStoreStates extends SdkStoreStates {
  router: RouterState<LocationState>;
}

export const history = createHashHistory();

const router = routerMiddleware(history);

const appReducers = {
  router: connectRouter(history) as Reducer<RouterState<LocationState>, AnyAction>,
};

const appMiddlewares = [router];
const composeActionCreators = { ...routerActions };

export const reduxConfig: SdkConfigureStore = {
  appMiddlewares,
  appReducers,
  composeActionCreators
};
  • for assumption 4:
  1. define your actions and reducers like the samples at the end of this page in redux/actions or redux/reducers respectively.

  2. export your actions in redux/actions/index.ts like below:

// root/redux/actions/index.ts

export * from "@payping/redux/actions";
export { logout, refreshToken, toggleLoggingIn } from "./appAuth";
  1. add your reducer in appReducers at redux/configs/index.ts and add it to AppStoreStates interface (implement interface if it doesn't exist).
// root/redux/configs/index.ts

import { SdkStoreStates } from "@payping/redux/reducers";
import { SdkConfigureStore } from "@payping/redux/store";
import appAuth, { AppAuthState } from "../reducers/appAuth";

export interface AppStoreStates extends SdkStoreStates {
  appAuth: AppAuthState;
}

const appReducers = {
  appAuth
};

export const reduxConfig: SdkConfigureStore = {
  appReducers,
};
  1. modify configureStore(reduxConfig) to configureStore<AppStoreStates>(reduxConfig).

sample action

// root/redux/actions/appAuth.ts

import { CLEAR_LIST, LOGGED_OUT, LOGGED_IN, SdkThunkActionResult, SdkThunkDispatch } from "@payping/redux/reducers";
import {
  LoggedInAction,
  TOGGLE_LOGGING_IN,
  ToggleLoggingInAction
} from "../reducers/appAuth";

function toggleLoggingIn(): ToggleLoggingInAction {
  return {
    type: TOGGLE_LOGGING_IN
  };
}

function loggedIn(token: string, expiresIn: number): LoggedInAction {
  return {
    payload: { expiresIn, token },
    type: LOGGED_IN
  };
}

function logoutDispatchs(): SdkThunkActionResult<Promise<void>> {
  return async (dispatch): Promise<void> => {
    dispatch({ type: LOGGED_OUT });
  };
}

export { toggleLoggingIn, loggedIn, logoutDispatchs };

sample reducer

// root/redux/reducers/appAuth.ts

import { LoggedOutAction } from "@payping/redux/reducers";

export const TOGGLE_LOGGING_IN = "TOGGLE_LOGGING_IN";
export const LOGGED_OUT = "LOGGED_OUT";
export const LOGGED_IN = "LOGGED_IN";

export interface AppAuthState {
  isLoggingIn: boolean;
}

export interface ToggleLoggingInAction {
  type: typeof TOGGLE_LOGGING_IN;
}

export interface LoggedInAction {
  type: typeof LOGGED_IN;
  payload: { token: string; expiresIn: number };
}

export type AuthActionTypes = ToggleLoggingInAction | LoggedInAction | LoggedOutAction;

export const initialState: AppAuthState = {
  isLoggingIn: false
};

export default function appAuth(state = initialState, action: AuthActionTypes): AppAuthState {
  if (action.type === TOGGLE_LOGGING_IN) {
    return {
      ...state,
      isLoggingIn: !state.isLoggingIn
    };
  }

  if (action.type === LOGGED_IN) {
    const { token, expiresIn } = action.payload;
    return {
      ...state,
      expiresIn,
      token
    };
  }

  if (action.type === LOGGED_OUT) {
    return initialState;
  }

  return state;
}

Checking changes before Publish:

  1. Run script for building package on local (customPrepublishOnly:app or customPrepublishOnly:admin).
  2. Output will be in a dist|distAdmin folder at the root of the project.
  3. Go to the project that you want to install this package in and install it locally:
    yarn add ../redux/dist/
    or
    yarn add ../redux/distAdmin/
    		```
    > **NOTE:** If your project has submodules you need to use the `-W` option in `yarn add` command and replace the version of the package in all package.json files with this route `../redux/dist/`.

How to Publish:

Admin:

  1. Create a release branch for this new version (its name should be the new version that you want to publish plus admin for example 1.0.4-admin):

  2. Edit build_config/admin/package.json version (pay attention to Semver)

  3. Run script for publishing package on registry:

    NOTE: In your terminal you should be logged in at our registry at https://npmregistry.manatadbir.ir If you aren't, run:

     npm logout
     npm login --registry=https://npmregistry.manatadbir.ir
yarn customPublish:admin
  1. Finish your release branch

Dashboard:

  1. Create a release branch for this new version (its name should be the new version that you want to publish plus app for example 1.0.4-app):

  2. Edit build_config/app/package.json version (pay attention to Semver)

  3. Run script for publishing package on registry:

    NOTE: In your terminal you should be logged in at npm registry at https://registry.npmjs.org. If you aren't, run:

     npm logout
     npm login
yarn customPublish:app
  1. Finish your release branch
2.24.0-beta.14

9 months ago

2.24.0-beta.13

9 months ago

2.24.0-beta.12

9 months ago

2.24.0-alpha.15

9 months ago

2.24.0-alpha.17

9 months ago

2.24.0-alpha.16

9 months ago

2.24.0-alpha.19

9 months ago

2.24.0-alpha.18

9 months ago

2.23.0-beta.3

11 months ago

2.23.0-beta.2

11 months ago

2.23.0-beta.1

12 months ago

2.24.0-beta.9

9 months ago

2.24.0-beta.2

10 months ago

2.24.0-beta.1

10 months ago

2.24.0-beta.4

10 months ago

2.24.0-beta.3

10 months ago

2.24.0-beta.6

10 months ago

2.24.0-beta.5

10 months ago

2.24.0-beta.8

9 months ago

2.24.0-beta.7

9 months ago

2.22.1-beta.5

12 months ago

2.24.0-beta.11

9 months ago

2.24.0-beta.10

9 months ago

2.23.0-alpha.1

12 months ago

2.23.0-alpha.2

12 months ago

2.24.0-alpha.8

10 months ago

2.24.0-alpha.2

10 months ago

2.24.0-alpha.1

10 months ago

2.22.1-beta.4

1 year ago

2.22.1-beta.3

1 year ago

2.22.1-beta.2

1 year ago

2.22.1-beta.1

1 year ago

2.22.1-alpha.1

1 year ago

2.22.0-beta.5

1 year ago

2.22.0-alpha.6

1 year ago

2.22.0-beta.1

1 year ago

2.21.0-alpha.4

1 year ago

2.22.0-alpha.5

1 year ago

2.21.0-beta.1

1 year ago

2.22.0-alpha.4

1 year ago

2.22.0-alpha.3

1 year ago

2.22.0-alpha.2

1 year ago

2.22.0-alpha.1

1 year ago

2.21.0-alpha.3

1 year ago

2.21.0-alpha.2

2 years ago

2.21.0-alpha.1

2 years ago

2.19.0

2 years ago

2.19.1

2 years ago

2.20.0-alpha.1

2 years ago

2.20.0

2 years ago

2.20.1

2 years ago

2.18.6-alpha.0

2 years ago

2.18.6-alpha.1

2 years ago

2.18.6-alpha.4

2 years ago

2.18.6-alpha.5

2 years ago

2.18.6-alpha.2

2 years ago

2.18.6-alpha.3

2 years ago

2.18.5

2 years ago

2.18.6

2 years ago

2.18.5-alpha.0

2 years ago

2.18.3-alpha.0

2 years ago

2.16.13-betta.0

2 years ago

2.18.1

2 years ago

2.18.2

2 years ago

2.18.0

2 years ago

2.16.14

2 years ago

2.16.13

2 years ago

2.16.13-alpha.3

2 years ago

2.16.13-alpha.2

2 years ago

2.16.13-alpha.1

2 years ago

2.16.13-alpha.0

3 years ago

2.17.0-alpha.0

2 years ago

2.17.0-alpha.1

2 years ago

2.16.12

3 years ago

2.16.12-beta.4

3 years ago

2.16.12-alpha.4

3 years ago

2.16.12-alpha.3

3 years ago

2.16.12-alpha.5

3 years ago

2.16.12-beta.3

3 years ago

2.16.12-beta.0

3 years ago

2.16.12-beta.2

3 years ago

2.16.12-beta.1

3 years ago

2.16.12-alpha.0

3 years ago

2.16.12-alpha.2

3 years ago

2.16.12-alpha.1

3 years ago

2.16.11

3 years ago

2.16.10

3 years ago

2.16.8-alpha.2

3 years ago

2.16.10-alpha.0

3 years ago

2.16.8-alpha.0

3 years ago

2.16.8-alpha.1

3 years ago

2.16.9

3 years ago

2.16.7

3 years ago

2.16.8

3 years ago

2.16.6

3 years ago

2.16.6-alpha.6

3 years ago

2.16.6-alpha.5

3 years ago

2.16.6-alpha.4

3 years ago

2.16.6-alpha.3

3 years ago

2.16.6-alpha.2

3 years ago

2.16.6-alpha.1

3 years ago

2.16.6-alpha.0

3 years ago

2.16.5-alpha.0

3 years ago

2.16.5-alpha.1

3 years ago

2.16.5

3 years ago

2.16.4

3 years ago

2.16.4-alpha.0

3 years ago

2.16.3

3 years ago

2.16.1

3 years ago

2.16.2

3 years ago

2.16.0

3 years ago

2.15.16-alpha.0

4 years ago

2.15.17-alpha.0

4 years ago

2.15.18-alpha.0

4 years ago

2.15.18-alpha.1

4 years ago

2.15.18-alpha.2

3 years ago

2.15.19

3 years ago

2.15.17

4 years ago

2.15.18

4 years ago

2.15.16

4 years ago

2.15.15

4 years ago

2.15.13

4 years ago

2.15.14

4 years ago

2.15.11

4 years ago

2.15.12

4 years ago

2.15.10

4 years ago

2.15.9

4 years ago

2.15.8

4 years ago

2.15.7

4 years ago

2.15.6

4 years ago

2.15.5

4 years ago

2.15.4

4 years ago

2.15.3

4 years ago

2.15.2

4 years ago

2.15.1

4 years ago

2.15.0

4 years ago

2.14.14

4 years ago

2.14.13

4 years ago

2.14.12

4 years ago

2.14.10

4 years ago

2.14.11

4 years ago

2.14.9

4 years ago

2.14.8

4 years ago

2.14.8-alpha.1

4 years ago

2.14.7

4 years ago

2.14.6

4 years ago

2.14.5

4 years ago

2.14.3

4 years ago

2.14.4

4 years ago

2.14.2

4 years ago

2.14.1

4 years ago

2.14.0

4 years ago

2.13.8

4 years ago

2.13.7

4 years ago

2.13.6

4 years ago

2.13.5

4 years ago

2.13.4

4 years ago

2.13.3

4 years ago

2.13.2

4 years ago

2.13.1

4 years ago

2.13.0

4 years ago

2.12.0

4 years ago

2.11.0

4 years ago

2.10.3

4 years ago

2.10.2

5 years ago

2.9.1

5 years ago

2.10.1

5 years ago

2.10.0

5 years ago

2.9.0

5 years ago

2.8.20

5 years ago

2.8.19

5 years ago

2.8.18

5 years ago

2.8.17

5 years ago

2.8.16

5 years ago

2.8.15

5 years ago

2.8.14

5 years ago

2.8.13

5 years ago

2.8.12

5 years ago

2.8.11

5 years ago

2.8.10

5 years ago

2.8.9

5 years ago

2.8.8

5 years ago

2.8.7

5 years ago

2.8.5

5 years ago

2.8.6

5 years ago

2.8.4

5 years ago

2.8.3

5 years ago

2.8.2

5 years ago

2.8.1

5 years ago

2.7.0

5 years ago

2.7.0-beta.1

5 years ago

2.6.2

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.6.0-beta.4

5 years ago

2.5.2

5 years ago

2.6.0-beta.3

5 years ago

2.6.0-beta.2

5 years ago

2.6.0-beta.1

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.9.0-beta.1

5 years ago

1.8.0-beta.1

5 years ago

1.8.0

5 years ago

2.0.0-alpha.2

5 years ago

1.7.3

5 years ago

2.0.0-alpha.1

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.4

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago