mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
177 lines
6.4 KiB
JavaScript
177 lines
6.4 KiB
JavaScript
import { createStore } from 'vuex'
|
|
import { getHouseholdByPerson, getCoursesByPerson, getRelationship } from './api'
|
|
import { adapt2vis } from './vis-network'
|
|
|
|
const debug = process.env.NODE_ENV !== 'production'
|
|
|
|
const store = createStore({
|
|
strict: debug,
|
|
state: {
|
|
persons: [],
|
|
households: [],
|
|
courses: [],
|
|
relationships: [],
|
|
householdLoadingIds: [],
|
|
courseLoadedIds: []
|
|
},
|
|
getters: {
|
|
nodes(state) {
|
|
let nodes = []
|
|
state.persons.forEach(p => {
|
|
nodes.push(p)
|
|
})
|
|
state.households.forEach(h => {
|
|
nodes.push(h)
|
|
})
|
|
state.courses.forEach(c => {
|
|
nodes.push(c)
|
|
})
|
|
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)
|
|
},
|
|
isCourseLoaded: (state) => (course_id) => {
|
|
return state.courseLoadedIds.includes(course_id)
|
|
},
|
|
},
|
|
mutations: {
|
|
addPerson(state, person) {
|
|
console.log('+ addPerson', person.id)
|
|
state.persons.push(adapt2vis(person))
|
|
},
|
|
addHousehold(state, household) {
|
|
console.log('+ addHousehold', household.id)
|
|
state.households.push(adapt2vis(household))
|
|
},
|
|
addCourse(state, course) {
|
|
console.log('+ addCourse', course.id)
|
|
state.courses.push(adapt2vis(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)
|
|
},
|
|
markCourseLoaded(state, id) {
|
|
state.courseLoadedIds.push(id)
|
|
},
|
|
unmarkCourseLoaded(state, id) {
|
|
state.courseLoadedIds = state.courseLoadedIds.filter(i => i !== id)
|
|
},
|
|
},
|
|
actions: {
|
|
addPerson({ commit, dispatch }, person) {
|
|
commit('addPerson', person)
|
|
dispatch('fetchInfoForPerson', person)
|
|
},
|
|
fetchInfoForPerson({ dispatch }, person) {
|
|
dispatch('fetchHouseholdForPerson', person)
|
|
dispatch('fetchCoursesByPerson', 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
|
|
*/
|
|
fetchCoursesByPerson({ dispatch }, person) {
|
|
//console.log('fetchCoursesByPerson', person)
|
|
getCoursesByPerson(person)
|
|
.then(courses => new Promise(resolve => {
|
|
console.log('fetch courses', courses.length)
|
|
dispatch('addCourse', courses)
|
|
resolve()
|
|
}))
|
|
|
|
},
|
|
addCourse({ commit, getters, dispatch }, courses) {
|
|
//console.log('addCourse', courses)
|
|
let currentCourses = courses.filter(c => c.closingDate === null)
|
|
currentCourses.forEach(course => {
|
|
console.log(' isCourseLoaded ?', getters.isCourseLoaded(course.id))
|
|
if (! getters.isCourseLoaded(course.id)) {
|
|
//console.log('course', course.id)
|
|
commit('markCourseLoaded', course.id)
|
|
commit('addCourse', course)
|
|
dispatch('addLinkFromPersonsToCourse', course)
|
|
}
|
|
})
|
|
},
|
|
addLinkFromPersonsToCourse({ commit }, course) {
|
|
let currentParticipations = course.participations.filter(p => p.endDate === null)
|
|
console.log(' participations', currentParticipations.length)
|
|
currentParticipations.forEach(p => {
|
|
commit('addRelationship', {
|
|
from: `${p.person.type}_${p.person.id}`,
|
|
to: `${course.id}`,
|
|
id: `p${p.person.id}-c`+ course.id.split('_')[2]
|
|
})
|
|
})
|
|
},
|
|
|
|
|
|
/**
|
|
* 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 }
|