From 46ef24dfb28fca1ae14d6d1ad4499540cdb74956 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Mon, 10 Apr 2023 12:46:32 +0200 Subject: [PATCH] implement usefull actions and getters --- src/modules/record/interfaces/recordable.ts | 3 +- .../record/interfaces/step-recordable.ts | 6 ++ .../record/stores/useTaskRecordStore.ts | 64 ++++++++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/modules/record/interfaces/step-recordable.ts diff --git a/src/modules/record/interfaces/recordable.ts b/src/modules/record/interfaces/recordable.ts index 696b410..0822fd4 100644 --- a/src/modules/record/interfaces/recordable.ts +++ b/src/modules/record/interfaces/recordable.ts @@ -1,9 +1,10 @@ import type { ISODate } from '@/shared/types/date' +import type { StepRecordable } from './step-recordable' export interface Recordable { id: string taskId: string start: ISODate end?: ISODate - stepRecords: Record + stepRecords: Record } diff --git a/src/modules/record/interfaces/step-recordable.ts b/src/modules/record/interfaces/step-recordable.ts new file mode 100644 index 0000000..0ec4e3c --- /dev/null +++ b/src/modules/record/interfaces/step-recordable.ts @@ -0,0 +1,6 @@ +import type { ISODate } from '@/shared/types/date' + +export interface StepRecordable { + start: ISODate + end?: ISODate +} diff --git a/src/modules/record/stores/useTaskRecordStore.ts b/src/modules/record/stores/useTaskRecordStore.ts index d9e1339..ef525ed 100644 --- a/src/modules/record/stores/useTaskRecordStore.ts +++ b/src/modules/record/stores/useTaskRecordStore.ts @@ -1,13 +1,71 @@ +import type { ISODate } from '@/shared/types/date' import { defineStore } from 'pinia' import type { Recordable } from '../interfaces/recordable' +import type { StepRecordable } from '../interfaces/step-recordable' +import type { TaskRecord } from '../models/task-record' + +type RecordId = string export interface TaskRecordStoreState { - records: { [taskId: string]: Recordable[] } + records: { [recordId: string]: Recordable } + taskRecordMaps: { [taskId: string]: RecordId[] } } export const useTaskRecordStore = defineStore('task-record-store', { persist: true, state: (): TaskRecordStoreState => ({ - records: {} - }) + records: {}, + taskRecordMaps: {} + }), + actions: { + addRecord(taskRecord: TaskRecord) { + if (!this.taskRecordMaps[taskRecord.taskId]) { + this.taskRecordMaps[taskRecord.taskId] = [] + } + + this.taskRecordMaps[taskRecord.taskId].push(taskRecord.id) + + this.records[taskRecord.id] = taskRecord + }, + removeRecord(recordId: string) { + for (const taskId in this.taskRecordMaps) { + this.taskRecordMaps[taskId] = this.taskRecordMaps[taskId].filter( + (rId) => rId !== recordId + ) + } + + delete this.records[recordId] + }, + startStepRecord(params: { + recordId: string + stepId: string + start: ISODate + }) { + this.records[params.recordId].stepRecords[params.stepId] = { + start: params.start + } + }, + endStepRecord(params: { recordId: string; stepId: string; end: ISODate }) { + const stepRecord = + this.records[params.recordId].stepRecords[params.stepId] + + if (!stepRecord) { + return + } + + stepRecord.end = params.end + } + }, + getters: { + getTaskRecords() { + return (taskId: string): Recordable[] => + this.taskRecordMaps?.[taskId]?.map( + (recordId) => this.records[recordId] + ) ?? [] + }, + getStepRecord() { + return (recordId: string, stepId: string): StepRecordable | null => + this.records?.[recordId]?.stepRecords[stepId] ?? null + } + } })