admin front: create new besoin on puissance d'agir is ok

This commit is contained in:
2024-06-03 12:10:08 +02:00
parent b260ab1b87
commit eeed61df4b
5 changed files with 172 additions and 7 deletions

View File

@@ -5,6 +5,8 @@ import { ConcernementsStore } from '@stores/concernements'
import { UserStore } from '@/stores/user'
import { CommonStore } from '@/stores/common'
import REST from '@api/rest-axios'
import CartoucheLayout from '@components/layout/CartoucheLayout.vue';
import SvgIcon from '@jamescoyle/vue-icon';
@@ -14,6 +16,7 @@ import { mdiPencilPlus } from '@mdi/js';
import { mdiPencilPlusOutline } from '@mdi/js';
import { mdiRhombus } from '@mdi/js';
import { mdiRhombusOutline } from '@mdi/js';
import { mdiStickerPlusOutline } from '@mdi/js';
export default {
@@ -26,11 +29,13 @@ export default {
pencilplus_path: mdiPencilPlus,
pencilplusoutline_path: mdiPencilPlusOutline,
rhombus_path: mdiRhombus,
rhombusoutline_path: mdiRhombusOutline
rhombusoutline_path: mdiRhombusOutline,
mdiStickerPlusOutline_path: mdiStickerPlusOutline,
reloading_concernements: false,
}
},
computed: {
...mapState(UserStore,['isloggedin']),
...mapState(UserStore,['isloggedin', 'csrf_token']),
...mapState(ConcernementsStore,['opened_concernement',
'ct_concernement',
'ct_entite'
@@ -45,6 +50,7 @@ export default {
},
methods: {
...mapActions(CommonStore,['setHoverElmt']),
...mapActions(ConcernementsStore, ['reloadConcernementBesoins']),
onClickBesoin(id){
console.log("onClickBesoin", id);
this.opened_besoin_id = id === this.opened_besoin_id ? null : id;
@@ -61,7 +67,89 @@ export default {
bid: rid ? bid : null,
no_popup: true
});
}
},
addBesoin(e){
console.log('add besoin');
this.reloading_concernements = true;
// 1 create besoin node
this.createBesoinNode()
.then((besoin) => {
console.log('createBesoinNode then node', besoin);
// 2 record new besoin in concernement's field_besoin
this.recordConcernementBesoinField(besoin)
.then((concernement) => {
console.log('concernement', concernement);
// reload the map item
this.reloadConcernementBesoins(concernement.nid[0].value)
.then(() => {
this.reloading_concernements = false;
});
})
})
},
createBesoinNode(){
return new Promise((resolve, reject) => {
// 1 create entite node
const params_node_besoin = {
type: 'besoin',
body: [{value:'Description à personaliser'}],
field_confidentialite: [{value:'confidentialite_public'}]
};
const configs = {
headers: {'X-CSRF-Token': this.csrf_token}
};
REST.post(`/node?_format=json`, params_node_besoin, configs)
.then(({ data }) => {
console.log('REST post new node besoin', data);
resolve(data)
})
.catch(error => {
console.warn(`Issue with post new node besoin`, error)
reject(error)
})
})
},
recordConcernementBesoinField(new_besoin){
return new Promise((resolve, reject) => {
// 3 record concernement field_entite
// get all the field_entite values, we don't want to ersae everything
let besoins = [];
this.opened_concernement.besoins.forEach((besoin) =>{
besoins.push({
target_id: besoin.id
})
})
// add the new field value
besoins.push({
target_id: new_besoin.nid[0].value
})
console.log('besoins', besoins);
const params_node = {
type: 'concernement',
nid: [{value: this.cid}],
'field_besoin': besoins
};
const configs = {
headers: {'X-CSRF-Token': this.csrf_token}
};
REST.patch(`/node/${this.cid}?_format=json`, params_node, configs)
.then(({ data }) => {
console.log('REST patch entite new field_entite', data)
resolve(data)
})
.catch(error => {
console.warn(`Issue with patch node entite field_entite`, error)
reject(error)
})
// resolve('test')
})
},
},
components: {
CartoucheLayout,
@@ -76,7 +164,9 @@ export default {
<!-- <template v-slot:header>
</template> -->
<template v-slot:main>
<ul class="besoins">
<li
v-for="besoin in opened_concernement.besoins"
@@ -156,6 +246,15 @@ export default {
<template v-slot:footer>
<template v-if="opened_concernement.can_update">
<div v-if="!reloading_concernements" @click="addBesoin" class="add-besoin-btn">
<span>Ajouter un besoin</span>
<svg-icon type="mdi" :path="mdiStickerPlusOutline_path"/>
</div>
<div v-else class="add-besoin-btn">
<div class="loading">Chargement</div>
</div>
</template>
</template>
</CartoucheLayout>