Skip to content
Snippets Groups Projects
projects.store.js 3.87 KiB
Newer Older
Florent Lavelle's avatar
Florent Lavelle committed
import axios from '@/axios-client.js';
import projectAPI from '@/services/project-api';
Florent Lavelle's avatar
dev
Florent Lavelle committed

const projects = {

  namespaced: true,

  state: {
Florent Lavelle's avatar
Florent Lavelle committed
    currentPage: 1,
    projects: [],
Florent Lavelle's avatar
Florent Lavelle committed
    project_slug: null,
    filters: {
      moderation: null,
      access_level: null,
      user_access_level: null
    },
    searchProjectsFilter: null,
    isProjectsListSearched: null,
Florent Lavelle's avatar
dev
Florent Lavelle committed
  },

  getters: {
Florent Lavelle's avatar
Florent Lavelle committed
    project: state => state.projects.find((project) => project.slug === state.project_slug),
    project_types: state => state.projects.filter(projet => projet.is_project_type),
    project_user: state => state.projects.filter(projet => projet.creator === state.user.id),
Florent Lavelle's avatar
dev
Florent Lavelle committed
  },

  mutations: {
Florent Lavelle's avatar
Florent Lavelle committed
    SET_CURRENT_PAGE (state, payload) {
      state.currentPage = payload;
    },

    SET_PROJECTS(state, projects) {
      state.projects = projects.results;
      state.count = projects.count;
Florent Lavelle's avatar
Florent Lavelle committed
    },
Florent Lavelle's avatar
dev
Florent Lavelle committed

Florent Lavelle's avatar
Florent Lavelle committed
    ADD_PROJECT(state, project) {
      state.projects = [project, ...state.projects];
    },

    SET_PROJECT_SLUG(state, slug) {
      state.project_slug = slug;
    },

    SET_PROJECTS_FILTER(state, payload) {
      state.filters[payload.filter] = payload.value;
    },

    SET_PROJECTS_SEARCH_STATE(state, payload) {
      state.isProjectsListSearched = payload.isSearched;
      state.searchProjectsFilter = payload.text;
    },
Florent Lavelle's avatar
dev
Florent Lavelle committed
  },

  actions: {
Florent Lavelle's avatar
Florent Lavelle committed
    async GET_ALL_PROJECTS({ rootState, commit }) {
      try {
        const response = await axios
          .get(`${rootState.configuration.VUE_APP_DJANGO_API_BASE}projects/`);
        if (response.status === 200 && response.data) {
          // const orderedProjects = response.data.sort((a, b) => parseDate(b.created_on) - parseDate(a.created_on));
          commit('SET_PROJECTS', response.data);
Florent Lavelle's avatar
Florent Lavelle committed
        }
      } catch (error) {
        console.error(error);
        throw error;
      }
    },

    async GET_PROJECTS({ state, rootState, commit }, page) {
      if (!page) {
        page = state.currentPage;
      }
      const baseUrl = rootState.configuration.VUE_APP_DJANGO_API_BASE;
      const projects = await projectAPI.getProjects(baseUrl, state.filters, page);
      commit('SET_PROJECTS', projects);
    },

    async SEARCH_PROJECTS({ commit, dispatch }, text) {
      if (text) {
        await dispatch('HANDLE_PROJECTS_SEARCH_REQUEST', text);
      } else {
        commit('SET_PROJECTS_SEARCH_STATE', {
          isSearched: false,
          text: null
        });
        await dispatch('GET_PROJECTS');
      }
    },

    async HANDLE_PROJECTS_SEARCH_REQUEST({ state, rootState, commit }, text) {

      if (rootState.cancellableSearchRequest.length > 0) {
        const currentRequestCancelToken =
          rootState.cancellableSearchRequest[rootState.cancellableSearchRequest.length - 1];
        currentRequestCancelToken.cancel();
      }
  
      const cancelToken = axios.CancelToken.source();
      commit('SET_CANCELLABLE_SEARCH_REQUEST', cancelToken, { root: true });
  
      const url = `${rootState.configuration.VUE_APP_DJANGO_API_BASE}projects/?search=${text}`;
      let filteredUrl;
      if (Object.values(state.filters).some(el => el && el.length > 0)) {
        filteredUrl = url;
        for (const filter in state.filters) {
          if (state.filters[filter]) {
            filteredUrl = filteredUrl.concat('', `&${filter}=${state.filters[filter]}`);
          }
        }
      }
  
      try {
        const response = await axios.get(
          filteredUrl ? filteredUrl : url,
          {
            cancelToken: cancelToken.token,
          }
        );
        if (response.status === 200) {
          const projects = response.data;
          if (projects) {
            commit('SET_PROJECTS', projects);
            commit('SET_PROJECTS_SEARCH_STATE', {
              isSearched: true,
              text: text
            });
          }
        }
      } catch(err) {
        console.error(err);
      }
    },
Florent Lavelle's avatar
dev
Florent Lavelle committed
  }

};

export default projects;