{"size": 3314, "ext": "js", "max_stars_repo_path": "src/store/actions/peopleSearchActions.js", "max_stars_repo_name": "bryanszendel/mobile-app-ejected", "max_stars_repo_stars_event_min_datetime": "2019-06-12T16:33:16.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-09T18:01:26.000Z", "max_issues_repo_path": "src/store/actions/peopleSearchActions.js", "max_issues_repo_name": "bryanszendel/mobile-app-ejected", "max_issues_repo_issues_event_min_datetime": "2021-10-06T06:01:13.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-18T13:04:20.000Z", "max_forks_repo_path": "src/store/actions/peopleSearchActions.js", "max_forks_repo_name": "bryanszendel/mobile-app-ejected", "max_forks_repo_forks_event_min_datetime": "2019-06-17T20:34:49.000Z", "max_forks_repo_forks_event_max_datetime": "2019-07-31T14:50:49.000Z"}
{"max_stars_count": 8, "max_issues_count": 2, "max_forks_count": 4, "avg_line_length": 28.5689655172, "max_line_length": 78, "alphanum_fraction": 0.5950512975}
import axios from 'axios';
import {
  FETCH_PEOPLE_SUCCESS,
  FETCH_PERSON,
  FETCH_PERSON_FAILURE,
  FETCH_PERSON_SUCCESS,
  FETCH_SEARCH_RESULT,
  FETCH_SEARCH_RESULT_FAILURE,
  RESET_PERSON,
  RESET_STATE,
  SAVING_RECENT_SEARCHES
} from './actionTypes';

import getEnvVars from '../../../environment';
import saveToRecentSearches from '../../helpers/saveToRecentSearches';
import { sendEvent, createOptions } from './../../helpers/createEvent';

const { peopleSearchURL } = getEnvVars();

export const fetchPerson = (body, email) => dispatch => {
  dispatch({ type: FETCH_PERSON });
  axios
    .post(`${peopleSearchURL}`, body)
    .then(res => {
      dispatch({
        type: FETCH_PERSON_SUCCESS,
        payload: res.data.person
      });
      options = createOptions(0, null, null);
      sendEvent(email, 'search', 'person', 'success', options);
    })
    .catch(err => {
      dispatch({ type: FETCH_PERSON_FAILURE, payload: err });
      sendEvent(email, 'search', 'person', 'failed');
    });
};

export const fetchSearchResult = (body, cb, email) => dispatch => {
  dispatch({ type: FETCH_SEARCH_RESULT });
  let isPerson = false;
  let options;
  axios
    .post(`${peopleSearchURL}`, body.requestObject)
    .then(res => {
      if (res.data.possible_persons) {
        options = createOptions(res.data.possible_persons.length, null, null);
        dispatch({
          type: FETCH_PEOPLE_SUCCESS,
          payload: res.data.possible_persons
        });
        sendEvent(email, 'search', 'person', 'success', options);
        // SAVE TO RECENT SEARCH
        if (body.searchType && body.searchInput) {
          saveToRecentSearches({
            searchType: body.searchType,
            searchInput: body.searchInput,
            data: res.data.possible_persons
          });
          dispatch({ type: SAVING_RECENT_SEARCHES });
        }
      } else if (res.data.person) {
        options = createOptions(0, null, null);
        isPerson = true;
        dispatch({
          type: FETCH_PERSON_SUCCESS,
          payload: res.data.person
        });
        sendEvent(email, 'search', 'person', 'success', options);
        // SAVE TO RECENT SEARCH
        if (body.searchType && body.searchInput) {
          saveToRecentSearches({
            searchType: body.searchType,
            searchInput: body.searchInput,
            data: res.data.person
          });
          dispatch({ type: SAVING_RECENT_SEARCHES });
        }
      } else if (
        res.data.persons_count === 0 ||
        res.data['@persons_count'] === 0
      ) {
        dispatch({
          type: FETCH_SEARCH_RESULT_FAILURE,
          data: res.data.query,
          query: res.data.query,
          payload: true
        });
        sendEvent(email, 'search', 'person', 'success', options);
      }
    })
    .then(() => {
      if (isPerson) {
        cb();
      }
    })
    .catch(err => {
      dispatch({ type: FETCH_SEARCH_RESULT_FAILURE, payload: err });
      sendEvent(email, 'search', 'person', 'failed');
    });
};

export const resetPerson = () => {
  return { type: RESET_PERSON };
};

export const resetState = () => {
  return { type: RESET_STATE };
};

export const sendSearchErrorMessage = errorObject => {
  return {
    type: FETCH_SEARCH_RESULT_FAILURE,
    payload: errorObject
  };
};
