{"size": 2752, "ext": "js", "max_stars_repo_path": "client/scenes/document/data/documentMetadata/actions.js", "max_stars_repo_name": "yungtiec/bkp", "max_stars_repo_stars_event_min_datetime": "2019-06-24T16:59:27.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-24T16:59:27.000Z", "max_issues_repo_path": "client/scenes/document/data/documentMetadata/actions.js", "max_issues_repo_name": "yungtiec/bkp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/scenes/document/data/documentMetadata/actions.js", "max_forks_repo_name": "yungtiec/bkp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.016, "max_line_length": 79, "alphanum_fraction": 0.6151889535}
import { maxBy } from "lodash";
import * as types from "./actionTypes";
import {
  getMetadataByDocumentId,
  getMetadataBySlug,
  postUpvoteToDocument,
  postDownvoteToDocument,
  putContentHTMLBySlug
} from "./service";
import { loadModal } from "../../../../data/reducer";
import history from "../../../../history";

export function fetchMetadataByDocumentId(documentId, versionId) {
  return async (dispatch, getState) => {
    try {
      var documentMetadata = await getMetadataByDocumentId(documentId);
      dispatch({
        type: types.DOCUMENT_METADATA_FETCH_SUCCESS,
        documentMetadata
      });
    } catch (error) {
      console.error(error);
    }
  };
}

export function resetDocumentMetadata() {
  return {
    type: types.DOCUMENT_METADATA_RESET
  };
}

export function fetchMetadataBySlug(slug, path) {
  return async (dispatch, getState) => {
    try {
      var documentMetadata = await getMetadataBySlug(slug);
      dispatch({
        type: types.DOCUMENT_METADATA_FETCH_SUCCESS,
        documentMetadata
      });
    } catch (error) {
      history.push({
        pathname: '/permission-denied',
        state: {
          lastPath: path
        }
      });
      console.error(error);
    }
  };
}

export function updateContentHTMLBySlug(slug, propertiesToUpdate, hideEditor) {
  return async (dispatch, getState) => {
    try {
      var documentMetadata = await putContentHTMLBySlug(
        slug,
        propertiesToUpdate
      );
      dispatch({
        type: types.DOCUMENT_CONTENT_HTML_UPDATE_SUCCESS,
        documentMetadata
      });
      hideEditor();
      if (slug !== documentMetadata.slug) {
        history.push(`/s/${documentMetadata.slug}`)
      }
    } catch (error) {
      console.error(error);
    }
  };
}

export function downvoteDocument({
  documentId,
  versionId,
  projectSymbol,
  hasUpvoted,
  hasDownvoted
}) {
  return async (dispatch, getState) => {
    try {
      const [upvotesFrom, downvotesFrom] = await postDownvoteToDocument({
        documentId,
        projectSymbol,
        hasUpvoted,
        hasDownvoted
      });
      dispatch({
        type: types.SURVEY_VOTED,
        upvotesFrom,
        downvotesFrom
      });
    } catch (err) {
      console.log(err);
    }
  };
}

export function upvoteDocument({
  documentId,
  versionId,
  projectSymbol,
  hasUpvoted,
  hasDownvoted
}) {
  return async (dispatch, getState) => {
    try {
      const [upvotesFrom, downvotesFrom] = await postUpvoteToDocument({
        documentId,
        projectSymbol,
        hasUpvoted,
        hasDownvoted
      });
      dispatch({
        type: types.SURVEY_VOTED,
        upvotesFrom,
        downvotesFrom
      });
    } catch (err) {
      console.log(err);
    }
  };
}
