github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/src/store/modules/report/actions.ts (about)

     1  import { ActionContext } from 'vuex';
     2  import Axios from 'axios';
     3  import { Route } from 'vue-router';
     4  import { ReportState, Mutations } from '.';
     5  import { RootState } from '@/store';
     6  import { reasonToError } from '@/plugins/http';
     7  
     8  export type FetchReportOption = {
     9    ReportID: string;
    10    Ref?: string;
    11  };
    12  
    13  export function fetchCurrentReport<S extends ReportState, R extends RootState>(context: ActionContext<S, R>, option: FetchReportOption): Promise<void> {
    14    return new Promise((resolve) => {
    15      context.commit(Mutations.START_REPORT_LOADING);
    16      const params: Record<string, string | boolean> = {};
    17      params.latest = true;
    18      if (option.Ref) {
    19        params.ref = option.Ref;
    20      }
    21      Axios.get<Report[]>(`${context.rootState.base}/api/v1/reports/${option.ReportID}`, {
    22        params: params
    23      })
    24        .then((response) => {
    25          if (response.data.length <= 0) {
    26            context.commit(Mutations.SET_REPORT_CURRENT, undefined);
    27            context.commit(Mutations.SET_REPORT_ERROR, new Error('report not found'));
    28          } else {
    29            context.commit(Mutations.SET_REPORT_CURRENT, response.data[0]);
    30          }
    31        })
    32        .catch(reason => {
    33          context.commit(Mutations.SET_REPORT_CURRENT, undefined);
    34          context.commit(Mutations.SET_REPORT_ERROR, reasonToError(reason));
    35        })
    36        .finally(() => {
    37          context.commit(Mutations.STOP_REPORT_LOADING);
    38          resolve();
    39        });
    40    });
    41  }
    42  
    43  export function fetchSource<S extends ReportState, R extends RootState>(context: ActionContext<S, R>, to: Route): Promise<void> {
    44    return new Promise(resolve => {
    45      context.commit(Mutations.START_REPORT_LOADING);
    46      const base = context.rootState.base;
    47      const { scm, namespace, name, path } = to.params;
    48      let params = {};
    49      if (context.state.current) {
    50        params = {
    51          ref: context.state.current.commit
    52        };
    53      }
    54      Axios.get<string>(
    55        `${base}/api/v1/repos/${scm}/${namespace}/${name}/content/${path}`,
    56        {
    57          params: params
    58        })
    59        .then(response => {
    60          context.commit(Mutations.SET_REPORT_SOURCE, response.data);
    61        })
    62        .catch(reason => {
    63          context.commit(Mutations.SET_REPORT_ERROR, reasonToError(reason));
    64        })
    65        .finally(() => {
    66          context.commit(Mutations.STOP_REPORT_LOADING);
    67          resolve();
    68        });
    69    });
    70  }
    71  
    72  export function fetchHistory<S extends ReportState, R extends RootState>(context: ActionContext<S, R>, option: FetchReportOption): Promise<void> {
    73    return new Promise(resolve => {
    74      context.commit(Mutations.START_REPORT_LOADING);
    75      const params: Record<string, string | boolean> = {};
    76      if (option.Ref) {
    77        params.ref = option.Ref;
    78      }
    79      Axios.get<Report[]>(`${context.rootState.base}/api/v1/reports/${option.ReportID}`, {
    80        params: params
    81      }).then(response => {
    82        context.commit(Mutations.SET_REPORT_HISTORY, response.data);
    83      }).catch(() => {
    84        context.commit(Mutations.SET_REPORT_HISTORY, []);
    85      }).finally(() => {
    86        context.commit(Mutations.STOP_REPORT_LOADING);
    87        resolve();
    88      });
    89    });
    90  }