-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Waitp 1338 add matrices #4725
Merged
Merged
Waitp 1338 add matrices #4725
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bff494f
Placeholder images for dashboard
alexd-bes 224115a
Working matrix
alexd-bes 0208aab
Merge branch 'epic-frontend-rewrite' into waitp-1338-add-matrices
alexd-bes f7fde28
Add description and handle no data
alexd-bes db90349
Add comments
alexd-bes 0df3610
Fix grouping and width
alexd-bes 564316c
Merge branch 'epic-frontend-rewrite' into waitp-1338-add-matrices
alexd-bes 267328b
Move getPlaceholderImage
alexd-bes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import { | ||
MatrixColumnType, | ||
MatrixRowType, | ||
getIsUsingDots, | ||
Matrix as MatrixComponent, | ||
Alert, | ||
} from '@tupaia/ui-components'; | ||
import { MatrixDataColumn, MatrixDataRow, MatrixViewContent } from '../types'; | ||
import { ConditionalPresentationOptions } from '@tupaia/types'; | ||
import styled from 'styled-components'; | ||
|
||
const NoDataMessage = styled(Alert).attrs({ | ||
severity: 'info', | ||
})` | ||
width: 100%; | ||
margin: 1rem auto; | ||
max-width: 24rem; | ||
`; | ||
|
||
// This is a recursive function that parses the rows of the matrix into a format that the Matrix component can use. | ||
const parseRows = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a lot going on here but separating it out into parseRows and parseColumns and all the comments help clarify it a lot. |
||
rows: MatrixDataRow[], | ||
categoryId?: MatrixDataRow['categoryId'], | ||
): MatrixRowType[] => { | ||
let topLevelRows = []; | ||
// if a categoryId is not passed in, then we need to find the top level rows | ||
if (!categoryId) { | ||
// get the highest level rows, which are the ones that have a category but no categoryId | ||
const highestLevel = rows.filter(row => row.category && !row.categoryId); | ||
// if there are no highest level rows, then the top level rows are just all of the rows | ||
topLevelRows = highestLevel.length ? highestLevel : rows; | ||
} else { | ||
// otherwise, the top level rows are the ones that have the categoryId that was passed in | ||
topLevelRows = rows.filter(row => row.categoryId === categoryId); | ||
} | ||
|
||
// loop through the topLevelRows, and parse them into the format that the Matrix component can use | ||
return topLevelRows.map(row => { | ||
const { dataElement = '', category, ...rest } = row; | ||
// if the row has a category, then it has children, so we need to parse them using this same function | ||
if (category) { | ||
return { | ||
title: category, | ||
...rest, | ||
children: parseRows(rows, category), | ||
}; | ||
} | ||
// otherwise, handle as a regular row | ||
return { | ||
title: dataElement, | ||
...rest, | ||
}; | ||
}); | ||
}; | ||
|
||
// This is a recursive function that parses the columns of the matrix into a format that the Matrix component can use. | ||
const parseColumns = (columns: MatrixDataColumn[]): MatrixColumnType[] => { | ||
return columns.map(column => { | ||
const { category, key, title, columns: children } = column; | ||
// if a column has a category, then it has children, so we need to parse them using this same function | ||
if (category) | ||
return { | ||
title: category, | ||
key: category, | ||
children: parseColumns(children!), | ||
}; | ||
// otherwise, handle as a regular column | ||
return { | ||
title, | ||
key, | ||
}; | ||
}); | ||
}; | ||
|
||
const getPlaceholderImage = ({ presentationOptions = {}, categoryPresentationOptions = {} }) => { | ||
// if the matrix is not using any dots, show a text-only placeholder | ||
if (!getIsUsingDots(presentationOptions) && !getIsUsingDots(categoryPresentationOptions)) | ||
return '/images/matrix-placeholder-text-only.png'; | ||
// if the matrix has applyLocation.columnIndexes, show a mix placeholder, because this means it is a mix of dots and text | ||
if ((presentationOptions as ConditionalPresentationOptions)?.applyLocation?.columnIndexes) | ||
return '/images/matrix-placeholder-mix.png'; | ||
// otherwise, show a dot-only placeholder | ||
return '/images/matrix-placeholder-dot-only.png'; | ||
}; | ||
|
||
/** | ||
* This is the component that is used to display a matrix. It handles the parsing of the data into the format that the Matrix component can use, as well as placeholder images. It shows a message when there are no rows available to display. | ||
*/ | ||
|
||
interface MatrixProps { | ||
viewContent: MatrixViewContent; | ||
isEnlarged?: boolean; | ||
} | ||
export const Matrix = ({ viewContent, isEnlarged = false }: MatrixProps) => { | ||
const { columns, rows, ...config } = viewContent; | ||
|
||
const placeholderImage = getPlaceholderImage(config); | ||
// in the dashboard, show a placeholder image | ||
if (!isEnlarged) return <img src={placeholderImage} alt="Matrix Placeholder" />; | ||
|
||
const parsedRows = parseRows(rows); | ||
const parsedColumns = parseColumns(columns); | ||
|
||
if (!parsedRows.length) return <NoDataMessage>No data available</NoDataMessage>; | ||
|
||
return <MatrixComponent {...config} rows={parsedRows} columns={parsedColumns} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
*/ | ||
|
||
export { Matrix } from './Matrix'; | ||
export * from './utils'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good pick up on this