1.3.4 User-Scoped Storage Pattern: Add StorageService integration across modules for improved data management
- Introduced STORAGE_SERVICE token in the DI container for consistent service registration. - Updated BaseService to include storageService as a dependency, ensuring proper initialization and error handling. - Refactored ChatService to utilize storageService for managing unread messages and peers, replacing localStorage usage. - Enhanced MarketStore to save and load orders using storageService, improving data persistence and user experience. - Registered storageService in the base module, ensuring it is initialized and disposed of correctly. This integration streamlines data handling across the application, promoting better maintainability and consistency.
This commit is contained in:
parent
3abdd2d7d9
commit
3cf10b1db4
6 changed files with 285 additions and 103 deletions
|
|
@ -4,6 +4,7 @@ import { invoiceService } from '@/lib/services/invoiceService'
|
|||
import { paymentMonitor } from '@/lib/services/paymentMonitor'
|
||||
import { nostrmarketService } from '../services/nostrmarketService'
|
||||
import { useAuth } from '@/composables/useAuth'
|
||||
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
|
||||
import type { LightningInvoice } from '@/lib/services/invoiceService'
|
||||
|
||||
|
||||
|
|
@ -16,12 +17,7 @@ import type {
|
|||
|
||||
export const useMarketStore = defineStore('market', () => {
|
||||
const auth = useAuth()
|
||||
|
||||
// Helper function to get user-specific storage key
|
||||
const getUserStorageKey = (baseKey: string) => {
|
||||
const userPubkey = auth.currentUser?.value?.pubkey
|
||||
return userPubkey ? `${baseKey}_${userPubkey}` : baseKey
|
||||
}
|
||||
const storageService = injectService(SERVICE_TOKENS.STORAGE_SERVICE) as any
|
||||
// Core market state
|
||||
const markets = ref<Market[]>([])
|
||||
const stalls = ref<Stall[]>([])
|
||||
|
|
@ -681,63 +677,42 @@ export const useMarketStore = defineStore('market', () => {
|
|||
|
||||
// Persistence methods
|
||||
const saveOrdersToStorage = () => {
|
||||
try {
|
||||
const storageKey = getUserStorageKey('market_orders')
|
||||
localStorage.setItem(storageKey, JSON.stringify(orders.value))
|
||||
|
||||
// Debug: Check what's being saved
|
||||
const orderCount = Object.keys(orders.value).length
|
||||
const paidOrders = Object.values(orders.value).filter(o => o.paymentStatus === 'paid' || o.status === 'paid')
|
||||
|
||||
console.log('💾 Saved orders to localStorage:', {
|
||||
storageKey,
|
||||
totalOrders: orderCount,
|
||||
paidOrders: paidOrders.length,
|
||||
orderStatuses: Object.values(orders.value).map(o => ({
|
||||
id: o.id?.slice(-8),
|
||||
status: o.status,
|
||||
paymentStatus: o.paymentStatus,
|
||||
hasPaymentRequest: !!o.paymentRequest
|
||||
}))
|
||||
})
|
||||
} catch (error) {
|
||||
console.warn('Failed to save orders to localStorage:', error)
|
||||
}
|
||||
storageService.setUserData('market_orders', orders.value)
|
||||
|
||||
// Debug: Check what's being saved
|
||||
const orderCount = Object.keys(orders.value).length
|
||||
const paidOrders = Object.values(orders.value).filter(o => o.paymentStatus === 'paid' || o.status === 'paid')
|
||||
|
||||
console.log('💾 Saved orders to storage:', {
|
||||
totalOrders: orderCount,
|
||||
paidOrders: paidOrders.length,
|
||||
orderStatuses: Object.values(orders.value).map(o => ({
|
||||
id: o.id?.slice(-8),
|
||||
status: o.status,
|
||||
paymentStatus: o.paymentStatus,
|
||||
hasPaymentRequest: !!o.paymentRequest
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
const loadOrdersFromStorage = () => {
|
||||
try {
|
||||
const storageKey = getUserStorageKey('market_orders')
|
||||
const stored = localStorage.getItem(storageKey)
|
||||
if (stored) {
|
||||
const parsedOrders = JSON.parse(stored)
|
||||
orders.value = parsedOrders
|
||||
|
||||
// Debug: Check payment status of loaded orders
|
||||
const orderCount = Object.keys(parsedOrders).length
|
||||
const paidOrders = Object.values(parsedOrders).filter((o: any) => o.paymentStatus === 'paid' || o.status === 'paid')
|
||||
|
||||
console.log('📦 Loaded orders from localStorage:', {
|
||||
storageKey,
|
||||
totalOrders: orderCount,
|
||||
paidOrders: paidOrders.length,
|
||||
orderStatuses: Object.values(parsedOrders).map((o: any) => ({
|
||||
id: o.id?.slice(-8),
|
||||
status: o.status,
|
||||
paymentStatus: o.paymentStatus,
|
||||
hasPaymentRequest: !!o.paymentRequest
|
||||
}))
|
||||
})
|
||||
} else {
|
||||
console.log('No orders found in localStorage for key:', storageKey)
|
||||
// Clear any existing orders when switching users
|
||||
orders.value = {}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to load orders from localStorage:', error)
|
||||
// Clear orders on error
|
||||
orders.value = {}
|
||||
}
|
||||
const parsedOrders = storageService.getUserData('market_orders', {})
|
||||
orders.value = parsedOrders
|
||||
|
||||
// Debug: Check payment status of loaded orders
|
||||
const orderCount = Object.keys(parsedOrders).length
|
||||
const paidOrders = Object.values(parsedOrders).filter((o: any) => o.paymentStatus === 'paid' || o.status === 'paid')
|
||||
|
||||
console.log('📦 Loaded orders from storage:', {
|
||||
totalOrders: orderCount,
|
||||
paidOrders: paidOrders.length,
|
||||
orderStatuses: Object.values(parsedOrders).map((o: any) => ({
|
||||
id: o.id?.slice(-8),
|
||||
status: o.status,
|
||||
paymentStatus: o.paymentStatus,
|
||||
hasPaymentRequest: !!o.paymentRequest
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
// Clear orders when user changes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue