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

     1  import { ActionContext } from 'vuex';
     2  import Axios from 'axios';
     3  import { UserState, Mutations } from '.';
     4  import { RootState } from '@/store';
     5  import { reasonToError } from '@/plugins/http';
     6  
     7  export function fetchUser<S extends UserState, R extends RootState>(context: ActionContext<S, R>) {
     8    return new Promise<void>((resolve) => {
     9      context.commit(Mutations.CLEAR_USER_ERROR);
    10      Axios.get<User>(`${context.rootState.base}/api/v1/user`).then((response) => {
    11        context.commit(Mutations.UPDATE_USER, response.data);
    12      }).catch(reason => {
    13        if (reason.response && reason.response.status === 404) {
    14          context.commit(Mutations.SET_USER_ERROR, new Error('user not found'));
    15        } else {
    16          context.commit(Mutations.SET_USER_ERROR, reasonToError(reason));
    17        }
    18      }).finally(() => {
    19        resolve();
    20      });
    21    });
    22  }
    23  
    24  export function fetchTokens<S extends UserState, R extends RootState>(context: ActionContext<S, R>): Promise<void> {
    25    return new Promise<void>((resolve) => {
    26      const base = context.rootState.base;
    27      context.commit(Mutations.START_USER_LOADING);
    28      context.commit(Mutations.UPDATE_USER_TOKENS, []);
    29      Axios.get<Token[]>(`${base}/api/v1/user/tokens`)
    30        .then(response => {
    31          context.commit(Mutations.UPDATE_USER_TOKENS, response.data);
    32        })
    33        .catch(reason => {
    34          console.warn(reasonToError(reason));
    35        })
    36        .finally(() => {
    37          context.commit(Mutations.STOP_USER_LOADING);
    38          resolve();
    39        });
    40    });
    41  }
    42  
    43  export function fetchSCM<S extends UserState, R extends RootState>(context: ActionContext<S, R>): Promise<void> {
    44    return new Promise<void>((resolve) => {
    45      const base = context.rootState.base;
    46      context.commit(Mutations.START_USER_LOADING);
    47      Axios.get<Record<string, boolean>>(`${base}/api/v1/user/scm`).then(response => {
    48        context.commit(Mutations.UPDATE_USER_SCM, response.data);
    49      }).catch(reason => {
    50        console.warn(reasonToError(reason));
    51      }).finally(() => {
    52        context.commit(Mutations.STOP_USER_LOADING);
    53        resolve();
    54      });
    55    });
    56  }