mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
143 lines
5.0 KiB
JavaScript
143 lines
5.0 KiB
JavaScript
import { createStore } from 'vuex'
|
|
import { getHouseholdByPerson, getCourseByPerson, getRelationship } from './api'
|
|
|
|
const debug = process.env.NODE_ENV !== 'production'
|
|
|
|
const store = createStore({
|
|
strict: debug,
|
|
state: {
|
|
persons: [],
|
|
households: [],
|
|
courses: [],
|
|
relationships: [],
|
|
householdLoadingIds: [],
|
|
},
|
|
getters: {
|
|
nodes(state) {
|
|
let nodes = []
|
|
state.persons.forEach(p => {
|
|
nodes.push(p)
|
|
})
|
|
state.households.forEach(h => {
|
|
nodes.push(h)
|
|
})
|
|
return nodes
|
|
},
|
|
edges(state) {
|
|
let edges = []
|
|
state.relationships.forEach(r => {
|
|
edges.push(r)
|
|
})
|
|
return edges
|
|
},
|
|
isHouseholdLoading: (state) => (household_id) => {
|
|
return state.householdLoadingIds.includes(household_id)
|
|
},
|
|
},
|
|
mutations: {
|
|
addPerson(state, person) {
|
|
console.log('+ addPerson', person.id)
|
|
person.label = person.text // vis need label
|
|
person.id = `person_${person.id}` // vis need unique id
|
|
state.persons.push(person)
|
|
},
|
|
addHousehold(state, household) {
|
|
console.log('+ addHousehold', household.id)
|
|
household.label = `Ménage n° ${household.id}` // vis need label
|
|
household.id = `household_${household.id}` // vis need unique id
|
|
state.households.push(household)
|
|
},
|
|
addCourse(state, course) {
|
|
console.log('+ addCourse', course.id)
|
|
state.courses.push(course)
|
|
},
|
|
addRelationship(state, relationship) {
|
|
console.log('+ addRelationship', relationship)
|
|
state.relationships.push(relationship)
|
|
},
|
|
markHouseholdLoading(state, id) {
|
|
console.log('..loading household', id)
|
|
state.householdLoadingIds.push(id)
|
|
},
|
|
unmarkHouseholdLoading(state, id) {
|
|
state.householdLoadingIds = state.householdLoadingIds.filter(i => i !== id)
|
|
},
|
|
},
|
|
actions: {
|
|
addPerson({ commit, dispatch }, person) {
|
|
commit('addPerson', person)
|
|
dispatch('fetchInfoForPerson', person)
|
|
},
|
|
fetchInfoForPerson({ dispatch }, person) {
|
|
dispatch('fetchHouseholdForPerson', person)
|
|
//dispatch('fetchCourseByPerson', person)
|
|
//dispatch('fetchRelationship', person)
|
|
},
|
|
|
|
/**
|
|
* Fetch person current household if it is not already loading
|
|
* check first isHouseholdLoading to fetch household once
|
|
*/
|
|
fetchHouseholdForPerson({ commit, getters, dispatch }, person) {
|
|
console.log(' isHouseholdLoading ?', getters.isHouseholdLoading(person.current_household_id))
|
|
if (! getters.isHouseholdLoading(person.current_household_id)) {
|
|
commit('markHouseholdLoading', person.current_household_id)
|
|
getHouseholdByPerson(person)
|
|
.then(household => new Promise(resolve => {
|
|
//console.log('getHouseholdByPerson', household)
|
|
commit('addHousehold', household)
|
|
dispatch('addLinkFromPersonsToHousehold', household)
|
|
resolve()
|
|
})
|
|
).catch( () => {
|
|
commit('unmarkHouseholdLoading', person.current_household_id)
|
|
})
|
|
}
|
|
},
|
|
|
|
addLinkFromPersonsToHousehold({ commit }, household) {
|
|
const members = household.members.filter(v => household.current_members_id.includes(v.id))
|
|
members.forEach(m => {
|
|
//console.log('-> addLink from person', m.person.id, 'to household', m.person.current_household_id)
|
|
commit('addRelationship', {
|
|
from: `${m.person.type}_${m.person.id}`,
|
|
to: `household_${m.person.current_household_id}`,
|
|
id: `p${m.person.id}-h${m.person.current_household_id}`
|
|
})
|
|
})
|
|
|
|
},
|
|
|
|
/**
|
|
* Fetch person current AccompanyingCourses
|
|
*/
|
|
fetchCourseByPerson({ commit, getters }, person) {
|
|
console.log('fetchCourseByPerson', person)
|
|
getCourseByPerson(person)
|
|
.then(course => new Promise(resolve => {
|
|
console.log('getCourseByPerson', course)
|
|
commit('addCourse', course)
|
|
resolve()
|
|
}))
|
|
|
|
},
|
|
|
|
/**
|
|
* Fetch Relationship
|
|
*/
|
|
fetchRelationship({ commit, getters }, person) {
|
|
console.log('fetchRelationship', person)
|
|
getRelationship(person)
|
|
.then(relationship => new Promise(resolve => {
|
|
console.log('getRelationship', relationship)
|
|
commit('addRelationship', relationship)
|
|
resolve()
|
|
}))
|
|
|
|
},
|
|
|
|
}
|
|
})
|
|
|
|
export { store }
|