{"size": 1565, "ext": "js", "max_stars_repo_path": "src/actions/evaluation.js", "max_stars_repo_name": "Klynger/genus-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/actions/evaluation.js", "max_issues_repo_name": "Klynger/genus-client", "max_issues_repo_issues_event_min_datetime": "2018-09-15T16:28:19.000Z", "max_issues_repo_issues_event_max_datetime": "2019-04-06T22:48:23.000Z", "max_forks_repo_path": "src/actions/evaluation.js", "max_forks_repo_name": "Klynger/genus-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 65, "max_forks_count": null, "avg_line_length": 27.9464285714, "max_line_length": 99, "alphanum_fraction": 0.6555910543}
import { normalize } from 'normalizr';
import { evaluationSchema } from '../models/schema';
import { requestGraphql } from '../utils/HTTPClient';
import {
  mutationCreateEvaluation,
  mutationEditEvaluation,
} from '../queryGenerators/evaluationMutations';
import {
  SAVE_EVALUATION,
  SAVE_ALL_EVALUATION_RESULTS,
  SAVE_ALL_EVALUATIONS,
  ADD_EVALUATION_TO_SUBJECT,
} from './actionTypes';

export const createEvaluation = newEvaluation => dispatch => {
  return requestGraphql(
    mutationCreateEvaluation(newEvaluation),
    localStorage.getItem('token'),
  ).then(res => {
    if (res.data.data && res.data.data.createEvaluation) {
      const { entities } = normalize(res.data.data.createEvaluation, evaluationSchema);
      dispatch({
        type: SAVE_ALL_EVALUATION_RESULTS,
        payload: entities.evaluationResult,
      });
      dispatch({
        type: SAVE_ALL_EVALUATIONS,
        payload: entities.evaluation,
      });
      dispatch({
        type: ADD_EVALUATION_TO_SUBJECT,
        payload: {
          subjectId: newEvaluation.subjectId,
          evaluation: Object.keys(entities.evaluation)[0],
        },
      });
    } else {
      // TODO
    }
    return res;
  });
};

export const editEvaluation = input => dispatch => {
  return requestGraphql(mutationEditEvaluation(input), localStorage.getItem('token')).then(res => {
    if (res.data.data && res.data.data.editEvaluation) {
      dispatch({
        type: SAVE_EVALUATION,
        evaluation: res.data.data.editEvaluation,
      });
    } else {
      // TODO
    }
  });
};
