Skip to content
Snippets Groups Projects
projects.store.js 1.97 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
    },
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;
    }
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
      console.log(state.filters);
      const projects = await projectAPI.getProjects(baseUrl, state.filters, page);
      commit('SET_PROJECTS', projects);
    },
Florent Lavelle's avatar
dev
Florent Lavelle committed
  }

};

export default projects;