Skip to content
Snippets Groups Projects
Commit 01b63128 authored by Timothee P's avatar Timothee P :sunflower:
Browse files

Import attachment file

parent eb5447a5
No related branches found
No related tags found
No related merge requests found
...@@ -34,27 +34,24 @@ ...@@ -34,27 +34,24 @@
<div class="required field"> <div class="required field">
<label>Fichier (PDF, PNG, JPEG)</label> <label>Fichier (PDF, PNG, JPEG)</label>
<!-- // todo : mettre en place la sélection de fichier --> <!-- // todo : mettre en place la sélection de fichier -->
<label <!-- @click="selectFile" -->
@click="selectFile" <label class="ui icon button" for="attachment_file">
class="ui icon button"
:for="form.attachment_file.id_for_label"
>
<i class="file icon"></i> <i class="file icon"></i>
<span v-if="form.attachment_file.value" class="label">{{ <span v-if="form.attachment_file.value" class="label">{{
form.attachment_file.value form.attachment_file.value
}}</span> }}</span>
<span v-else class="label">Sélectionner un fichier ...</span> <span v-else class="label">Sélectionner un fichier ... </span>
</label> </label>
<!-- // todo: récupérer la valeur :accept="IMAGE_FORMAT" -->
<!-- @change="processImgData" -->
<input <input
@change="onFileChange"
type="file" type="file"
style="display: none" style="display: none"
:name="form.attachment_file.html_name" :name="form.attachment_file.html_name"
class="image_file" id="attachment_file"
:id="form.attachment_file.id_for_label"
@change="updateStore"
/> />
<!-- // todo: récupérer la valeur :accept="IMAGE_FORMAT" -->
<!-- @change="processImgData" -->
{{ form.attachment_file.errors }} {{ form.attachment_file.errors }}
</div> </div>
</div> </div>
...@@ -81,6 +78,7 @@ export default { ...@@ -81,6 +78,7 @@ export default {
data() { data() {
return { return {
fileToImport: null,
form: { form: {
title: { title: {
errors: null, errors: null,
...@@ -94,7 +92,6 @@ export default { ...@@ -94,7 +92,6 @@ export default {
}, },
attachment_file: { attachment_file: {
errors: null, errors: null,
id_for_label: "titre",
html_name: "titre", html_name: "titre",
label: "Titre", label: "Titre",
value: "", value: "",
...@@ -116,9 +113,13 @@ export default { ...@@ -116,9 +113,13 @@ export default {
methods: { methods: {
initForm(attachmentForm) { initForm(attachmentForm) {
console.log("initForm");
for (let el in attachmentForm) { for (let el in attachmentForm) {
if (el && this.form[el]) { if (el && this.form[el]) {
this.form[el].value = attachmentForm[el]; this.form[el].value =
el === "attachment_file"
? attachmentForm[el].split("/").pop() //* keep only the file name
: attachmentForm[el];
} }
} }
}, },
...@@ -128,15 +129,24 @@ export default { ...@@ -128,15 +129,24 @@ export default {
this.attachmentForm.dataKey this.attachmentForm.dataKey
); );
}, },
selectFile() {},
updateStore() { updateStore() {
this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", { this.$store.commit("feature/UPDATE_ATTACHMENT_FORM", {
dataKey: this.attachmentForm.dataKey, dataKey: this.attachmentForm.dataKey,
title: this.form.title.value, title: this.form.title.value,
attachment_file: this.form.attachment_file.value, attachment_file: this.form.attachment_file.value,
info: this.form.info.value, info: this.form.info.value,
fileToImport: this.fileToImport,
}); });
}, },
onFileChange(e) {
const files = e.target.files || e.dataTransfer.files;
console.log(e, files);
if (!files.length) return;
this.fileToImport = files[0];
this.form.attachment_file.value = files[0].name;
this.updateStore();
//this.$store.commit("feature_type/SET_FILE_TO_IMPORT", this.fileToImport);
},
}, },
mounted() { mounted() {
......
...@@ -58,9 +58,11 @@ const feature = { ...@@ -58,9 +58,11 @@ const feature = {
axios axios
.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/feature/`) .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${project_slug}/feature/`)
.then((response) => { .then((response) => {
const features = response.data.features; if (response.status === 200 && response.data) {
commit("SET_FEATURES", features); const features = response.data.features;
//dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated commit("SET_FEATURES", features);
//dispatch("map/ADD_FEATURES", null, { root: true }); //todo: should check if map was initiated
}
}) })
.catch((error) => { .catch((error) => {
throw error; throw error;
...@@ -90,7 +92,9 @@ const feature = { ...@@ -90,7 +92,9 @@ const feature = {
axios axios
.put(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${state.form.feature_id}/`, geojson) .put(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${state.form.feature_id}/`, geojson)
.then((response) => { .then((response) => {
console.log(response, response.data) if (response.status === 200 && response.data) {
console.log(response, response.data)
}
}) })
.catch((error) => { .catch((error) => {
throw error; throw error;
...@@ -99,8 +103,10 @@ const feature = { ...@@ -99,8 +103,10 @@ const feature = {
axios axios
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson) .post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/`, geojson)
.then((response) => { .then((response) => {
console.log(response, response.data) if (response.status === 201 && response.data) {
dispatch("SEND_ATTACHMENTS", response.data.id) console.log(response, response.data)
dispatch("SEND_ATTACHMENTS", response.data.id)
}
}) })
.catch((error) => { .catch((error) => {
throw error; throw error;
...@@ -109,11 +115,21 @@ const feature = { ...@@ -109,11 +115,21 @@ const feature = {
}, },
SEND_ATTACHMENTS({ state }, featureId) { SEND_ATTACHMENTS({ state }, featureId) {
for (let attachment of state.attachmentFormset) { 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));
axios axios
.post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/attachments/`, attachment) .post(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}features/${featureId}/attachments/`, formdata)
.then((response) => { .then((response) => {
console.log(response, response.data) if (response.status === 200 && response.data) {
console.log(response, response.data)
return "La pièce jointe a bien été ajouté"
}
}) })
.catch((error) => { .catch((error) => {
throw error; throw error;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment