|
@@ -1,20 +1,19 @@
|
|
|
import { parse } from 'date-fns'
|
|
|
import {
|
|
|
- IBlock,
|
|
|
- IExercise,
|
|
|
- IRating,
|
|
|
TTraining,
|
|
|
TExerciseInstance,
|
|
|
TBlockInstance,
|
|
|
TBlock,
|
|
|
- TExercise
|
|
|
+ TExercise,
|
|
|
+ TRating
|
|
|
} from './types'
|
|
|
import {
|
|
|
TrainingQuery,
|
|
|
SubBlockFragment,
|
|
|
- BlockContentFragment,
|
|
|
- Training,
|
|
|
- ExerciseContentFragment
|
|
|
+ Exercise,
|
|
|
+ Block,
|
|
|
+ ExerciseInstance,
|
|
|
+ BlockInstance
|
|
|
} from '../gql'
|
|
|
import { isArray, transform, isEqual, isObject } from 'lodash'
|
|
|
|
|
@@ -22,20 +21,21 @@ import { isArray, transform, isEqual, isObject } from 'lodash'
|
|
|
* Takes a block of exercises and calculates the duration in seconds.
|
|
|
* @param block
|
|
|
*/
|
|
|
-export function calculateDuration(block: IBlock): number {
|
|
|
- if (block.duration) return block.duration
|
|
|
- const repetitions = block.repetitions || 1
|
|
|
- const rest = block.rest || 0
|
|
|
- if (block.blocks) {
|
|
|
- const subblockDuration = block.blocks.reduce(
|
|
|
- (accumulator, block) =>
|
|
|
- accumulator + (block.duration || calculateDuration(block)),
|
|
|
+export function calculateDuration(
|
|
|
+ blocks?: TBlockInstance | TBlockInstance[]
|
|
|
+): number {
|
|
|
+ if (!blocks) return 0
|
|
|
+ const blocksArray = isArray(blocks) ? blocks : [blocks]
|
|
|
+ const duration = blocksArray.map(blockInstance => {
|
|
|
+ const blockRounds = blockInstance.rounds ?? 1
|
|
|
+ const blockDuration =
|
|
|
+ blockInstance.block?.duration ??
|
|
|
+ calculateDuration(blockInstance.block?.blocks) ??
|
|
|
0
|
|
|
- )
|
|
|
- return repetitions * (subblockDuration + rest)
|
|
|
- } else {
|
|
|
- return 0
|
|
|
- }
|
|
|
+ const blockRest = blockInstance.block?.rest ?? 0
|
|
|
+ return blockRounds * (blockDuration + blockRest)
|
|
|
+ })
|
|
|
+ return duration.reduce((a, b) => a + b, 0)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -53,12 +53,12 @@ export function formatTime(seconds: number) {
|
|
|
* 4x Exercise 1 - Exercise 2 - 2x Exercise 3
|
|
|
* @param exercises
|
|
|
*/
|
|
|
-export function printExercises(exercises: IExercise[]) {
|
|
|
+export function printExercises(exercises: TExerciseInstance[]) {
|
|
|
return exercises
|
|
|
- .map(exercise =>
|
|
|
- exercise.repetitions > 1
|
|
|
- ? `${exercise.repetitions}x ${exercise.name}`
|
|
|
- : exercise.name
|
|
|
+ .map(exerciseInstance =>
|
|
|
+ exerciseInstance.repetitions && exerciseInstance.repetitions > 1
|
|
|
+ ? `${exerciseInstance.repetitions}x ${exerciseInstance.exercise?.name}`
|
|
|
+ : exerciseInstance.exercise?.name
|
|
|
)
|
|
|
.join(' - ')
|
|
|
}
|
|
@@ -67,7 +67,7 @@ export function printExercises(exercises: IExercise[]) {
|
|
|
* Takes an array of rating and calculates the average rating
|
|
|
* @param ratings
|
|
|
*/
|
|
|
-export function calculateRating(ratings: IRating[]) {
|
|
|
+export function calculateRating(ratings: TRating[]) {
|
|
|
const numberOfRatings = ratings.length
|
|
|
const sumOfRatings = ratings.reduce(
|
|
|
(accumulator, rating) => accumulator + rating.value,
|
|
@@ -83,8 +83,6 @@ export function trainingDBToArray(DBTraining: TrainingQuery['training']) {
|
|
|
return { ...data, blocks: blockDBToArray(blocks) }
|
|
|
}
|
|
|
|
|
|
-export function trainingArrayToDB() {}
|
|
|
-
|
|
|
export function blockDBToArray(DBSubBlock?: SubBlockFragment[]) {
|
|
|
console.log({ DBSubBlock })
|
|
|
if (!DBSubBlock) return undefined
|
|
@@ -123,8 +121,8 @@ function randomID() {
|
|
|
.substr(0, 10)}`
|
|
|
}
|
|
|
|
|
|
-export function emptyExercise(input?: TExercise) {
|
|
|
- const emptyExercise = {
|
|
|
+export function emptyExercise(input?: Partial<Exercise>) {
|
|
|
+ const emptyExercise: TExercise = {
|
|
|
id: randomID(),
|
|
|
name: '',
|
|
|
description: '',
|
|
@@ -136,8 +134,8 @@ export function emptyExercise(input?: TExercise) {
|
|
|
return { ...emptyExercise, ...input }
|
|
|
}
|
|
|
|
|
|
-export function emptyExerciseInstance(input?: TExerciseInstance) {
|
|
|
- const emptyExerciseInstance = {
|
|
|
+export function emptyExerciseInstance(input?: Partial<ExerciseInstance>) {
|
|
|
+ const emptyExerciseInstance: TExerciseInstance = {
|
|
|
id: randomID(),
|
|
|
order: 0,
|
|
|
exercise: emptyExercise()
|
|
@@ -145,8 +143,8 @@ export function emptyExerciseInstance(input?: TExerciseInstance) {
|
|
|
return { ...emptyExerciseInstance, ...input }
|
|
|
}
|
|
|
|
|
|
-export function emptyBlock(input?: TBlock) {
|
|
|
- const emptyBlock = {
|
|
|
+export function emptyBlock(input?: Partial<Block>) {
|
|
|
+ const emptyBlock: TBlock = {
|
|
|
id: randomID(),
|
|
|
title: '',
|
|
|
format: { id: '', name: '', description: '' },
|
|
@@ -158,8 +156,8 @@ export function emptyBlock(input?: TBlock) {
|
|
|
return { ...emptyBlock, ...input }
|
|
|
}
|
|
|
|
|
|
-export function emptyBlockInstance(input?: TBlockInstance) {
|
|
|
- const emptyBlockInstance = {
|
|
|
+export function emptyBlockInstance(input?: Partial<BlockInstance>) {
|
|
|
+ const emptyBlockInstance: TBlockInstance = {
|
|
|
id: randomID(),
|
|
|
block: emptyBlock(),
|
|
|
order: 0
|
|
@@ -168,7 +166,7 @@ export function emptyBlockInstance(input?: TBlockInstance) {
|
|
|
}
|
|
|
|
|
|
export function emptyTraining(input?: TTraining) {
|
|
|
- const emptyTraining = {
|
|
|
+ const emptyTraining: TTraining = {
|
|
|
id: randomID(),
|
|
|
title: '',
|
|
|
type: { id: '', name: '', description: '' },
|
|
@@ -180,11 +178,12 @@ export function emptyTraining(input?: TTraining) {
|
|
|
return { ...emptyTraining, ...input }
|
|
|
}
|
|
|
|
|
|
-export function collectMutationCreateConnect(arr: any[]) {
|
|
|
+export function collectMutations(arr: any[]) {
|
|
|
const create: any[] = []
|
|
|
const connect: any[] = []
|
|
|
const del: any[] = []
|
|
|
const update: any[] = []
|
|
|
+ console.log('collect', arr)
|
|
|
arr.forEach(val => {
|
|
|
if (typeof val === 'object' && val['connect']) connect.push(val['connect'])
|
|
|
if (typeof val === 'object' && val['create']) create.push(val['create'])
|
|
@@ -204,17 +203,19 @@ export function collectMutationCreateConnect(arr: any[]) {
|
|
|
return returnObject
|
|
|
}
|
|
|
|
|
|
+function isNotInDB(key: any, val: any) {
|
|
|
+ return (
|
|
|
+ key === '__typename' ||
|
|
|
+ (key === 'id' && typeof val === 'string' && val.startsWith('++'))
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
|
- if (key === '__typename') {
|
|
|
- // remove the typename from the database
|
|
|
- return
|
|
|
- } else if (key === 'id' && typeof val === 'string' && val.startsWith('++')) {
|
|
|
- // remove placeholder IDs
|
|
|
- return
|
|
|
- } else if (isArray(val)) {
|
|
|
- // collect 'create' and 'connect' statements
|
|
|
- acc[key] = collectMutationCreateConnect(transform(val, transformArrayToDB))
|
|
|
+ if (isNotInDB(key, val)) return
|
|
|
+ if (isArray(val)) {
|
|
|
+ acc[key] = collectMutations(transform(val, transformArrayToDB))
|
|
|
} else if (typeof val === 'object' && !!val) {
|
|
|
+ console.log('object', key, val)
|
|
|
// we found an object!
|
|
|
if (!!val['id'] && val['id'].startsWith('++')) {
|
|
|
// values with placeholder IDs are preserved
|
|
@@ -223,7 +224,6 @@ export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
|
// IDs starting with -- are for deletion
|
|
|
acc[key] = { delete: { id: val['id'].substr(2) } }
|
|
|
} else {
|
|
|
- // values with real IDs are just connected
|
|
|
const { id, ...data } = val
|
|
|
if (id?.startsWith('@@')) {
|
|
|
if (typeof key === 'string') {
|
|
@@ -236,7 +236,6 @@ export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- console.log('update candidate', key, id, data, acc[key].update.data)
|
|
|
} else {
|
|
|
acc[key] = { connect: { id } }
|
|
|
}
|
|
@@ -247,19 +246,31 @@ export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function diffDB(newObject: any = {}, oldObject: any = {}) {
|
|
|
+export function KdiffDB(newObject: any = {}, oldObject: any = {}) {
|
|
|
const transformResult = transform(
|
|
|
newObject,
|
|
|
(result: any, value: any, key: string) => {
|
|
|
- if (key === 'id') {
|
|
|
- if (isEqual(value, oldObject[key])) result[key] = `@@${value}`
|
|
|
- } else if (!isEqual(value, oldObject[key])) {
|
|
|
+ /*if (key === 'id') {
|
|
|
+ result[key] = value
|
|
|
+ } else*/ if (
|
|
|
+ !isEqual(value, oldObject[key])
|
|
|
+ ) {
|
|
|
const newValue =
|
|
|
isObject(value) && isObject(oldObject[key])
|
|
|
- ? diffDB(value, oldObject[key])
|
|
|
+ ? KdiffDB(value, oldObject[key])
|
|
|
: value
|
|
|
if (newValue !== undefined && newValue !== null) {
|
|
|
- result[key] = newValue
|
|
|
+ if (isEqual(key, 'id')) {
|
|
|
+ if (result[key] !== undefined) return
|
|
|
+ } else {
|
|
|
+ result[key] = newValue
|
|
|
+ if (
|
|
|
+ oldObject['id'] !== undefined &&
|
|
|
+ oldObject['id'] === newObject['id']
|
|
|
+ ) {
|
|
|
+ result['id'] = `@@${oldObject['id']}`
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|