cleaning store

This commit is contained in:
Mathieu Jaumotte 2021-11-05 14:58:52 +01:00
parent bfcd420cdf
commit e911dd30f5
2 changed files with 45 additions and 71 deletions

View File

@ -142,7 +142,7 @@ export default {
computed: {
...mapGetters(['nodes', 'edges',
// not used
'isInWhitelist', 'isHouseholdLoading', 'isCourseLoaded', 'isRelationshipLoaded', 'isPersonLoaded', 'isExcludedNode', 'countLinksByNode', 'getParticipationsByCourse', 'getMembersByHousehold', 'getFoldedPersons',
'isInWhitelist', 'isHouseholdLoading', 'isCourseLoaded', 'isRelationshipLoaded', 'isPersonLoaded', 'isExcludedNode', 'countLinksByNode', 'getParticipationsByCourse', 'getMembersByHousehold', 'getWithFoldedPersons',
]),
...mapState(['households', 'courses', 'excludedNodesIds', 'persons',
// not used
@ -219,7 +219,7 @@ export default {
eventHub.off('delete-relationship-modal', this.deleteRelationshipModal)
},
mounted() {
//console.log('=== mounted: init graph')hill
//console.log('=== mounted: init graph')
this.initGraph()
this.listenOnGraph()
this.getRelationsList()

View File

@ -39,11 +39,7 @@ const store = createStore({
return nodes
},
edges(state) {
let edges = []
state.links.forEach(l => {
edges.push(l)
})
return edges
return state.links
},
isInWhitelist: (state) => (person_id) => {
return state.whitelistIds.includes(person_id)
@ -80,7 +76,6 @@ const store = createStore({
},
getParticipationsByCourse: (state) => (course_id) => {
//console.log('getParticipationsByCourse', course_id)
const course = state.courses.filter(c => c.id === course_id)[0]
const currentParticipations = course.participations.filter(p => p.endDate === null)
console.log('get persons in', course_id, currentParticipations.map(p => p.person.id),
@ -89,7 +84,6 @@ const store = createStore({
},
getMembersByHousehold: (state) => (household_id) => {
//console.log('getMembersByHousehold', household_id)
const household = state.households.filter(h => h.id === household_id)[0]
const currentMembers = household.members.filter(m => household.current_members_id.includes(m.id))
console.log('get persons in', household_id, currentMembers.map(m => m.person.id),
@ -98,74 +92,63 @@ const store = createStore({
},
/**
* This getter is a little bit mysterious :
* The 2 previous getters return complete array, but folded (missing) persons are not taken into consideration and are not displayed (!?!)
* This getter compare input array (participations|members) to personLoadedIds array
* It return an array of folded persons
* and return complete array with folded persons taken into consideration
*/
getFoldedPersons: (state) => (array) => {
let folded = []
getWithFoldedPersons: (state) => (array) => {
let withFolded = []
array.forEach(item => {
let id = splitId(item.person.id, 'id')
if (state.personLoadedIds.includes(id)) {
folded.push(state.persons.filter(person => person._id === id)[0])
withFolded.push(state.persons.filter(person => person._id === id)[0])
}
})
//console.log('array', array.map(item => item.person.id))
console.log('get FoldedPersons', folded.map(f => f.id))
return folded
console.log('get FoldedPersons', withFolded.map(f => f.id))
return withFolded
},
},
mutations: {
addPerson(state, [person, options]) {
//console.log('+ addPerson', person.id)
person.group = person.type
person._id = person.id
person.id = `person_${person.id}`
person.label = `*${person.text}*\n_${getGender(person.gender)} - ${getAge(person.birthdate)}_`
person.folded = false
if (options.folded) { // used by missing persons
// folded is used for missing persons
if (options.folded) {
person.title = visMessages.fr.visgraph.click_to_expand
person._label = person.label // keep label
person.label = null
person.folded = true
}
state.persons.push(person)
},
addHousehold(state, household) {
//console.log('+ addHousehold', household.id)
household.group = household.type
household._id = household.id
household.label = `${visMessages.fr.visgraph.Household}${household.id}`
household.id = `household_${household.id}`
state.households.push(household)
},
addCourse(state, course) {
//console.log('+ addCourse', course.id)
course.group = course.type
course._id = course.id
course.label = `${visMessages.fr.visgraph.Course}${course.id}`
course.id = `accompanying_period_${course.id}`
state.courses.push(course)
},
addRelationship(state, relationship) {
//console.log('+ addRelationship', relationship.id)
relationship.group = relationship.type
relationship._id = relationship.id
relationship.id = `relationship_${relationship.id}`
state.relationships.push(relationship)
},
addLink(state, link) {
//console.log('+ addLink from', link.from, 'to', link.to)
state.links.push(link)
},
@ -211,7 +194,7 @@ const store = createStore({
//// unfold
unfoldPerson(state, person) {
console.log('unfoldPerson', person)
//console.log('unfoldPerson', person)
person.label = person._label
delete person._label
delete person.title
@ -220,7 +203,11 @@ const store = createStore({
},
actions: {
/**
* 1) Add a person in state
* Expand loop (steps 1->10), always start from a person.
* Fetch household, courses, relationships, and others persons.
* These persons are "missing" and will be first display in fold mode.
*
* 1) Add a new person
* @param object
* @param person
*/
@ -256,7 +243,8 @@ const store = createStore({
getHouseholdByPerson(person)
.then(household => new Promise(resolve => {
commit('addHousehold', household)
//dispatch('addExcludedNode', household.id) // uncheck layer when added
// DISABLED: in init or expand loop, layer is uncheck when added
//dispatch('addExcludedNode', household.id)
dispatch('addLinkFromPersonsToHousehold', household)
resolve()
})
@ -286,7 +274,6 @@ const store = createStore({
width: getHouseholdWidth(m),
})
if (!getters.isPersonLoaded(m.person.id)) {
console.log('addMissingPerson from household', household.id)
dispatch('addMissingPerson', [m.person, household])
}
})
@ -298,10 +285,8 @@ const store = createStore({
* @param person
*/
fetchCoursesByPerson({ commit, dispatch }, person) {
//console.log('fetchCoursesByPerson', person)
getCoursesByPerson(person)
.then(courses => new Promise(resolve => {
//console.log('fetch courses', courses.length)
dispatch('addCourses', courses)
resolve()
}))
@ -313,15 +298,13 @@ const store = createStore({
* @param courses
*/
addCourses({ 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)
commit('addExcludedNode', course.id) // init: layer uncheck when added
commit('addExcludedNode', course.id) // in init or expand loop, layer is uncheck when added
dispatch('addLinkFromPersonsToCourse', course)
}
})
@ -360,7 +343,6 @@ const store = createStore({
//console.log('fetchRelationshipByPerson', person)
getRelationshipsByPerson(person)
.then(relationships => new Promise(resolve => {
//console.log('fetch relationships', relationships.length)
dispatch('addRelationships', relationships)
resolve()
}))
@ -375,7 +357,6 @@ const store = createStore({
relationships.forEach(relationship => {
//console.log(' isRelationshipLoaded ?', getters.isRelationshipLoaded(relationship.id))
if (! getters.isRelationshipLoaded(relationship.id)) {
//console.log('relationship', relationship.id)
commit('markRelationshipLoaded', relationship.id)
commit('addRelationship', relationship)
dispatch('addLinkFromRelationship', relationship)
@ -386,24 +367,25 @@ const store = createStore({
/**
* 10) Add an edge for each relationship (person -> person)
* @param object
* @param r (relationship)
* @param relationship
*/
addLinkFromRelationship({ commit, getters, dispatch }, r) {
//console.log('-> addLink from person', r.fromPerson.id, 'to person', r.toPerson.id)
addLinkFromRelationship({ commit, getters, dispatch }, relationship) {
//console.log('-> addLink from person', relationship.fromPerson.id, 'to person', relationship.toPerson.id)
commit('addLink', {
from: `person_${r.fromPerson.id}`,
to: `person_${r.toPerson.id}`,
id: 'relationship_' + splitId(r.id,'id') + '-person_' + r.fromPerson.id + '-person_' + r.toPerson.id,
arrows: getRelationshipDirection(r),
from: `person_${relationship.fromPerson.id}`,
to: `person_${relationship.toPerson.id}`,
id: 'relationship_' + splitId(relationship.id,'id')
+ '-person_' + relationship.fromPerson.id + '-person_' + relationship.toPerson.id,
arrows: getRelationshipDirection(relationship),
color: 'lightblue',
font: { color: '#33839d' },
dashes: true, //physics: false,
label: getRelationshipLabel(r),
title: getRelationshipTitle(r),
dashes: true,
label: getRelationshipLabel(relationship),
title: getRelationshipTitle(relationship),
})
for (let person of [r.fromPerson, r.toPerson]) {
for (let person of [relationship.fromPerson, relationship.toPerson]) {
if (!getters.isPersonLoaded(person.id)) {
dispatch('addMissingPerson', [person, r])
dispatch('addMissingPerson', [person, relationship])
}
}
},
@ -419,13 +401,13 @@ const store = createStore({
commit('markPersonLoaded', person.id)
commit('addPerson', [person, { folded: true }])
if (getters.isExcludedNode(parent.id)) {
commit('addExcludedNode', person.id) // init: exclude missing persons if parent is excluded
// in init or expand loop, exclude too missing persons if parent have been excluded
commit('addExcludedNode', person.id)
}
},
/**
* =============================================================================
*
* ==================================================================
* Triggered by a vis-network event when clicking on a Course Node.
* Each folded node is unfold, then expanded with fetch infos
* @param object
@ -433,15 +415,12 @@ const store = createStore({
*/
unfoldPersonsByCourse({ getters, commit, dispatch }, course) {
const participations = getters.getParticipationsByCourse(course.id)
getters.getFoldedPersons(participations)
getters.getWithFoldedPersons(participations)
.forEach(person => {
if (person.folded === true) {
console.log('-=. unfold and expand person', person.id)
commit('unfoldPerson', person)
dispatch('fetchInfoForPerson', person)
//} else {
//console.log('person is not folded', person)
//throw 'person is not folded'
}
})
},
@ -454,28 +433,23 @@ const store = createStore({
*/
unfoldPersonsByHousehold({ getters, commit, dispatch }, household) {
const members = getters.getMembersByHousehold(household.id)
getters.getFoldedPersons(members)
getters.getWithFoldedPersons(members)
.forEach(person => {
if (person.folded === true) {
console.log('-=. unfold and expand person', person.id)
commit('unfoldPerson', person)
dispatch('fetchInfoForPerson', person)
//} else {
//console.log('person is not folded', person)
//throw 'person is not folded'
}
})
},
/**
* =============================================================================
*
* ==================================================================
* For an excluded node, add|remove relative persons excluded too
* @param object
* @param array (add|remove action, id)
*/
excludedNode({ getters, commit }, [action, id]) {
const personGroup = () => {
switch (splitId(id, 'type')) {
case 'accompanying_period':
@ -488,7 +462,7 @@ const store = createStore({
}
if (action === 'add') {
commit('addExcludedNode', id)
getters.getFoldedPersons(personGroup())
getters.getWithFoldedPersons(personGroup())
.forEach(person => {
// countLinks < 2 but parent has just already been added !
if (!getters.isInWhitelist(person.id) && getters.countLinksByNode(person.id) < 1) {
@ -498,7 +472,7 @@ const store = createStore({
}
if (action === 'remove') {
commit('removeExcludedNode', id)
getters.getFoldedPersons(personGroup())
getters.getWithFoldedPersons(personGroup())
.forEach(person => {
commit('removeExcludedNode', person.id)
})