Skip to content

Commit

Permalink
Update logic and add more default hidden - #1121
Browse files Browse the repository at this point in the history
  • Loading branch information
libpitt committed Jan 3, 2024
1 parent 0d1a4f9 commit f240011
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
28 changes: 16 additions & 12 deletions src/components/custom/TableResultsEntities.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,25 @@ function TableResultsEntities({children, filters, onRowClicked, forData = false,
name: 'Organ',
selector: row => getUBKGFullName(raw(row.origin_sample)?.organ),
sortable: true,
}
}

const sourceColumns = [
{
},
SourceType: {
name: 'Type',
selector: row => raw(row.source_type),
sortable: true,
}
]

const sampleColumns = [
{
},
SampleCategory: {
name: 'Category',
selector: row => raw(row.sample_category) ? displayBodyHeader(raw(row.sample_category)) : null,
sortable: true,
},
}

const sourceColumns = [
reusableColumns.SourceType
]

const sampleColumns = [
reusableColumns.SampleCategory,
reusableColumns.Organ
]

Expand Down Expand Up @@ -183,6 +185,8 @@ function TableResultsEntities({children, filters, onRowClicked, forData = false,
let cols;
if (checkFilterType(filters) === false) {
cols = defaultColumns({});
cols.push(reusableColumns.SourceType)
cols.push(reusableColumns.SampleCategory)
cols.push(reusableColumns.Status)
} else {
let typeIndex = 0;
Expand Down Expand Up @@ -219,7 +223,7 @@ function TableResultsEntities({children, filters, onRowClicked, forData = false,
if (columnsToHide) {
hiddenColumns.current = columnsToHide
for (let col of cols) {
col.omit = columnsToHide[col.name]
col.omit = columnsToHide[col.name] || false
}
}
currentColumns.current = cols;
Expand All @@ -237,7 +241,7 @@ function TableResultsEntities({children, filters, onRowClicked, forData = false,
<>
<TableResultsProvider columnsRef={currentColumns} getId={getId} getHotLink={getHotLink} rows={children} filters={filters} onRowClicked={onRowClicked} forData={forData} raw={raw} inModal={inModal}>
<ResultsBlock
defaultHiddenColumns={['Status']}
defaultHiddenColumns={['Status', 'Type', 'Category']}
getTableColumns={getTableColumns}
/>
<AppModal
Expand Down
32 changes: 24 additions & 8 deletions src/components/custom/search/ColumnsDropdown.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {useRef, useEffect, useState} from 'react'
import {useRef, useEffect} from 'react'
import PropTypes from 'prop-types'
import Select from 'react-select'
import $ from 'jquery'


function ColumnsDropdown({ getTableColumns, setHiddenColumns, currentColumns, filters, defaultHiddenColumns = [] }) {
const multiVals = useRef(null)
const loaded = useRef(false)

const colourStyles = {
multiValueRemove: (styles, { data }) => ({
Expand All @@ -20,7 +19,6 @@ function ColumnsDropdown({ getTableColumns, setHiddenColumns, currentColumns, fi

const handleChange = (e) => {
multiVals.current = e

// Prevent user from removing all columns
if (e.length === (getTableColumns().length - 1)) {
e.pop()
Expand All @@ -36,22 +34,34 @@ function ColumnsDropdown({ getTableColumns, setHiddenColumns, currentColumns, fi

const handleDefaultHidden = (set = true) => {
let defaultHidden = null
let currentShowing = {}

if (!filters || !filters.length) {
loaded.current = true
for (let col of currentColumns.current) {
if (col.omit === false && !col.ignoreRowClick) {
currentShowing[col.name] = true
}
}

let currentVals = []
defaultHidden = {}
for (let col of defaultHiddenColumns) {
currentVals.push({ value: col, label: col })
defaultHidden[col] = true
if (!currentShowing[col]) {
currentVals.push({ value: col, label: col })
}
defaultHidden[col] = true
}
multiVals.current = Array.from(currentVals)
} else {
multiVals.current = null
}
setHiddenColumns(defaultHidden)
if (set) {
setHiddenColumns(defaultHidden)
}
}

const getColumnOptions = () => {
handleDefaultHidden(false)
if (!currentColumns.current) return []
let allColumns = Array.from(currentColumns.current)
allColumns.splice(0, 1)
Expand All @@ -65,12 +75,18 @@ function ColumnsDropdown({ getTableColumns, setHiddenColumns, currentColumns, fi
}

useEffect(() => {
const clearBtnSelector = '.clear-filter-button'
// Have to listen to click from here instead of in handleClearFiltersClick
// to manage value states of this independent component
$('body').on('click', '.clear-filter-button', () => {
$('body').on('click', clearBtnSelector, () => {
handleDefaultHidden()
})

handleDefaultHidden()
if (!filters || !filters.length) {
$(clearBtnSelector).trigger('click')
}

}, [])

return (
Expand Down

0 comments on commit f240011

Please sign in to comment.