refactor: use unified manager design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+57
-43
@@ -5,10 +5,10 @@ import { CollectionObject } from '@ChronoManager/models/collection'
|
||||
import { EntityObject } from '@ChronoManager/models/entity'
|
||||
import { EventObject } from '@ChronoManager/models/event'
|
||||
import { TaskObject } from '@ChronoManager/models/task'
|
||||
import { ServiceObject } from '@ChronoManager/models/service'
|
||||
import type { ServiceObject } from '@ChronoManager/models/service'
|
||||
import { useCollectionsStore } from '@ChronoManager/stores/collectionsStore'
|
||||
import { useEntitiesStore } from '@ChronoManager/stores/entitiesStore'
|
||||
import type { SourceSelector } from '@ChronoManager/types'
|
||||
import { useServicesStore } from '@ChronoManager/stores/servicesStore'
|
||||
import type { CalendarView as CalendarViewType } from '@/types'
|
||||
import {
|
||||
AGENDA_VIEW_SPANS,
|
||||
@@ -16,15 +16,14 @@ import {
|
||||
type AgendaViewSpan,
|
||||
type DaysViewSpan,
|
||||
} from '@/types/spans'
|
||||
import { useChronoEntityActions } from '@/composables/useChronoEntityActions'
|
||||
|
||||
type EntitySources = Record<string, Record<string, Record<string | number, true>>>
|
||||
type ChronoEntityProperties = EventObject | TaskObject
|
||||
|
||||
export const useChronoStore = defineStore('chronoStore', () => {
|
||||
const userStore = useUserStore()
|
||||
const servicesStore = useServicesStore()
|
||||
const collectionsStore = useCollectionsStore()
|
||||
const entitiesStore = useEntitiesStore()
|
||||
const entityActions = useChronoEntityActions(entitiesStore, collectionsStore)
|
||||
|
||||
const savedCalendarView = userStore.getSetting('chrono.calendarView')
|
||||
const savedDaysViewSpan = userStore.getSetting('chrono.daysViewSpan')
|
||||
@@ -95,22 +94,6 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
|
||||
const filteredTasks = computed(() => tasks.value)
|
||||
|
||||
function buildSources(collectionItems: CollectionObject[]): EntitySources {
|
||||
return collectionItems.reduce<EntitySources>((acc, collection) => {
|
||||
if (!acc[collection.provider]) {
|
||||
acc[collection.provider] = {}
|
||||
}
|
||||
|
||||
const serviceKey = String(collection.service)
|
||||
if (!acc[collection.provider][serviceKey]) {
|
||||
acc[collection.provider][serviceKey] = {}
|
||||
}
|
||||
|
||||
acc[collection.provider][serviceKey][collection.identifier] = true
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
|
||||
function setViewMode(mode: 'calendar' | 'tasks') {
|
||||
viewMode.value = mode
|
||||
}
|
||||
@@ -258,17 +241,32 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
showTaskEditor.value = false
|
||||
}
|
||||
|
||||
function prepareEntityProperties(entity: EntityObject): ChronoEntityProperties {
|
||||
const properties = entity.properties as ChronoEntityProperties
|
||||
const now = new Date().toISOString()
|
||||
|
||||
properties.modified = now as never
|
||||
if (!entity.identifier) {
|
||||
properties.created = now as never
|
||||
}
|
||||
|
||||
return properties
|
||||
}
|
||||
|
||||
async function persistEntity(entity: EntityObject, collection: CollectionObject): Promise<EntityObject> {
|
||||
const properties = prepareEntityProperties(entity).toJson()
|
||||
|
||||
return entity.identifier
|
||||
? entitiesStore.update(entity.identifier, properties)
|
||||
: entitiesStore.create(collection.identifier, properties)
|
||||
}
|
||||
|
||||
async function toggleCalendarVisibility(collection: CollectionObject) {
|
||||
const nextVisibility = collection.properties.visibility === false
|
||||
const updated = collection.clone()
|
||||
updated.properties.visibility = nextVisibility
|
||||
|
||||
await collectionsStore.update(
|
||||
updated.provider,
|
||||
updated.service,
|
||||
updated.identifier,
|
||||
updated.properties,
|
||||
)
|
||||
await collectionsStore.update(updated.identifier, updated.properties)
|
||||
}
|
||||
|
||||
async function saveEvent(entity: EntityObject, collection?: CollectionObject | null) {
|
||||
@@ -280,7 +278,7 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
throw new Error('No calendar collection selected')
|
||||
}
|
||||
|
||||
selectedEntity.value = await entityActions.saveEntity(entity, targetCollection)
|
||||
selectedEntity.value = await persistEntity(entity, targetCollection)
|
||||
selectedCollection.value = targetCollection
|
||||
entityEditorMode.value = 'view'
|
||||
}
|
||||
@@ -294,18 +292,26 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
throw new Error('No task list selected')
|
||||
}
|
||||
|
||||
selectedEntity.value = await entityActions.saveEntity(entity, targetCollection)
|
||||
selectedEntity.value = await persistEntity(entity, targetCollection)
|
||||
selectedCollection.value = targetCollection
|
||||
entityEditorMode.value = 'view'
|
||||
}
|
||||
|
||||
async function deleteEvent(entity: EntityObject, collection?: CollectionObject | null) {
|
||||
await entityActions.deleteEntity(entity, collection)
|
||||
if (!(collection instanceof CollectionObject) && !selectedCollection.value) {
|
||||
throw new Error('No calendar collection selected')
|
||||
}
|
||||
|
||||
await entitiesStore.delete([entity.identifier])
|
||||
closeEntityEditor()
|
||||
}
|
||||
|
||||
async function deleteTask(entity: EntityObject, collection?: CollectionObject | null) {
|
||||
await entityActions.deleteEntity(entity, collection)
|
||||
if (!(collection instanceof CollectionObject) && !selectedCollection.value) {
|
||||
throw new Error('No task list selected')
|
||||
}
|
||||
|
||||
await entitiesStore.delete([entity.identifier])
|
||||
closeEntityEditor()
|
||||
}
|
||||
|
||||
@@ -315,7 +321,15 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
return
|
||||
}
|
||||
|
||||
await entityActions.toggleTaskCompletion(taskEntity)
|
||||
const taskData = taskEntity.properties as TaskObject
|
||||
const isCompleted = taskData.status === 'completed'
|
||||
|
||||
taskData.status = isCompleted ? 'needs-action' : 'completed'
|
||||
taskData.completedOn = isCompleted ? null : new Date().toISOString() as never
|
||||
taskData.progress = isCompleted ? null : 100
|
||||
taskData.modified = new Date().toISOString() as never
|
||||
|
||||
await entitiesStore.update(taskEntity.identifier, taskData.toJson())
|
||||
}
|
||||
|
||||
async function saveCollection(collection: CollectionObject, service: ServiceObject) {
|
||||
@@ -323,35 +337,35 @@ export const useChronoStore = defineStore('chronoStore', () => {
|
||||
await collectionsStore.create(
|
||||
service.provider,
|
||||
service.identifier || '',
|
||||
null,
|
||||
collection.properties,
|
||||
)
|
||||
} else {
|
||||
await collectionsStore.update(
|
||||
collection.provider,
|
||||
collection.service,
|
||||
collection.identifier,
|
||||
collection.properties,
|
||||
)
|
||||
await collectionsStore.update(collection.identifier, collection.properties)
|
||||
}
|
||||
|
||||
showCollectionEditor.value = false
|
||||
}
|
||||
|
||||
async function deleteCollection(collection: CollectionObject) {
|
||||
await collectionsStore.delete(collection.provider, collection.service, collection.identifier)
|
||||
await collectionsStore.delete(collection.identifier)
|
||||
|
||||
if (selectedCollection.value?.identifier === collection.identifier) {
|
||||
selectedCollection.value = null
|
||||
selectedEntity.value = null
|
||||
}
|
||||
|
||||
showCollectionEditor.value = false
|
||||
}
|
||||
|
||||
async function initialize() {
|
||||
loading.value = true
|
||||
try {
|
||||
await servicesStore.list()
|
||||
await collectionsStore.list()
|
||||
|
||||
const sources = buildSources(collections.value)
|
||||
if (Object.keys(sources).length > 0) {
|
||||
await entitiesStore.list(sources as SourceSelector)
|
||||
const sources = collections.value.map(collection => collection.identifier)
|
||||
if (sources.length > 0) {
|
||||
await entitiesStore.list(sources)
|
||||
}
|
||||
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user