current step Id per record

This commit is contained in:
Julien Calixte
2023-05-14 13:32:12 +02:00
parent 14fbca6247
commit 0c1eb447d0
9 changed files with 28 additions and 22 deletions

View File

@@ -23,13 +23,13 @@ const getNextStepId = () => {
return null return null
} }
if (!recordStore.currentStepId) { if (!record.value.currentStepId) {
const [firstStep] = task.value.steps const [firstStep] = task.value.steps
return firstStep.id return firstStep.id
} }
const currentStepIndex = task.value.steps.findIndex( const currentStepIndex = task.value.steps.findIndex(
(step) => step.id === recordStore.currentStepId (step) => step.id === record.value.currentStepId
) )
const canHaveNextIndex = const canHaveNextIndex =
@@ -46,7 +46,9 @@ const hasStarted = computed(
() => Object.values(record.value?.stepRecords ?? {}).length > 0 () => Object.values(record.value?.stepRecords ?? {}).length > 0
) )
const canStart = computed(() => !recordStore.currentStepId && !hasStarted.value) const canStart = computed(
() => !record.value.currentStepId && !hasStarted.value
)
const startRecording = () => { const startRecording = () => {
if (!canStart.value || !task.value) { if (!canStart.value || !task.value) {
@@ -61,13 +63,13 @@ const startRecording = () => {
} }
const nextStep = () => { const nextStep = () => {
if (!task.value || !recordStore.currentStepId || !record.value) { if (!task.value || !record.value.currentStepId || !record.value) {
return return
} }
recordStore.nextStepRecord({ recordStore.nextStepRecord({
taskId: record.value.taskId, taskId: record.value.taskId,
currentStepId: recordStore.currentStepId, currentStepId: record.value.currentStepId,
nextStepId: getNextStepId(), nextStepId: getNextStepId(),
tick: toISODate(new Date()) tick: toISODate(new Date())
}) })
@@ -108,7 +110,7 @@ onUnmounted(() => {
<template> <template>
<div class="record-controls buttons has-addons"> <div class="record-controls buttons has-addons">
<template v-if="record && recordStore.currentStepId"> <template v-if="record && record.currentStepId">
<button <button
class="button is-primary is-light" class="button is-primary is-light"
v-if="record.breakTime" v-if="record.breakTime"

View File

@@ -19,7 +19,9 @@ const step = computed(() => taskStore.getStep(props.taskId, props.stepId))
const stepRecord = computed(() => const stepRecord = computed(() =>
recordStore.getStepRecord(props.taskId, props.stepId) recordStore.getStepRecord(props.taskId, props.stepId)
) )
const isCurrentStep = computed(() => recordStore.currentStepId === props.stepId) const isCurrentStep = computed(
() => record.value?.currentStepId === props.stepId
)
const isInBreakTime = computed(() => !!record.value?.breakTime) const isInBreakTime = computed(() => !!record.value?.breakTime)
const now = ref(toISODate(new Date())) const now = ref(toISODate(new Date()))

View File

@@ -27,7 +27,6 @@ const mountTaskRecordPreview = (withRecord = false) => {
'task-store': { tasks: [task] }, 'task-store': { tasks: [task] },
'task-record-store': withRecord 'task-record-store': withRecord
? { ? {
currentStepId: null,
records: { records: {
[task.id]: record [task.id]: record
} }

View File

@@ -14,5 +14,6 @@ export const fixtureRecordable = (
start: toISODate(faker.datatype.datetime()) start: toISODate(faker.datatype.datetime())
} }
}, },
currentStepId: null,
end: partialRecordable?.end end: partialRecordable?.end
}) })

View File

@@ -8,4 +8,5 @@ export interface Recordable {
stepRecords: Record<string, TimeRange> stepRecords: Record<string, TimeRange>
notes: string notes: string
breakTime?: TimeRange breakTime?: TimeRange
currentStepId: string | null
} }

View File

@@ -2,12 +2,13 @@ import { toISODate } from '@/shared/types/date'
import { faker } from '@faker-js/faker' import { faker } from '@faker-js/faker'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import type { Recordable } from '../interfaces/recordable' import type { Recordable } from '../interfaces/recordable'
import { fixtureRecordable } from '../interfaces/recordable.fixture'
import { fixtureTimeRange } from '../interfaces/time-range.fixture' import { fixtureTimeRange } from '../interfaces/time-range.fixture'
import { TaskRecord } from './task-record' import { TaskRecord } from './task-record'
describe('Task Record', () => { describe('Task Record', () => {
it('creates a Record from a Recordable', () => { it('creates a Record from a Recordable', () => {
const recordable: Recordable = { const recordable: Recordable = fixtureRecordable({
taskId: faker.datatype.uuid(), taskId: faker.datatype.uuid(),
notes: faker.lorem.paragraphs(), notes: faker.lorem.paragraphs(),
start: toISODate(faker.date.past(1)), start: toISODate(faker.date.past(1)),
@@ -16,7 +17,7 @@ describe('Task Record', () => {
stepRecords: { stepRecords: {
[faker.datatype.uuid()]: fixtureTimeRange() [faker.datatype.uuid()]: fixtureTimeRange()
} }
} })
expect(TaskRecord.fromRecordable(recordable)).toEqual(recordable) expect(TaskRecord.fromRecordable(recordable)).toEqual(recordable)
}) })

View File

@@ -8,6 +8,7 @@ export class TaskRecord implements Recordable {
public stepRecords: Record<string, TimeRange> = {} public stepRecords: Record<string, TimeRange> = {}
public notes = '' public notes = ''
public breakTime?: TimeRange public breakTime?: TimeRange
public currentStepId: string | null = null
public constructor(public readonly taskId: string) {} public constructor(public readonly taskId: string) {}
@@ -19,6 +20,7 @@ export class TaskRecord implements Recordable {
const taskRecord = new TaskRecord(recordable.taskId) const taskRecord = new TaskRecord(recordable.taskId)
taskRecord.stepRecords = recordable.stepRecords taskRecord.stepRecords = recordable.stepRecords
taskRecord.currentStepId = recordable.currentStepId
taskRecord.start = recordable.start taskRecord.start = recordable.start
taskRecord.end = recordable.end taskRecord.end = recordable.end
taskRecord.breakTime = recordable.breakTime taskRecord.breakTime = recordable.breakTime

View File

@@ -6,14 +6,12 @@ import { TaskRecord } from '../models/task-record'
import { addBreakTimeToStepRecords } from '../services/breaktime-service' import { addBreakTimeToStepRecords } from '../services/breaktime-service'
export interface TaskRecordStoreState { export interface TaskRecordStoreState {
currentStepId: string | null
records: { [recordId: string]: Recordable } records: { [recordId: string]: Recordable }
} }
export const useTaskRecordStore = defineStore('task-record-store', { export const useTaskRecordStore = defineStore('task-record-store', {
persist: true, persist: true,
state: (): TaskRecordStoreState => ({ state: (): TaskRecordStoreState => ({
currentStepId: null,
records: {} records: {}
}), }),
actions: { actions: {
@@ -57,10 +55,10 @@ export const useTaskRecordStore = defineStore('task-record-store', {
[params.stepId]: { [params.stepId]: {
start: params.start start: params.start
} }
}
}
}, },
currentStepId: params.stepId currentStepId: params.stepId
}
}
}) })
}, },
endStepRecord(params: { taskId: string; stepId: string; end: ISODate }) { endStepRecord(params: { taskId: string; stepId: string; end: ISODate }) {
@@ -101,7 +99,7 @@ export const useTaskRecordStore = defineStore('task-record-store', {
} }
this.records[taskId].end = toISODate(new Date()) this.records[taskId].end = toISODate(new Date())
this.currentStepId = null this.records[taskId].currentStepId = null
}, },
updateRecordNotes(taskId: string, notes: string) { updateRecordNotes(taskId: string, notes: string) {
const record = this.records[taskId] const record = this.records[taskId]
@@ -119,12 +117,12 @@ export const useTaskRecordStore = defineStore('task-record-store', {
} }
}, },
reset(taskId: string) { reset(taskId: string) {
this.currentStepId = null
if (!this.records[taskId]) { if (!this.records[taskId]) {
return return
} }
this.records[taskId].stepRecords = {} this.records[taskId].stepRecords = {}
this.records[taskId].end = undefined this.records[taskId].end = undefined
this.records[taskId].currentStepId = null
}, },
pause(taskId: string) { pause(taskId: string) {
if (this.records[taskId]?.breakTime) { if (this.records[taskId]?.breakTime) {

View File

@@ -1,3 +1,4 @@
import { fixtureRecordable } from '@/modules/record/interfaces/recordable.fixture'
import type { TaskRecordStoreState } from '@/modules/record/stores/useTaskRecordStore' import type { TaskRecordStoreState } from '@/modules/record/stores/useTaskRecordStore'
import { fixtureTask } from '@/modules/task/models/task.fixture' import { fixtureTask } from '@/modules/task/models/task.fixture'
import type { TaskStoreState } from '@/modules/task/stores/useTask.store' import type { TaskStoreState } from '@/modules/task/stores/useTask.store'
@@ -18,20 +19,19 @@ const [firstTask, secondTask] = tasks
const initialState: InitialState = { const initialState: InitialState = {
'task-store': { tasks }, 'task-store': { tasks },
'task-record-store': { 'task-record-store': {
currentStepId: null,
records: { records: {
[firstTask.id]: { [firstTask.id]: fixtureRecordable({
taskId: firstTask.id, taskId: firstTask.id,
stepRecords: {}, stepRecords: {},
start: toISODate(new Date()), start: toISODate(new Date()),
notes: '' notes: ''
}, }),
[secondTask.id]: { [secondTask.id]: fixtureRecordable({
taskId: secondTask.id, taskId: secondTask.id,
stepRecords: {}, stepRecords: {},
start: toISODate(new Date()), start: toISODate(new Date()),
notes: '' notes: ''
} })
} }
} }
} }