如何减少样板

时间:2018-09-17 20:05:43

标签: javascript

我有这样的商店结构

/store
  /entities
    users.js
    sessions.js

我所有的实体看起来都是这样

import { FORM_ERROR } from 'react-final-form';
import { normalize, schema } from 'normalizr';

import { SCHEMA as USER_SHEMA } from './users';

export const SCHEMA = new schema.Entity(
  'sessions',
  { user: USER_SHEMA },
  { idAttribute: 'sessionGuid' },
);

export const CREATE_SESSION_REQUEST = 'CREATE_SESSION_REQUEST';
export const CREATE_SESSION_SUCCESS = 'CREATE_SESSION_SUCCESS';
export const CREATE_SESSION_FAILURE = 'CREATE_SESSION_FAILURE';

export const create = session => async (dispatch, getState, api) => {
  dispatch({ type: CREATE_SESSION_REQUEST });
  try {
    const { data } = await api.post('/sessions', session);
    const payload = normalize(data, SCHEMA);

    dispatch({ type: CREATE_SESSION_SUCCESS, payload });
  } catch (error) {
    dispatch({ type: CREATE_SESSION_FAILURE, payload: error, error: true });

    return { [FORM_ERROR]: error };
  }
  return null;
};

export const FETCH_ALL_SESSIONS_REQUEST = 'FETCH_ALL_SESSIONS_REQUEST';
export const FETCH_ALL_SESSIONS_SUCCESS = 'FETCH_ALL_SESSIONS_SUCCESS';
export const FETCH_ALL_SESSIONS_FAILURE = 'FETCH_ALL_SESSIONS_FAILURE';

export const fetchAll = params => async (dispatch, getState, api) => {
  dispatch({ type: FETCH_ALL_SESSIONS_REQUEST });
  try {
    const { data } = await api.get('/sessions', { params });
    const payload = normalize(data, [SCHEMA]);

    dispatch({ type: CREATE_SESSION_SUCCESS, payload });
  } catch (error) {
    dispatch({ type: FETCH_ALL_SESSIONS_FAILURE, payload: error, error: true });
  }
};

export const GET_ONE_SESSION_REQUEST = 'GET_ONE_SESSION_REQUEST';
export const GET_ONE_SESSION_SUCCESS = 'GET_ONE_SESSION_SUCCESS';
export const GET_ONE_SESSION_FAILURE = 'GET_ONE_SESSION_FAILURE';

export const fetchOne = sessionId => async (dispatch, getState, api) => {
  dispatch({ type: GET_ONE_SESSION_REQUEST });
  try {
    const { data } = await api.get(`/sessions/${sessionId}`);
    const payload = normalize(data, SCHEMA);

    dispatch({ type: GET_ONE_SESSION_SUCCESS, payload });
  } catch (error) {
    dispatch({ type: GET_ONE_SESSION_FAILURE, payload: error, error: true });
  }
};

export const UPDATE_SESSION_REQUEST = 'UPDATE_SESSION_REQUEST';
export const UPDATE_SESSION_SUCCESS = 'UPDATE_SESSION_SUCCESS';
export const UPDATE_SESSION_FAILURE = 'UPDATE_SESSION_FAILURE';

export const update = (sessionId, session) => async (dispatch, getState, api) => {
  dispatch({ type: UPDATE_SESSION_REQUEST, meta: { id: sessionId } });
  try {
    await api.put(`/sessions/${sessionId}`, session);

    dispatch({ type: UPDATE_SESSION_SUCCESS, meta: { id: sessionId } });
  } catch (error) {
    dispatch({
      type: UPDATE_SESSION_FAILURE,
      error: true,
      payload: error,
      meta: { id: sessionId },
    });

    return { [FORM_ERROR]: error };
  }
  return null;
};

export const DESTROY_SESSION_REQUEST = 'DESTROY_SESSION_REQUEST';
export const DESTROY_SESSION_SUCCESS = 'DESTROY_SESSION_SUCCESS';
export const DESTROY_SESSION_FAILURE = 'DESTROY_SESSION_FAILURE';

export const destroy = sessionId => async (dispatch, getState, api) => {
  dispatch({ type: DESTROY_SESSION_REQUEST, meta: { id: sessionId } });
  try {
    await api.delete(`/sessions/${sessionId}`);

    dispatch({ type: DESTROY_SESSION_SUCCESS, meta: { id: sessionId } });
  } catch (error) {
    dispatch({
      type: DESTROY_SESSION_FAILURE,
      error: true,
      payload: error,
      meta: { id: sessionId },
    });
  }
};

我想在组件中使用import * as ns from './ns'语法,因此我导出了actionTypes like as constants。在users.js中,使用相同的代码,但是所有单词session都被users替换。我的项目现在有12个实体,当我替换第13个文件中的所有关键字时,我想起了DRY原则。但是我没有找到减少模板代码的正确方法,而不会减少导入。

import * as sessionsActions from 'store/modules/entities/sessions';

如何通过保存当前代码来最小化样板?

0 个答案:

没有答案