-
Notifications
You must be signed in to change notification settings - Fork 339
[WV-2287] Moved ManageMyCandidates navigation to ManageMyCandidatesLanding.jsx and UI components to other files [MERGE READY] #4614
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
Open
ric0de
wants to merge
3
commits into
wevote:develop
Choose a base branch
from
ric0de:WV-2287ManageCandidatesMasterNav
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React from 'react'; | ||
| import React, { useMemo, useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import styled, { createGlobalStyle } from 'styled-components'; | ||
| import { WarningAmber as WarningIcon } from '@mui/icons-material'; | ||
|
|
@@ -7,16 +7,96 @@ import ModalDisplayTemplateA from '../Widgets/ModalDisplayTemplateA'; | |
|
|
||
| const escapeHTML = (s) => s.replace(/[&<>]/g, (element) => ({ '&': '&', '<': '<', '>': '>' }[element])); | ||
|
|
||
| const emailRE = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i; | ||
|
|
||
| function parsePastedList (text) { | ||
| const lines = text.split(/\r?\n/); | ||
| const rows = []; | ||
| const errors = []; | ||
|
|
||
| lines.forEach((raw, idx) => { | ||
| const line = raw.trim(); | ||
| if (!line) return; | ||
|
|
||
| const parts = line.split(',').map((s) => s.trim()).filter(Boolean); | ||
| const emailCount = (line.match(emailRE) || []).length; | ||
|
|
||
| if (parts.length > 3) { | ||
| errors.push({ line: idx, reason: 'Too many commas' }); | ||
| return; | ||
| } | ||
| if (emailCount > 1) { | ||
| errors.push({ line: idx, reason: 'Two emails on one line (missing line break?)' }); | ||
| return; | ||
| } | ||
| // Must have at least "Name, Email" | ||
| if (parts.length < 2) { | ||
| errors.push({ line: idx, reason: 'Missing email (format: Name, email[, phone])' }); | ||
| return; | ||
| } | ||
| // Validate email | ||
| const emailPart = parts[1].replace(/[<>]/g, ''); | ||
| if (!emailRE.test(emailPart)) { | ||
| errors.push({ line: idx, reason: 'Invalid email' }); | ||
| return; | ||
| } | ||
|
|
||
| rows.push({ | ||
| name: parts[0] || '', | ||
| email: emailPart || '', | ||
| phone: parts[2] || '', | ||
| }); | ||
| }); | ||
|
|
||
| return { rows, errors }; | ||
| } | ||
|
|
||
| function buildMirrorHTML (text, errors) { | ||
| const errSet = new Set(errors.map((e) => e.line)); | ||
| return text.split(/\r?\n/).map((line, i) => { | ||
| const isError = errSet.has(i) && line.trim().length > 0; | ||
| const safe = escapeHTML(line); | ||
| return isError ? `<span class="err">${safe}</span>` : safe; | ||
| }).join('\n'); | ||
| } | ||
|
|
||
| export default function PasteListModal ({ | ||
| isOpen, | ||
| onClose, | ||
| pasteText, | ||
| onPasteTextChange, | ||
| mirrorHTML, | ||
| pasteErrors, | ||
| onImport, | ||
| prospectiveCount, | ||
| }) { | ||
| const [pasteText, setPasteText] = useState(''); | ||
| const [pasteErrors, setPasteErrors] = useState([]); | ||
| const [mirrorHTML, setMirrorHTML] = useState(''); | ||
|
|
||
| const onPasteTextChange = (e) => { | ||
| const { value } = e.target; | ||
| setPasteText(value); | ||
| const { errors } = parsePastedList(value); | ||
| setPasteErrors(errors); | ||
| setMirrorHTML(buildMirrorHTML(value, errors)); | ||
| }; | ||
|
|
||
| const prospectiveCount = useMemo(() => parsePastedList(pasteText).rows.length, [pasteText]); | ||
|
|
||
| const handlePasteImport = () => { | ||
| const { rows, errors } = parsePastedList(pasteText); | ||
| if (errors.length) { | ||
| setPasteErrors(errors); | ||
| setMirrorHTML(buildMirrorHTML(pasteText, errors)); | ||
| return; // block import until fixed | ||
| } | ||
| onImport(rows); | ||
| }; | ||
|
|
||
| const handleClose = () => { | ||
| // Reset state on close | ||
| setPasteText(''); | ||
| setPasteErrors([]); | ||
| setMirrorHTML(''); | ||
| onClose(); | ||
| }; | ||
|
|
||
| const dialogTitleJSX = ( | ||
| <HeaderRow> | ||
| <Title>Paste voters</Title> | ||
|
|
@@ -93,10 +173,10 @@ John Dough, [email protected], (213)-123-4567`} | |
| </EditAreaWrapper> | ||
|
|
||
| <Footer style={{ justifyContent: 'space-between' }}> | ||
| <PlainButton type="button" onClick={onClose}>Cancel</PlainButton> | ||
| <PlainButton type="button" onClick={handleClose}>Cancel</PlainButton> | ||
| <PrimaryButton | ||
| type="button" | ||
| onClick={onImport} | ||
| onClick={handlePasteImport} | ||
| disabled={!pasteText.trim()} | ||
| > | ||
| {prospectiveCount ? | ||
|
|
@@ -113,7 +193,7 @@ John Dough, [email protected], (213)-123-4567`} | |
| <SoftenCorners /> | ||
| <ModalDisplayTemplateA | ||
| show={isOpen} | ||
| toggleModal={onClose} | ||
| toggleModal={handleClose} | ||
| externalUniqueId="pasteListModal" | ||
| dialogTitleJSX={dialogTitleJSX} | ||
| tallMode={false} | ||
|
|
@@ -126,15 +206,7 @@ John Dough, [email protected], (213)-123-4567`} | |
| PasteListModal.propTypes = { | ||
| isOpen: PropTypes.bool.isRequired, | ||
| onClose: PropTypes.func.isRequired, | ||
| pasteText: PropTypes.string.isRequired, | ||
| onPasteTextChange: PropTypes.func.isRequired, | ||
| mirrorHTML: PropTypes.string.isRequired, | ||
| pasteErrors: PropTypes.arrayOf(PropTypes.shape({ | ||
| line: PropTypes.number.isRequired, | ||
| reason: PropTypes.string.isRequired, | ||
| })).isRequired, | ||
| onImport: PropTypes.func.isRequired, | ||
| prospectiveCount: PropTypes.number.isRequired, | ||
| }; | ||
|
|
||
| // Global Styles | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React from 'react'; | ||
| import React, { useRef, useState } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import styled, { createGlobalStyle } from 'styled-components'; | ||
| import { FileDownloadOutlined as DownloadIcon, CheckCircle as CheckIcon } from '@mui/icons-material'; | ||
|
|
@@ -8,15 +8,88 @@ import ModalDisplayTemplateA from '../Widgets/ModalDisplayTemplateA'; | |
| export default function UploadCSVModal ({ | ||
| isOpen, | ||
| onClose, | ||
| onDownloadSample, | ||
| onSelectFile, | ||
| allColumnsOK, | ||
| onImport, | ||
| notify, | ||
| }) { | ||
| const fileInputRef = useRef(null); | ||
| const [allColumnsOK, setAllColumnsOK] = useState(false); | ||
|
|
||
| const handleDownloadSample = () => { | ||
| const csv = | ||
| 'Name,Email,Mobile,Address\n' + | ||
| 'John Smith,[email protected],"(123) 456-7890","123 State St, Anytown, CA 94117"\n'; | ||
| const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); | ||
| const url = URL.createObjectURL(blob); | ||
| const a = document.createElement('a'); | ||
| a.href = url; | ||
| a.download = 'wevote_sample.csv'; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| URL.revokeObjectURL(url); | ||
| }; | ||
|
|
||
| const handleSelectCSV = () => fileInputRef.current?.click(); | ||
|
|
||
| const handleCSVSelected = async (e) => { | ||
| const file = e.target.files?.[0]; | ||
| if (!file) return; | ||
|
|
||
| if (!/\.csv$/i.test(file.name)) { | ||
| notify('Please choose a .csv file.', false); | ||
| e.target.value = ''; | ||
| return; | ||
| } | ||
|
|
||
| const text = await file.text(); | ||
| const lines = text.replace(/\r/g, '').split('\n').filter((l) => l.trim().length > 0); | ||
| const [headerLine, ...dataLines] = lines; | ||
| const headers = headerLine.split(',').map((h) => h.trim().toLowerCase()); | ||
|
|
||
| const nameIdx = headers.indexOf('name'); | ||
| const emailIdx = headers.indexOf('email'); | ||
| const mobileIdx = headers.indexOf('mobile'); | ||
| const phoneIdx = headers.indexOf('phone'); | ||
| const phoneCol = mobileIdx > -1 ? mobileIdx : phoneIdx; | ||
| const addressIdx = headers.indexOf('address'); | ||
|
|
||
| const ok = nameIdx > -1 && emailIdx > -1 && phoneCol > -1 && addressIdx > -1; | ||
|
|
||
| const rows = dataLines.map((line) => { | ||
| const cols = line.split(',').map((supporter) => supporter.trim()); | ||
| return { | ||
| name: nameIdx > -1 ? cols[nameIdx] : '', | ||
| email: emailIdx > -1 ? cols[emailIdx] : '', | ||
| phone: phoneCol > -1 ? cols[phoneCol] : '', | ||
| address: addressIdx > -1 ? cols[addressIdx] : '', | ||
| }; | ||
| }).filter((r) => r.name || r.email || r.phone || r.address); | ||
|
|
||
| if (ok && rows.length > 0) { | ||
| onImport(rows); | ||
| setAllColumnsOK(false); | ||
| } else { | ||
| setAllColumnsOK(ok); | ||
| if (!ok) { | ||
| notify('We could not find all required columns (Name, Email, Mobile/Phone, Address). Please adjust and re-upload.', false); | ||
| } else if (rows.length === 0) { | ||
| notify('No rows found in this CSV.', false); | ||
| } | ||
| } | ||
|
|
||
| e.target.value = ''; | ||
| }; | ||
|
|
||
| const handleClose = () => { | ||
| setAllColumnsOK(false); | ||
| onClose(); | ||
| }; | ||
|
|
||
| const dialogTitleJSX = ( | ||
| <HeaderRow> | ||
| <Title>Upload CSV file</Title> | ||
| <HeaderDivider /> | ||
| <HeaderLink type="button" onClick={onDownloadSample}> | ||
| <HeaderLink type="button" onClick={handleDownloadSample}> | ||
| <DownloadIcon fontSize="small" /> | ||
| <span>Download sample file</span> | ||
| </HeaderLink> | ||
|
|
@@ -60,9 +133,17 @@ export default function UploadCSVModal ({ | |
| </Grid> | ||
|
|
||
| <Footer> | ||
| <CancelButton type="button" onClick={onClose}>Cancel</CancelButton> | ||
| <SelectButton type="button" onClick={onSelectFile}>Select file</SelectButton> | ||
| <CancelButton type="button" onClick={handleClose}>Cancel</CancelButton> | ||
| <SelectButton type="button" onClick={handleSelectCSV}>Select file</SelectButton> | ||
| </Footer> | ||
|
|
||
| <input | ||
| ref={fileInputRef} | ||
| type="file" | ||
| accept=".csv" | ||
| style={{ display: 'none' }} | ||
| onChange={handleCSVSelected} | ||
| /> | ||
| </div> | ||
| ); | ||
|
|
||
|
|
@@ -73,7 +154,7 @@ export default function UploadCSVModal ({ | |
| <SoftenCorners /> | ||
| <ModalDisplayTemplateA | ||
| show={isOpen} | ||
| toggleModal={onClose} | ||
| toggleModal={handleClose} | ||
| externalUniqueId="uploadCSVModal" | ||
| dialogTitleJSX={dialogTitleJSX} | ||
| tallMode={false} | ||
|
|
@@ -86,9 +167,8 @@ export default function UploadCSVModal ({ | |
| UploadCSVModal.propTypes = { | ||
| isOpen: PropTypes.bool.isRequired, | ||
| onClose: PropTypes.func.isRequired, | ||
| onDownloadSample: PropTypes.func.isRequired, | ||
| onSelectFile: PropTypes.func.isRequired, | ||
| allColumnsOK: PropTypes.bool.isRequired, | ||
| onImport: PropTypes.func.isRequired, | ||
| notify: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| // Global Styles | ||
|
|
||
Oops, something went wrong.
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.
Looks like this element is missing an accessible name or label. That makes it hard for people using screen readers or voice control to use the control.