Skip to content

Commit

Permalink
Merge pull request #769 from digital-land/rosado/755-column-order
Browse files Browse the repository at this point in the history
Ensure column order in table view
  • Loading branch information
rosado authored Jan 21, 2025
2 parents 2244213 + a896b51 commit 2fcf34a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/middleware/dataview.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ export const setRecordCount = (req, res, next) => {
export const constructTableParams = (req, res, next) => {
const { entities, uniqueDatasetFields } = req

const columns = uniqueDatasetFields
const fields = uniqueDatasetFields
const leadingFields = []
const trailngFields = []
for (const field of uniqueDatasetFields) {
if (field === 'reference') {
leadingFields.splice(0, 0, field)
} else if (field === 'name') {
leadingFields.push(field)
} else {
trailngFields.push(field)
}
}
const orderedDatasetFields = [...leadingFields, ...trailngFields]
const columns = orderedDatasetFields
const fields = orderedDatasetFields
const rows = entities.map(entity => ({
columns: Object.fromEntries(fields.map(field => {
let value = entity[field]
Expand Down
30 changes: 30 additions & 0 deletions test/unit/middleware/dataview.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ describe('dataview.middleware.test.js', () => {
expect(next).toHaveBeenCalledTimes(1)
})

it('constructs table parameters with "name" and "refernce" as leading columns', () => {
const req = {
entities: [{ id: 2, foo_field: 'foo2', baz_field: 'quux', name: 'baz' }, { id: 1, name: 'bar', foo_field: 'foo1', baz_field: 'qux' }],
uniqueDatasetFields: ['name', 'baz_field']
}
const res = {}
const next = vi.fn()

constructTableParams(req, res, next)

expect(req.tableParams.columns[0]).toBe('name')
expect(req.tableParams.fields[0]).toBe('name')
})

it('constructs table parameters "refernce" as first column', () => {
const req = {
entities: [{ id: 2, name: 'item 2', reference: '002' }, { id: 1, name: 'bar', reference: '001' }],
uniqueDatasetFields: ['id', 'name', 'reference']
}
const res = {}
const next = vi.fn()

constructTableParams(req, res, next)

expect(req.tableParams.columns[0]).toBe('reference')
expect(req.tableParams.fields[0]).toBe('reference')
expect(req.tableParams.columns[1]).toBe('name')
expect(req.tableParams.fields[1]).toBe('name')
})

it('constructs table parameters with correct html field as a link for url values', () => {
const req = {
entities: [
Expand Down

0 comments on commit 2fcf34a

Please sign in to comment.