From 57bb2f88f86d994431aecd40cd886d96bcbe7483 Mon Sep 17 00:00:00 2001 From: James Chang Date: Wed, 19 May 2021 16:22:56 -0400 Subject: [PATCH] Resolve #19 -- ability to dump raw PB data on My PBs (All) page --- frontend/mixins/crud.js | 91 +++++++++++++++++++------------- frontend/models/personalBest.ts | 16 +++++- frontend/models/special/myPbs.ts | 1 - frontend/types/index.ts | 5 +- 4 files changed, 73 insertions(+), 40 deletions(-) diff --git a/frontend/mixins/crud.js b/frontend/mixins/crud.js index 89dd556..d8556de 100644 --- a/frontend/mixins/crud.js +++ b/frontend/mixins/crud.js @@ -522,39 +522,47 @@ export default { // create a map field -> serializeFn for fast serialization const serializeMap = new Map() - const query = collapseObject( + // use custom download fields if provided + const customFields = this.recordInfo.paginationOptions.downloadOptions + .fields + const fields = + customFields ?? this.recordInfo.paginationOptions.headers .concat( (this.recordInfo.requiredFields ?? []).map((field) => ({ field, })) ) - .reduce( - (total, headerInfo) => { - const fieldInfo = this.recordInfo.fields[headerInfo.field] + .map((headerObject) => headerObject.field) - // field unknown, abort - if (!fieldInfo) - throw new Error('Unknown field: ' + headerInfo.field) + if (fields.length < 1) throw new Error('No fields to export') - // if field has '+', add all of the fields - if (headerInfo.field.match(/\+/)) { - headerInfo.field.split(/\+/).forEach((field) => { - total[field] = true - // assuming all fields are valid - serializeMap.set( - field, - this.recordInfo.fields[field].serialize - ) - }) - } else { - total[headerInfo.field] = true - serializeMap.set(headerInfo.field, fieldInfo.serialize) - } - return total - }, - { id: true } // always add id - ) + const query = collapseObject( + fields.reduce( + (total, field) => { + const fieldInfo = this.recordInfo.fields[field] + + // field unknown, abort + if (!fieldInfo) throw new Error('Unknown field: ' + field) + + // if field has '+', add all of the fields + if (field.match(/\+/)) { + field.split(/\+/).forEach((field) => { + total[field] = true + // assuming all fields are valid + serializeMap.set( + field, + this.recordInfo.fields[field].serialize + ) + }) + } else { + total[field] = true + serializeMap.set(field, fieldInfo.serialize) + } + return total + }, + { id: true } // always add id + ) ) const args = { @@ -614,18 +622,27 @@ export default { }) }) - const data = results.map((item) => { - const returnItem = {} - this.headers.forEach((headerObject) => { - if (headerObject.value) { - returnItem[headerObject.value] = this.getTableRowData( - headerObject, - item - ) - } - }) - return returnItem - }) + // extract results + const data = customFields + ? results.map((item) => { + const returnItem = {} + customFields.forEach((field) => { + returnItem[field] = getNestedProperty(item, field) + }) + return returnItem + }) + : results.map((item) => { + const returnItem = {} + this.headers.forEach((headerObject) => { + if (headerObject.value) { + returnItem[headerObject.value] = this.getTableRowData( + headerObject, + item + ) + } + }) + return returnItem + }) if (data.length < 1) { throw new Error('No results to export') diff --git a/frontend/models/personalBest.ts b/frontend/models/personalBest.ts index 19b0bfa..450785b 100644 --- a/frontend/models/personalBest.ts +++ b/frontend/models/personalBest.ts @@ -316,7 +316,21 @@ export const PersonalBest = >{ sortable: false, }, ], - downloadOptions: {}, + downloadOptions: { + fields: [ + 'event.name', + 'pbClass.name', + 'setSize', + 'timeElapsed', + 'movesCount', + 'attemptsSucceeded', + 'attemptsTotal', + 'event.scoreMethod', + 'score', + 'happenedOn', + 'createdBy.name', + ], + }, }, addOptions: { diff --git a/frontend/models/special/myPbs.ts b/frontend/models/special/myPbs.ts index ba600f1..cc30ca8 100644 --- a/frontend/models/special/myPbs.ts +++ b/frontend/models/special/myPbs.ts @@ -5,7 +5,6 @@ const MyPbs = { routeName: 'i-view', paginationOptions: { ...(!!PersonalBest.paginationOptions && PersonalBest.paginationOptions), - downloadOptions: undefined, }, enterOptions: {}, } diff --git a/frontend/types/index.ts b/frontend/types/index.ts index 99df8c6..0af81f8 100644 --- a/frontend/types/index.ts +++ b/frontend/types/index.ts @@ -90,7 +90,10 @@ export type RecordInfo = { // custom component interfaceComponent?: any // can the results be downloaded? - downloadOptions?: {} + downloadOptions?: { + // custom fields to download. otherwise, the header fields will be downloaded + fields?: string[] + } } addOptions?: {