Skip to content

Commit

Permalink
Resolve #19 -- ability to dump raw PB data on My PBs (All) page
Browse files Browse the repository at this point in the history
  • Loading branch information
big213 committed May 19, 2021
1 parent c00e258 commit 57bb2f8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 40 deletions.
91 changes: 54 additions & 37 deletions frontend/mixins/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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')
Expand Down
16 changes: 15 additions & 1 deletion frontend/models/personalBest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,21 @@ export const PersonalBest = <RecordInfo<'personalBest'>>{
sortable: false,
},
],
downloadOptions: {},
downloadOptions: {
fields: [
'event.name',
'pbClass.name',
'setSize',
'timeElapsed',
'movesCount',
'attemptsSucceeded',
'attemptsTotal',
'event.scoreMethod',
'score',
'happenedOn',
'createdBy.name',
],
},
},

addOptions: {
Expand Down
1 change: 0 additions & 1 deletion frontend/models/special/myPbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const MyPbs = <any>{
routeName: 'i-view',
paginationOptions: {
...(!!PersonalBest.paginationOptions && PersonalBest.paginationOptions),
downloadOptions: undefined,
},
enterOptions: {},
}
Expand Down
5 changes: 4 additions & 1 deletion frontend/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export type RecordInfo<T extends keyof MainTypes> = {
// 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?: {
Expand Down

0 comments on commit 57bb2f8

Please sign in to comment.