mirror of
				https://github.com/KevinMidboe/planetposen.git
				synced 2025-10-29 17:50:32 +00:00 
			
		
		
		
	Cart store module.
This commit is contained in:
		
							
								
								
									
										44
									
								
								frontend/modules/cartModule.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								frontend/modules/cartModule.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
|  | ||||
| const updateLocalStorage = (key, value) => { | ||||
|   localStorage.setItem(key, JSON.stringify(value)); | ||||
| } | ||||
| const getParsedFromLocalStorage = key => { | ||||
|   const value = localStorage.getItem(key); | ||||
|   try { | ||||
|     return JSON.parse(value) | ||||
|   } catch { | ||||
|     return null  | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default { | ||||
|   namespaced: true, | ||||
|   state: { | ||||
|     inventory: getParsedFromLocalStorage('inventory') || [], | ||||
|   }, | ||||
|   getters: { | ||||
|     inventory: state => { | ||||
|       return state.inventory; | ||||
|     } | ||||
|   }, | ||||
|   mutations: { | ||||
|     ADD_ITEM_TO_CART: (state, item) => { | ||||
|       state.inventory.push(item); | ||||
|  | ||||
|       updateLocalStorage('inventory', state.inventory) | ||||
|     }, | ||||
|     REMOVE_ITEM_FROM_CART: (state, item) => { | ||||
|       state.inventory = state.inventory.filter(i => i !== item) | ||||
|  | ||||
|       updateLocalStorage('inventory', state.inventory) | ||||
|     } | ||||
|   }, | ||||
|   actions: { | ||||
|     addItemToCart({ commit }, item) { | ||||
|       commit("ADD_ITEM_TO_CART", item); | ||||
|     }, | ||||
|     removeItemFromCart({ commit }, item) { | ||||
|       commit('REMOVE_ITEM_FROM_CART', item) | ||||
|     } | ||||
|   } | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user