<commit_before>import axios from '../utils/axiosWrapper'
import { browserHistory } from 'react-router'

// action types
export const USERS_FETCH = "USERS_FETCH"
export const USERS_FETCH_SUCCESS = "USERS_FETCH_SUCCESS"
export const USERS_FETCH_ERROR = "USERS_FETCH_ERROR"

export const USER_FETCH = "USER_FETCH"
export const USER_FETCH_SUCCESS = "USER_FETCH_SUCCESS"
export const USER_FETCH_ERROR = "USER_FETCH_ERROR"

export const USER_RESET = "USER_RESET"
export const USER_UPDATE_FIELD = "USER_UPDATE_FIELD"

export const USER_UPDATE = "USER_UPDATE"
export const USER_UPDATE_SUCCESS = "USER_UPDATE_SUCCESS"
export const USER_UPDATE_ERROR = "USER_UPDATE_ERROR"

export const USER_CREATE = "USER_CREATE"
export const USER_CREATE_SUCCESS = "USER_CREATE_SUCCESS"
export const USER_CREATE_ERROR = "USER_CREATE_ERROR"

export const USER_DELETE = "USER_DELETE"
export const USER_DELETE_SUCCESS = "USER_DELETE_SUCCESS"
export const USER_DELETE_ERROR = "USER_DELETE_ERROR"

// gets error message from server response
const errorHandler = err => err.response.data.message;
const endpoint = "/user";

// action creators

// receives pagination params
// and dispatches actions to fetch the list of users
export function fetchUsers(params) {
  return (dispatch) => {
    dispatch({ type: USERS_FETCH });
    axios.get(endpoint, {params})
    .then(res => dispatch({ type: USERS_FETCH_SUCCESS, users: res.data }))
    .catch(err => dispatch({ type: USERS_FETCH_ERROR, error: errorHandler(err) }))
  }
}

// receives project id
// and dispatches actions to fetch the user
export function fetchSingleUser(id) {
  return (dispatch) => {
    dispatch({ type: USER_FETCH })
    axios.get(`${endpoint}/id/${id}`)
    .then(res => dispatch({ type: USER_FETCH_SUCCESS, user: res.data }))
    .catch(err => dispatch({ type: USER_FETCH_ERROR, error: errorHandler(err) }))
  }
}

// resets active client back to null
// useful for creating a new user
export function resetUser() {
  return { type: USER_RESET }
}

// updates form field when user types
export function updateUserField(name, value) {
  return { name, value, type: USER_UPDATE_FIELD }
}

// receives user, sends data to backend,
// and dispatch the related actions
export function saveUser(user) {
  return (dispatch) => {
    dispatch({ type: USER_UPDATE })
    axios.put(`${endpoint}/id/${user.id}`, user)
    .then(res => {
      dispatch({ type: USER_UPDATE_SUCCESS, user: res.data })
      browserHistory.push('/users')
    })
    .catch(err => dispatch({ type: USER_UPDATE_ERROR, error: errorHandler(err) }))
  }
}

// receives user, sends data to backend,
// and dispatch the related actions
export function createUser(user) {
  return (dispatch) => {
    dispatch({ type: USER_CREATE })
    axios.post(endpoint, user)
    .then(res => {
      dispatch({ type: USER_CREATE_SUCCESS, user: res.data })
      browserHistory.push('/users')
    })
    .catch(err => dispatch({ type: USER_CREATE_ERROR, error: errorHandler(err) }))
  }
}

// receives client, deletes data in backend,
// and dispatch the related actions
export function deleteUser(user) {
  return (dispatch) => {
    dispatch({ type: USER_DELETE })
    axios.delete(`${endpoint}/id/${user.id}`)
    .then(res => dispatch({ type: USER_DELETE_SUCCESS, user }))
    .catch(err => dispatch({ type: USER_DELETE_ERROR, error: errorHandler(err) }))
  }
}

const initialUser = {
  activated: true
}
const initialState = {
  currentPage: [],
  activeUser: {...initialUser}
}

// computes a slice of the state for the USER_UPDATE_SUCCESS action
const updateSuccess = (state, action) => {
  const users = state.currentPage.map(user => {
    return user.id === action.user.id ?
      action.user :
      user;
  });
  return {
    ...state,
    currentPage: users,
    activeUser: action.user,
    loading: false
  }
}

// computes a slice of the state for the USER_CREATE_SUCCESS action
const createSuccess = (state, action) => {
  const users = state.currentPage.concat(action.user);
  return {
    ...state,
    currentPage: users,
    activeUser: {...initialUser},
    loading: false
  }
}


// reducer
export default (state = initialState, action) => {
  switch (action.type) {
    case USERS_FETCH:
    case USER_FETCH:
    case USER_UPDATE:
    case USER_CREATE:
    case USER_DELETE:
      return {
        ...state,
        loading: true
      }
    case USERS_FETCH_SUCCESS:
      return {
        ...state,
        currentPage: action.users.content,
        loading: false
      }
    case USER_FETCH_SUCCESS:
      return {
        ...state,
        activeUser: action.user,
        loading: false
      }
    case USER_UPDATE_SUCCESS:
      return updateSuccess(state, action)
    case USER_CREATE_SUCCESS:
      return createSuccess(state, action)
    case USER_DELETE_SUCCESS:
      return {
        ...state,
        loading: false,
        currentPage: state.currentPage
          .filter(user => user.id !== action.user.id)
      }
    case USER_RESET:
      return {
        ...state,
        activeUser: {...initialUser}
      }
    case USER_UPDATE_FIELD:
      return {
        ...state,
        activeUser: {
          ...state.activeUser,
          [action.name]: action.value
        }
      }
    case USERS_FETCH_ERROR:
    case USER_FETCH_ERROR:
    case USER_UPDATE_ERROR:
    case USER_CREATE_ERROR:
    case USER_DELETE_ERROR:
      return {
        ...state,
        error: action.error,
        loading: false
      }
    default:
      return state;
  }
}
<commit_msg>refactor: clean user reducer for promise middleware<commit_after>import axios from '../utils/axiosWrapper'
import { browserHistory } from 'react-router'

// action types
export const USERS_FETCH = "USERS_FETCH"
export const USER_FETCH = "USER_FETCH"
export const USER_UPDATE = "USER_UPDATE"
export const USER_CREATE = "USER_CREATE"
export const USER_DELETE = "USER_DELETE"

export const USER_RESET = "USER_RESET"
export const USER_UPDATE_FIELD = "USER_UPDATE_FIELD"

// gets error message from server response
const errorHandler = err => err.response.data.message;
const endpoint = "/user";

// action creators

// receives pagination params
// and dispatches actions to fetch the list of users
export function fetchUsers(params) {
  return {
    type: USERS_FETCH,
    payload: axios.get(endpoint, {params}).then(res => res.data)
  }
}

// receives project id
// and dispatches actions to fetch the user
export function fetchSingleUser(id) {
  return {
    type: USER_FETCH,
    payload: axios.get(`${endpoint}/id/${id}`)
      .then(res => res.data)
  }
}

// resets active client back to null
// useful for creating a new user
export function resetUser() {
  return { type: USER_RESET }
}

// updates form field when user types
export function updateUserField(name, value) {
  return { name, value, type: USER_UPDATE_FIELD }
}

// receives user, sends data to backend,
// and dispatch the related actions
export function saveUser(user) {
  const promise = axios.put(`${endpoint}/id/${user.id}`, user)
    .then(res => res.data)
  promise.then(() => {
    browserHistory.push('/users')
  })
  return {
    type: USER_UPDATE,
    payload: promise
  }
}

// receives user, sends data to backend,
// and dispatch the related actions
export function createUser(user) {
  const promise = axios.post(endpoint, user)
    .then(res => res.data)
  promise.then(() => {
    browserHistory.push('/users')
  })
  return {
    type: USER_CREATE,
    payload: promise
  }
}

// receives client, deletes data in backend,
// and dispatch the related actions
export function deleteUser(user) {
  return {
    type: USER_DELETE,
    payload: axios.delete(`${endpoint}/id/${user.id}`)
      .then(res => res.data)
  }
}

const initialUser = {
  activated: true
}
const initialState = {
  currentPage: [],
  activeUser: {...initialUser}
}

// computes a slice of the state for the USER_UPDATE_SUCCESS action
const updateSuccess = (state, action) => {
  const users = state.currentPage.map(user => {
    return user.id === action.payload.id ?
      action.payload :
      user;
  });
  return {
    ...state,
    currentPage: users,
    activeUser: action.payload,
    loading: false
  }
}

// computes a slice of the state for the USER_CREATE_SUCCESS action
const createSuccess = (state, action) => {
  const users = state.currentPage.concat(action.payload);
  return {
    ...state,
    currentPage: users,
    activeUser: {...initialUser},
    loading: false
  }
}


// reducer
export default (state = initialState, action) => {
  switch (action.type) {
    case `${USERS_FETCH}_LOADING`:
    case `${USER_FETCH}_LOADING`:
    case `${USER_UPDATE}_LOADING`:
    case `${USER_CREATE}_LOADING`:
    case `${USER_DELETE}_LOADING`:
      return {
        ...state,
        loading: true
      }
    case `${USERS_FETCH}_SUCCESS`:
      return {
        ...state,
        currentPage: action.payload.content,
        loading: false
      }
    case `${USER_FETCH}_SUCCESS`:
      return {
        ...state,
        activeUser: action.payload,
        loading: false
      }
    case `${USER_UPDATE}_SUCCESS`:
      return updateSuccess(state, action)
    case `${USER_CREATE}_SUCCESS`:
      return createSuccess(state, action)
    case `${USER_DELETE}_SUCCESS`:
      return {
        ...state,
        loading: false,
        currentPage: state.currentPage
          .filter(user => user.id !== action.payload.id)
      }
    case USER_RESET:
      return {
        ...state,
        activeUser: {...initialUser}
      }
    case USER_UPDATE_FIELD:
      return {
        ...state,
        activeUser: {
          ...state.activeUser,
          [action.name]: action.value
        }
      }
    case `${USERS_FETCH}_ERROR`:
    case `${USER_FETCH}_ERROR`:
    case `${USER_UPDATE}_ERROR`:
    case `${USER_CREATE}_ERROR`:
    case `${USER_DELETE}_ERROR`:
      return {
        ...state,
        error: action.error,
        loading: false
      }
    default:
      return state;
  }
}

{'fraction_non_alphanumeric': 0.11938202247191011, 'fraction_numerical': 0.0, 'mean_word_length': 2.9891956782713085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6486232', 'n_tokens_mistral': 3120, 'n_tokens_neox': 3079, 'n_words': 811}