Skip to content
Snippets Groups Projects
feature.js 5.31 KiB
Newer Older
const axios = require("axios");
//import router from '../../router'


const feature = {
  namespaced: true,
  state: {
    attachmentFormset: [],
    linkedFormset: [],
    features: [],
    form: null,
    extra_form: []
  },
  mutations: {
    SET_FEATURES(state, features) {
      state.features = features;
    },
    UPDATE_FORM(state, payload) {
      state.form = payload;
    },
    UPDATE_EXTRA_FORM(state, extra_form) {
      const index = state.extra_form.findIndex(el => el.label === extra_form.label);
      if (index !== -1) {
        state.extra_form[index] = extra_form;
      }
    },
    SET_EXTRA_FORM(state, extra_form) {
      state.extra_form = extra_form;
    },
    ADD_ATTACHMENT_FORM(state, attachmentFormset) {
      state.attachmentFormset = [...state.attachmentFormset, attachmentFormset];
    },
    UPDATE_ATTACHMENT_FORM(state, payload) {
      const index = state.attachmentFormset.findIndex((el) => el.dataKey === payload.dataKey);
      if (index !== -1) state.attachmentFormset[index] = payload
    },
    REMOVE_ATTACHMENT_FORM(state, payload) {
      state.attachmentFormset = state.attachmentFormset.filter(form => form.dataKey !== payload);
    },
    CLEAR_ATTACHMENT_FORM(state) {
      state.attachmentFormset = [];
    },
    ADD_LINKED_FORM(state, dataKey) {
      state.linkedFormset = [...state.linkedFormset, { dataKey }];
    },
    UPDATE_LINKED_FORM(state, payload) {
      const index = state.linkedFormset.findIndex((el) => el.dataKey === payload.dataKey);
      if (index !== -1) state.linkedFormset[index] = payload
    },
    REMOVE_LINKED_FORM(state, payload) {
      state.linkedFormset = state.linkedFormset.filter(form => form.dataKey !== payload);
    },
  },
  getters: {
  },
  actions: {
    GET_PROJECT_FEATURES({ commit, /* dispatch */ }, project_slug) {
      axios
        .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/feature/`)
        .then((response) => {
Timothee P's avatar
Timothee P committed
          if (response.status === 200 && response.data) {
            const features = response.data.features;
            commit("SET_FEATURES", features);
            //dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated
          }
    SEND_FEATURE({ state, rootState, dispatch }, routeName) {
      let extraFormObject = {}; //* prepare an object to be flatten in properties of geojson
      for (const field of state.extra_form) {
        extraFormObject[field.name] = field.value;
      const geojson = {
        "id": state.form.feature_id,
        "type": "Feature",
        "geometry": state.form.geometry,
        "properties": {
          "title": state.form.title,
          "description": state.form.description.value,
          "status": state.form.status.value,
          "project": rootState.project_slug,
          "feature_type": rootState.feature_type.current_feature_type_slug,
      if (routeName === "editer-signalement") {
          .put(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${state.form.feature_id}/`, geojson)
Timothee P's avatar
Timothee P committed
            if (response.status === 200 && response.data) {
              console.log(response, response.data)
            }
          .post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson)
Timothee P's avatar
Timothee P committed
            if (response.status === 201 && response.data) {
              console.log(response, response.data)
              dispatch("SEND_ATTACHMENTS", response.data.id)
            }

    SEND_ATTACHMENTS({ state }, featureId) {
Timothee P's avatar
Timothee P committed
      for (let attacht of state.attachmentFormset) {
        let formdata = new FormData();
        formdata.append("file", attacht.fileToImport, attacht.fileToImport.name);
        const data = {
          title: attacht.title,
          info: attacht.info,
        }
        formdata.append("data", JSON.stringify(data));
Timothee P's avatar
Timothee P committed
          .post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/attachments/`, formdata)
Timothee P's avatar
Timothee P committed
            if (response.status === 200 && response.data) {
              console.log(response, response.data)
              return "La pièce jointe a bien été ajouté"
            }
    //DELETE_FEATURE({ state }, feature_slug) {
    //console.log("Deleting feature:", feature_slug, state)

    // POST_COMMENT({ state }, data) {
    //console.log("post comment", data, state)

    /* axios
      .post(`${DJANGO_API_BASE}feature_type/`, data)
      .then((response) => {
        const routerHistory = router.options.routerHistory 
        commit("SET_USER", response.data.user);
        router.push(routerHistory[routerHistory.length - 1] || "/")
        dispatch("GET_USER_LEVEL_PROJECTS");
      })
      .catch(() => {
        commit("SET_USER", false)
      }); */
    // },
    // EXPORT_FEATURES({ /* state */ }) {
    //console.log("Export features", state.features)
    // }
  },

}

export default feature