|
@@ -1,12 +1,22 @@
|
|
import { parse } from 'date-fns'
|
|
import { parse } from 'date-fns'
|
|
-import { IBlock, IExercise, IRating, TTraining } from './types'
|
|
|
|
|
|
+import {
|
|
|
|
+ IBlock,
|
|
|
|
+ IExercise,
|
|
|
|
+ IRating,
|
|
|
|
+ TTraining,
|
|
|
|
+ TExerciseInstance,
|
|
|
|
+ TBlockInstance,
|
|
|
|
+ TBlock,
|
|
|
|
+ TExercise
|
|
|
|
+} from './types'
|
|
import {
|
|
import {
|
|
TrainingQuery,
|
|
TrainingQuery,
|
|
SubBlockFragment,
|
|
SubBlockFragment,
|
|
BlockContentFragment,
|
|
BlockContentFragment,
|
|
- Training
|
|
|
|
|
|
+ Training,
|
|
|
|
+ ExerciseContentFragment
|
|
} from '../gql'
|
|
} from '../gql'
|
|
-import { isArray, transform } from 'lodash'
|
|
|
|
|
|
+import { isArray, transform, isEqual, isObject } from 'lodash'
|
|
|
|
|
|
/**
|
|
/**
|
|
* Takes a block of exercises and calculates the duration in seconds.
|
|
* Takes a block of exercises and calculates the duration in seconds.
|
|
@@ -113,8 +123,30 @@ function randomID() {
|
|
.substr(0, 10)}`
|
|
.substr(0, 10)}`
|
|
}
|
|
}
|
|
|
|
|
|
-export function emptyBlock(input?: Partial<BlockContentFragment>) {
|
|
|
|
- const emptyBlock: BlockContentFragment = {
|
|
|
|
|
|
+export function emptyExercise(input?: TExercise) {
|
|
|
|
+ const emptyExercise = {
|
|
|
|
+ id: randomID(),
|
|
|
|
+ name: '',
|
|
|
|
+ description: '',
|
|
|
|
+ videos: [],
|
|
|
|
+ pictures: [],
|
|
|
|
+ targets: [],
|
|
|
|
+ baseExercise: []
|
|
|
|
+ }
|
|
|
|
+ return { ...emptyExercise, ...input }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function emptyExerciseInstance(input?: TExerciseInstance) {
|
|
|
|
+ const emptyExerciseInstance = {
|
|
|
|
+ id: randomID(),
|
|
|
|
+ order: 0,
|
|
|
|
+ exercise: emptyExercise()
|
|
|
|
+ }
|
|
|
|
+ return { ...emptyExerciseInstance, ...input }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export function emptyBlock(input?: TBlock) {
|
|
|
|
+ const emptyBlock = {
|
|
id: randomID(),
|
|
id: randomID(),
|
|
title: '',
|
|
title: '',
|
|
format: { id: '', name: '', description: '' },
|
|
format: { id: '', name: '', description: '' },
|
|
@@ -126,8 +158,8 @@ export function emptyBlock(input?: Partial<BlockContentFragment>) {
|
|
return { ...emptyBlock, ...input }
|
|
return { ...emptyBlock, ...input }
|
|
}
|
|
}
|
|
|
|
|
|
-export function emptyBlockInstance(input?: Partial<SubBlockFragment>) {
|
|
|
|
- const emptyBlockInstance: SubBlockFragment = {
|
|
|
|
|
|
+export function emptyBlockInstance(input?: TBlockInstance) {
|
|
|
|
+ const emptyBlockInstance = {
|
|
id: randomID(),
|
|
id: randomID(),
|
|
block: emptyBlock(),
|
|
block: emptyBlock(),
|
|
order: 0
|
|
order: 0
|
|
@@ -136,7 +168,7 @@ export function emptyBlockInstance(input?: Partial<SubBlockFragment>) {
|
|
}
|
|
}
|
|
|
|
|
|
export function emptyTraining(input?: TTraining) {
|
|
export function emptyTraining(input?: TTraining) {
|
|
- const emptyTraining: TTraining = {
|
|
|
|
|
|
+ const emptyTraining = {
|
|
id: randomID(),
|
|
id: randomID(),
|
|
title: '',
|
|
title: '',
|
|
type: { id: '', name: '', description: '' },
|
|
type: { id: '', name: '', description: '' },
|
|
@@ -152,22 +184,24 @@ export function collectMutationCreateConnect(arr: any[]) {
|
|
const create: any[] = []
|
|
const create: any[] = []
|
|
const connect: any[] = []
|
|
const connect: any[] = []
|
|
const del: any[] = []
|
|
const del: any[] = []
|
|
|
|
+ const update: any[] = []
|
|
arr.forEach(val => {
|
|
arr.forEach(val => {
|
|
- if (typeof val === 'object' && val['connect']) {
|
|
|
|
- connect.push(val['connect'])
|
|
|
|
- }
|
|
|
|
- if (typeof val === 'object' && val['create']) {
|
|
|
|
- create.push(val['create'])
|
|
|
|
- }
|
|
|
|
- if (typeof val === 'object' && val['delete']) {
|
|
|
|
- del.push(val['delete'])
|
|
|
|
- }
|
|
|
|
|
|
+ if (typeof val === 'object' && val['connect']) connect.push(val['connect'])
|
|
|
|
+ if (typeof val === 'object' && val['create']) create.push(val['create'])
|
|
|
|
+ if (typeof val === 'object' && val['delete']) del.push(val['delete'])
|
|
|
|
+ if (typeof val === 'object' && val['update']) update.push(val['update'])
|
|
})
|
|
})
|
|
- if (create.length > 0 || connect.length > 0) {
|
|
|
|
- return { create, connect, delete: del }
|
|
|
|
- } else {
|
|
|
|
- return arr
|
|
|
|
- }
|
|
|
|
|
|
+ const returnObject: {
|
|
|
|
+ connect?: any
|
|
|
|
+ delete?: any
|
|
|
|
+ create?: any
|
|
|
|
+ update?: any
|
|
|
|
+ } = {}
|
|
|
|
+ if (connect.length > 0) returnObject.connect = connect
|
|
|
|
+ if (del.length > 0) returnObject.delete = del
|
|
|
|
+ if (update.length > 0) returnObject.update = update
|
|
|
|
+ if (create.length > 0) returnObject.create = create
|
|
|
|
+ return returnObject
|
|
}
|
|
}
|
|
|
|
|
|
export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
@@ -182,26 +216,53 @@ export function transformArrayToDB(acc: any, val: any, key: any, object: any) {
|
|
acc[key] = collectMutationCreateConnect(transform(val, transformArrayToDB))
|
|
acc[key] = collectMutationCreateConnect(transform(val, transformArrayToDB))
|
|
} else if (typeof val === 'object' && !!val) {
|
|
} else if (typeof val === 'object' && !!val) {
|
|
// we found an object!
|
|
// we found an object!
|
|
- if (
|
|
|
|
- !!val['id'] &&
|
|
|
|
- typeof val['id'] === 'string' &&
|
|
|
|
- val['id'].startsWith('++')
|
|
|
|
- ) {
|
|
|
|
|
|
+ if (!!val['id'] && val['id'].startsWith('++')) {
|
|
// values with placeholder IDs are preserved
|
|
// values with placeholder IDs are preserved
|
|
acc[key] = { create: transform(val, transformArrayToDB) }
|
|
acc[key] = { create: transform(val, transformArrayToDB) }
|
|
- } else if (
|
|
|
|
- !!val['id'] &&
|
|
|
|
- typeof val['id'] === 'string' &&
|
|
|
|
- val['id'].startsWith('--')
|
|
|
|
- ) {
|
|
|
|
- // values with placeholder IDs are preserved
|
|
|
|
|
|
+ } else if (!!val['id'] && val['id'].startsWith('--')) {
|
|
|
|
+ // IDs starting with -- are for deletion
|
|
acc[key] = { delete: { id: val['id'].substr(2) } }
|
|
acc[key] = { delete: { id: val['id'].substr(2) } }
|
|
} else {
|
|
} else {
|
|
// values with real IDs are just connected
|
|
// values with real IDs are just connected
|
|
- acc[key] = { connect: { id: val['id'] } }
|
|
|
|
|
|
+ const { id, ...data } = val
|
|
|
|
+ if (id?.startsWith('@@')) {
|
|
|
|
+ if (typeof key === 'string') {
|
|
|
|
+ acc[key] = { update: transform(data, transformArrayToDB) }
|
|
|
|
+ } else {
|
|
|
|
+ acc[key] = {
|
|
|
|
+ update: {
|
|
|
|
+ where: { id: id.substr(2) },
|
|
|
|
+ data: transform(data, transformArrayToDB)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ console.log('update candidate', key, id, data, acc[key].update.data)
|
|
|
|
+ } else {
|
|
|
|
+ acc[key] = { connect: { id } }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
// copy the value
|
|
// copy the value
|
|
acc[key] = val
|
|
acc[key] = val
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+export function diffDB(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])) {
|
|
|
|
+ const newValue =
|
|
|
|
+ isObject(value) && isObject(oldObject[key])
|
|
|
|
+ ? diffDB(value, oldObject[key])
|
|
|
|
+ : value
|
|
|
|
+ if (newValue !== undefined && newValue !== null) {
|
|
|
|
+ result[key] = newValue
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ )
|
|
|
|
+ return Object.keys(transformResult).length > 0 ? transformResult : undefined
|
|
|
|
+}
|