implement usefull actions and getters
This commit is contained in:
@@ -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<string, { start: ISODate; end?: ISODate }>
|
||||
stepRecords: Record<string, StepRecordable>
|
||||
}
|
||||
|
||||
6
src/modules/record/interfaces/step-recordable.ts
Normal file
6
src/modules/record/interfaces/step-recordable.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { ISODate } from '@/shared/types/date'
|
||||
|
||||
export interface StepRecordable {
|
||||
start: ISODate
|
||||
end?: ISODate
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user