Skip to content
Snippets Groups Projects
terms-of-use.store.js 2.01 KiB
Newer Older
import client from '@/api/loginAPI.js';
import { ErrorService } from '@/services/error-service.js';
import router from '@/router';
Florent Lavelle's avatar
Florent Lavelle committed
import i18n from '@/i18n';

const state = {
  terms: null,
  error: null,
  success: null
};

const getters = {
};

export const GET_TERMS_OF_USE = 'GET_TERMS_OF_USE';
export const AGREE_TO_TERMS_OF_USE = 'AGREE_TO_TERMS_OF_USE';

const actions = {
  [GET_TERMS_OF_USE]: async ({ commit }) => {
    const response = await client.getTermsOfUse();
    if (response.status === 200) {
      commit('SET_TERMS', response.data);
      commit('SET_SUCCESS', {
        response: response,
        message: ''
      });
    }
    if (response.status === 404) {
      commit('SET_ERROR', {
        response: response,
Florent Lavelle's avatar
Florent Lavelle committed
        message: i18n.t('messages.error')
      });
    }
  },
  [AGREE_TO_TERMS_OF_USE]: async ({ commit }, data) => {
    const response = await client.postTermsOfUseAgreement(data);
    if (response.status === 200) {
      commit('SET_SUCCESS', {
        response: response,
        message: ''
      });
      if (
        this.$config.authorizedRedirections.some(reg => {
          return reg.test(decodeURIComponent(router.currentRoute.query.next))
        })
      ) {
        window.location.href = router.currentRoute.query.next;
      } else {
        this.$router.push({ name: 'NotFound' });
      }
    }
    if (response.status === 404) {
      commit('SET_ERROR', {
        response: response,
Florent Lavelle's avatar
Florent Lavelle committed
        message: i18n.t('messages.error')
      });
    }
  }
};

export const SET_TERMS = 'SET_TERMS';
export const SET_ERROR = 'SET_ERROR';
export const SET_SUCCESS = 'SET_SUCCESS';

const mutations = {
  [SET_TERMS]: (state, payload) => {
    state.terms = payload;
  },
  [SET_ERROR]: (state, payload) => {
    state.success = null;
    ErrorService.onError(payload.response);
    state.error = payload.message;
  },
  [SET_SUCCESS]: (state, payload) => {
    state.error = null;
    state.success = payload.message;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};