| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | import {sortByString} from '@theme/util/filterHelpers' | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  | import { getResourceIndexInLibrary } from '@theme/util' | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |   namespaced: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   state: { | 
					
						
							|  |  |  |     monsters: [], | 
					
						
							| 
									
										
										
										
											2020-05-04 16:52:03 +02:00
										 |  |  |     notPrintedMonsters: [] | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getters: { | 
					
						
							|  |  |  |     monsters: state => state.monsters, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   actions: { | 
					
						
							|  |  |  |     reset: ({ commit }) => { | 
					
						
							|  |  |  |       commit('resetMonsters') | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     updateMonsters: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('setMonsters', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     addMonster: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('addMonster', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     removeMonster: ({ commit }, payload) => { | 
					
						
							|  |  |  |       commit('removeMonster', payload) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mutations: { | 
					
						
							|  |  |  |     initialiseStore (state) { | 
					
						
							|  |  |  | 			// Récupération des données utilisateurs depuis le navigateur
 | 
					
						
							|  |  |  | 			if(localStorage.getItem('myMonsters') && localStorage.getItem('myMonsters') !== undefined) { | 
					
						
							|  |  |  |         let localStorageData = JSON.parse(localStorage.getItem('myMonsters')) | 
					
						
							| 
									
										
										
										
											2020-05-04 16:52:03 +02:00
										 |  |  |         if (localStorageData.monsters) { | 
					
						
							|  |  |  |           state.monsters = localStorageData.monsters | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (localStorageData.notPrintedMonsters) { | 
					
						
							|  |  |  |           state.notPrintedMonsters = localStorageData.notPrintedMonsters | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  |     setMonsters: (state, payload) => { | 
					
						
							|  |  |  |       state.monsters = payload | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     resetMonsters: (state) => { | 
					
						
							|  |  |  |       state.monsters = [] | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     addMonster: (state, payload) => { | 
					
						
							|  |  |  |       state.monsters.push(payload) | 
					
						
							|  |  |  |       state.monsters.sort((a, b) => { return sortByString(a.title, b.title) }) | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     updateMonster: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let monsterIndex = getResourceIndexInLibrary(payload, state.monsters) | 
					
						
							|  |  |  |       if (monsterIndex >= 0) { | 
					
						
							|  |  |  |         state.monsters[monsterIndex] = payload | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     removeMonster: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let monsterIndex = getResourceIndexInLibrary(payload, state.monsters) | 
					
						
							|  |  |  |       if (monsterIndex >= 0) { | 
					
						
							|  |  |  |         state.monsters.splice(monsterIndex, 1) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-05-04 16:52:03 +02:00
										 |  |  |     setNotPrintedMonsters: (state, payload) => { | 
					
						
							|  |  |  |       state.notPrintedMonsters = payload | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     addNotPrintedMonster: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let monsterIndex = getResourceIndexInLibrary(payload, state.notPrintedMonsters) | 
					
						
							| 
									
										
										
										
											2020-05-04 16:52:03 +02:00
										 |  |  |       if (!monsterIndex >= 0) { | 
					
						
							|  |  |  |         state.notPrintedMonsters.push(payload) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     removeNotPrintedMonster: (state, payload) => { | 
					
						
							| 
									
										
										
										
											2020-05-24 13:32:31 +02:00
										 |  |  |       let monsterIndex = getResourceIndexInLibrary(payload, state.notPrintedMonsters) | 
					
						
							| 
									
										
										
										
											2020-05-04 16:52:03 +02:00
										 |  |  |       if (monsterIndex >= 0) { | 
					
						
							|  |  |  |         state.notPrintedMonsters.splice(monsterIndex, 1) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-22 11:17:05 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |