|
| 1 | +/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */ |
| 2 | +/* eslint-disable no-fallthrough */ |
| 3 | +import React, { useState } from 'react' |
| 4 | +import Icon from '@conveyal/woonerf/components/icon' |
| 5 | +import { ListGroupItem, Table } from 'react-bootstrap' |
| 6 | +import Markdown from 'markdown-to-jsx' |
| 7 | + |
| 8 | +import toSentenceCase, { spaceOutNumbers } from '../../../common/util/text' |
| 9 | +import { |
| 10 | + mobilityDataValidationErrorMapping, |
| 11 | + validationErrorIconLookup |
| 12 | +} from '../../util/version' |
| 13 | + |
| 14 | +import rules from './rules.json' |
| 15 | + |
| 16 | +// from https://stackoverflow.com/a/4149671 |
| 17 | +function unCamelCase (s) { |
| 18 | + return s |
| 19 | + .split(/(?=[A-Z])/) |
| 20 | + .join(' ') |
| 21 | + .toLowerCase() |
| 22 | +} |
| 23 | + |
| 24 | +const NoticeTable = ({ headerOverides = { |
| 25 | + 'stopSequence1': 'Stop seq-uence 1', |
| 26 | + 'stopSequence2': 'Stop seq-uence 2' |
| 27 | +}, notices }) => { |
| 28 | + if (notices.length === 0) return null |
| 29 | + |
| 30 | + const headers = Object.keys(notices[0]) |
| 31 | + |
| 32 | + return ( |
| 33 | + <Table bordered className='table-fixed' fill hover striped style={{display: 'relative', borderCollapse: 'collapse'}}> |
| 34 | + <thead> |
| 35 | + <tr> |
| 36 | + {headers.map((header) => ( |
| 37 | + <th style={{position: 'sticky', top: 0, background: '#fefefe'}}> |
| 38 | + {headerOverides[header] || toSentenceCase(unCamelCase(header))} |
| 39 | + </th> |
| 40 | + ))} |
| 41 | + </tr> |
| 42 | + </thead> |
| 43 | + <tbody> |
| 44 | + {notices.map((notice) => ( |
| 45 | + <tr> |
| 46 | + {headers.map((header, index) => { |
| 47 | + const FieldWrapper = |
| 48 | + (header === 'fieldValue' || header === 'message') ? 'pre' : React.Fragment |
| 49 | + |
| 50 | + let field = notice[header] |
| 51 | + if (header.endsWith('Km') || header.endsWith('Kph')) { |
| 52 | + field = Math.round(field) |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <td key={`${header}-${index}`}> |
| 57 | + <FieldWrapper>{field}</FieldWrapper> |
| 58 | + </td> |
| 59 | + ) |
| 60 | + })} |
| 61 | + </tr> |
| 62 | + ))} |
| 63 | + </tbody> |
| 64 | + </Table> |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +// eslint-disable-next-line complexity |
| 69 | +const renderNoticeDetail = (notice) => { |
| 70 | + switch (notice.code) { |
| 71 | + case 'too_many_rows': |
| 72 | + notice.csvRowNumber = notice.rowNumber |
| 73 | + case 'fare_transfer_rule_duration_limit_type_without_duration_limit': |
| 74 | + case 'fare_transfer_rule_duration_limit_without_type': |
| 75 | + case 'fare_transfer_rule_missing_transfer_count': |
| 76 | + case 'fare_transfer_rule_with_forbidden_transfer_count': |
| 77 | + notice.filename = 'fare_transfer_rules.txt' |
| 78 | + case 'empty_file': |
| 79 | + case 'emtpy_row': |
| 80 | + case 'missing_timepoint_column': |
| 81 | + case 'missing_required_file': |
| 82 | + case 'missing_recommended_file': |
| 83 | + case 'unknown_file': |
| 84 | + return ( |
| 85 | + <ul> |
| 86 | + {notice.sampleNotices.map((notice) => ( |
| 87 | + <li> |
| 88 | + {notice.filename} |
| 89 | + {notice.csvRowNumber && `: row ${notice.csvRowNumber}`} |
| 90 | + </li> |
| 91 | + ))} |
| 92 | + </ul> |
| 93 | + ) |
| 94 | + default: |
| 95 | + return ( |
| 96 | + <NoticeTable notices={notice.sampleNotices} /> |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const MobilityDataValidationResult = ({notice}) => { |
| 102 | + const rule = rules.find((rd) => rd.rule === notice.code) |
| 103 | + if (!rule) return null |
| 104 | + |
| 105 | + const errorClass = `gtfs-error-${mobilityDataValidationErrorMapping[notice.severity]}` |
| 106 | + const [expanded, setExpanded] = useState(notice.totalNotices < 2) |
| 107 | + |
| 108 | + const onRowSelect = () => setExpanded(!expanded) |
| 109 | + |
| 110 | + return ( |
| 111 | + <ListGroupItem style={{ padding: 0 }}> |
| 112 | + <div style={{ padding: '10px 15px' }}> |
| 113 | + <h4 |
| 114 | + onClick={onRowSelect} |
| 115 | + onKeyDown={onRowSelect} |
| 116 | + style={{ cursor: 'pointer' }} |
| 117 | + > |
| 118 | + <span |
| 119 | + className={`buffer-icon ${errorClass}`} |
| 120 | + title={`${toSentenceCase(notice.severity)} priority`} |
| 121 | + > |
| 122 | + <Icon |
| 123 | + type={validationErrorIconLookup[mobilityDataValidationErrorMapping[notice.severity]]} |
| 124 | + /> |
| 125 | + </span> |
| 126 | + {toSentenceCase(spaceOutNumbers(notice.code))} |
| 127 | + <span className={errorClass}> |
| 128 | + {' '} |
| 129 | + — {notice.totalNotices} case |
| 130 | + {notice.totalNotices > 1 ? 's' : ''} found |
| 131 | + </span> |
| 132 | + <span className={`pull-right`}> |
| 133 | + <Icon type={expanded ? 'caret-up' : 'caret-down'} /> |
| 134 | + </span> |
| 135 | + </h4> |
| 136 | + {expanded && ( |
| 137 | + <> |
| 138 | + <p><Markdown>{rule.description}</Markdown></p> |
| 139 | + <p> |
| 140 | + <a |
| 141 | + href={`https://github.com/MobilityData/gtfs-validator/blob/master/RULES.md#${notice.code}`} |
| 142 | + target='_blank' |
| 143 | + rel='noreferrer' |
| 144 | + > |
| 145 | + More details |
| 146 | + </a> |
| 147 | + </p> |
| 148 | + </> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + <div>{expanded && renderNoticeDetail(notice)}</div> |
| 152 | + </ListGroupItem> |
| 153 | + ) |
| 154 | +} |
| 155 | + |
| 156 | +export default MobilityDataValidationResult |
0 commit comments