From 26ec20d97be268b18813c5856a5a386215cbf571 Mon Sep 17 00:00:00 2001 From: Blagoslav Mihaylov Date: Fri, 9 Jul 2021 04:15:51 +0300 Subject: [PATCH] Add party row columns for variable number of machines --- .../process_protocols/VerifyProtocolInfo.js | 29 ++-- .../validation_form/ProtocolForm.js | 162 ++++++++---------- .../validation_form/SectionDetails.js | 6 +- .../validation_form/ValidationFormState.js | 127 ++++++++------ 4 files changed, 163 insertions(+), 161 deletions(-) diff --git a/src/components/process_protocols/VerifyProtocolInfo.js b/src/components/process_protocols/VerifyProtocolInfo.js index 5572172..b53bf14 100644 --- a/src/components/process_protocols/VerifyProtocolInfo.js +++ b/src/components/process_protocols/VerifyProtocolInfo.js @@ -150,17 +150,18 @@ export default props => { const [protocolType, setProtocolType] = useState('unset'); const [machineCount, setMachineCount] = useState(0); const [isFinal, setIsFinal] = useState(false); - - const [formState, setFormState] = useState(new ValidationFormState({ protocol: props.protocol, parties, allParties })); - const { fieldStatus, invalidFields, changedFields } = formState.getFieldStatus(props.protocol, parties, allParties, sectionData, protocolType); + + const [formState, setFormState] = useState(() => new ValidationFormState({ protocol: props.protocol, parties, protocolType, machineCount })); + const { fieldStatus, invalidFields, changedFields } = formState.getFieldStatus(props.protocol, parties, protocolType, machineCount ); const ref = useRef(); useEffect(() => { - setFormState(new ValidationFormState({ protocol: props.protocol, parties, allParties })); + setFormState(new ValidationFormState({ protocol: props.protocol, parties, protocolType, machineCount })); + setMachineCount(0); }, [protocolType]); useEffect(() => { - setFormState(new ValidationFormState({ protocol: props.protocol, parties, allParties })); + setFormState(new ValidationFormState({ protocol: props.protocol, parties, protocolType, machineCount })); }, [machineCount]); useKeypress(['ArrowUp'], event => { @@ -298,8 +299,6 @@ export default props => { }; const replaceProtocol = async () => { - const results = formState.generateResults(sectionData.isMachine); - const postBody = { section: { id: formState.formData.sectionId }, hasPaperBallots: protocolType === 'paper' || protocolType === 'paper-machine', @@ -313,18 +312,12 @@ export default props => { machineVotesCount: parseInt(formState.formData.machineVotesCount, 10), invalidVotesCount: parseInt(formState.formData.invalidVotesCount, 10), validVotesCount: parseInt(formState.formData.validVotesCount, 10), - results: { - results: Object.keys(results).map(key => { - return { party: parseInt(key, 10), - validVotesCount: results[key].validVotesCount, - machineVotesCount: results[key].machineVotesCount, - nonMachineVotesCount: results[key].nonMachineVotesCount, - }; - }) - }, + results: formState.generateResults(parties, protocolType, machineCount), pictures: props.protocol.pictures, }; + console.log(postBody); + props.setLoading(true); try { const res = await authPost(`/protocols/${props.protocol.id}/replace`, postBody); @@ -386,6 +379,7 @@ export default props => { setProtocolType={setProtocolType} machineCount={machineCount} setMachineCount={setMachineCount} + setIsFinal={setIsFinal} /> { protocolType === 'unset' || @@ -396,11 +390,10 @@ export default props => { handleNumberChange={handleNumberChange} handleResultsChange={handleResultsChange} formState={formState} - sectionData={sectionData} parties={parties} allParties={allParties} protocolType={protocolType} - setIsFinal={setIsFinal} + machineCount={machineCount} /> } {protocolType !== 'unset'?
: null} diff --git a/src/components/process_protocols/validation_form/ProtocolForm.js b/src/components/process_protocols/validation_form/ProtocolForm.js index cc70cf6..bae2c1a 100644 --- a/src/components/process_protocols/validation_form/ProtocolForm.js +++ b/src/components/process_protocols/validation_form/ProtocolForm.js @@ -93,16 +93,22 @@ const PartyResultsTable = styled.table` cursor: pointer; } - ${props => props.isMachine? ` - td:nth-child(1) { width: 8%; } - td:nth-child(2) { width: 42%; } - td:nth-child(3) { width: 30%; } - td:nth-child(4) { width: 20%; } - ` : ` - td:nth-child(1) { width: 8%; } - td:nth-child(2) { width: 72%; } - td:nth-child(3) { width: 20%; } - `} + ${props => props.colCount === 1? ` + td:nth-child(1), th:nth-child(1) { width: 8%; } + td:nth-child(2), th:nth-child(2) { width: 72%; } + td:nth-child(3), th:nth-child(3) { width: 20%; } + ` : props.colCount === 2? ` + td:nth-child(1), th:nth-child(1) { width: 8%; } + td:nth-child(2), th:nth-child(2) { width: 72%; } + td:nth-child(3), th:nth-child(3) { width: 10%; } + td:nth-child(4), th:nth-child(4) { width: 10%; } + ` : props.colCount === 3? ` + td:nth-child(1), th:nth-child(1) { width: 8%; } + td:nth-child(2), th:nth-child(2) { width: 71%; } + td:nth-child(3), th:nth-child(3) { width: 7%; } + td:nth-child(4), th:nth-child(4) { width: 7%; } + td:nth-child(5), th:nth-child(5) { width: 7%; } + ` : ``} `; const PartyNumber = styled.span` @@ -121,83 +127,58 @@ const svgIcon = '< export default props => { const partyRow = (party, i) => { - const status = props.fieldStatus[`party${party.id}`]; - const statusM = props.fieldStatus[`party${party.id}m`]; - const statusNM = props.fieldStatus[`party${party.id}nm`]; - return( - !props.sectionData.isMachine - ? -
- {party.id.toString() === '0'? null : - party.color? - {party.id} : - {party.id} - } - - {party.displayName} - - - -
- : <> -
- {party.id.toString() === '0'? null : - party.color? - {party.id} : - {party.id} - } - - - {party.displayName} - - от бюлетини - - - -
-
- - от маш. гласове - - - -
-
- - общо Б + МГ - - - -
- +
+ {party.id.toString() === '0'? null : + party.color? + {party.id} : + {party.id} + } + + {party.displayName} + { + props.protocolType === 'paper' || props.protocolType === 'paper-machine'? + + + : null + + } + { + [...Array(props.machineCount).keys()].map(i => + + + + ) + } + +
); }; + const calculateColCount = () => { + let count = 0; + + if(props.protocolType === 'paper' || props.protocolType === 'paper-machine') { + count += 1; + } + + count += props.machineCount; + + console.log(count); + + return count; + }; + return(

ДАННИ ОТ ИЗБИРАТЕЛНИЯ СПИСЪК

@@ -205,9 +186,7 @@ export default props => { - 1. Брой на избирателите в избирателния списък при предаването му на СИК, - включително и вписаните в допълнителната страница (под чертата) на избирателния - списък в изборния ден + 1. Брой на избирателите в избирателния списък при предаването му на СИК {

7. РАЗПРЕДЕЛЕНИЕ НА ГЛАСОВЕТЕ ПО КАНДИДАТСКИ ЛИСТИ

- + + + # + Име + { + props.protocolType === 'paper' || props.protocolType === 'paper-machine'? + Х : null + } + {[...Array(props.machineCount).keys()].map(i => M{i+1})} + {props.parties.filter(party => (props.allParties? true : party.isFeatured)) .map(partyRow)} diff --git a/src/components/process_protocols/validation_form/SectionDetails.js b/src/components/process_protocols/validation_form/SectionDetails.js index 5a2be5b..17d7a35 100644 --- a/src/components/process_protocols/validation_form/SectionDetails.js +++ b/src/components/process_protocols/validation_form/SectionDetails.js @@ -142,7 +142,7 @@ export default props => { props.setIsFinal(false)}/> props.setIsFinal(true)}/> - +
Изберете вид протокол
@@ -158,9 +158,9 @@ export default props => { <>
Брой машини
- props.setMachineCount(1)}/> + props.setMachineCount(1)} checked={props.machineCount === 1}/> - props.setMachineCount(2)}/> + props.setMachineCount(2)} checked={props.machineCount === 2}/> : null diff --git a/src/components/process_protocols/validation_form/ValidationFormState.js b/src/components/process_protocols/validation_form/ValidationFormState.js index 70793f7..8e95d0d 100644 --- a/src/components/process_protocols/validation_form/ValidationFormState.js +++ b/src/components/process_protocols/validation_form/ValidationFormState.js @@ -1,6 +1,6 @@ export default class ValidationFormState { - constructor({ formData, resultsData, protocol, parties, allParties }) { + constructor({ formData, resultsData, protocol, parties, protocolType, machineCount }) { if(formData && resultsData) { this.formData = formData; this.resultsData = resultsData; @@ -19,30 +19,40 @@ export default class ValidationFormState { validVotesCount: zeroIfEmpty(protocol.results.validVotesCount), } - this.initResults(protocol, parties, allParties); + this.initResults(protocol, parties, protocolType, machineCount); } } - initResults(protocol, parties, allParties) { + initResults(protocol, parties, protocolType, machineCount) { const emptyStrIfNull = value => (value || value === 0)? value : ''; - this.resultsData = { '0': '' }; + this.resultsData = {}; for(const party of parties) { - if((allParties? true : party.isFeatured) || party.id.toString() === '0') - this.resultsData[party.id] = ''; - this.resultsData[`${party.id}m`] = ''; - this.resultsData[`${party.id}nm`] = ''; + //check if should add paper + if(protocolType === 'paper' || protocolType === 'machine-paper') { + this.resultsData[`party${party.id}paper`] = ''; + } + + //add machines + for(let i = 0; i < machineCount; i++) { + this.resultsData[`party${party.id}machine${i+1}`] = ''; + } } for(const result of protocol.results.results) { - this.resultsData[result.party.id] = emptyStrIfNull(result.validVotesCount); - this.resultsData[`${result.party.id}m`] = emptyStrIfNull(result.machineVotesCount); - this.resultsData[`${result.party.id}nm`] = emptyStrIfNull(result.nonMachineVotesCount); + if(protocolType === 'paper' || protocolType === 'machine-paper') { + this.resultsData[`party${result.party}paper`] = emptyStrIfNull(result.paperBallotsVotes); + } + + for(let i = 0; i < machineCount; i++) { + if(result.machineVotes[i]) + this.resultsData[`party${result.party}machine${i+1}`] = emptyStrIfNull(result.machineVotes[i]); + } } } - getFieldStatus(protocol, parties, allParties, sectionData, protocolType) { + getFieldStatus(protocol, parties, protocolType, machineCount) { const zeroIfEmpty = value => value? value : '';//0; const emptyStrIfNull = value => (value || value === 0)? value : ''; @@ -60,32 +70,39 @@ export default class ValidationFormState { fieldStatus[`sectionId${i+1}`] = { unchanged: true }; } - for(const party of parties) { - if((allParties? true : party.isFeatured) || party.id.toString() === '0') { - - const updateFieldStatus = (apiKey, resultSuffix) => { - let originalResult = ''; - for(const result of protocol.results.results) { - if(result.party.id === party.id) { - originalResult = emptyStrIfNull(result[apiKey]); - } - } - - if(this.resultsData[`${party.id}${resultSuffix}`] === '') - fieldStatus[`party${party.id}${resultSuffix}`] = { invalid: true }; - else if(originalResult.toString() !== this.resultsData[`${party.id}${resultSuffix}`].toString()) - fieldStatus[`party${party.id}${resultSuffix}`] = { changed: true }; - else - fieldStatus[`party${party.id}${resultSuffix}`] = { unchanged: true }; - }; - - updateFieldStatus('validVotesCount', ''); - - if(sectionData.isMachine) { - updateFieldStatus('machineVotesCount', 'm'); - updateFieldStatus('nonMachineVotesCount', 'nm'); + const getPartyResult = partyId => { + for(const result of protocol.results.results) { + if(result.party.id === partyId) { + return result; } } + + return null; + }; + + const compareResult = (key, originalResult) => { + if(!this.resultsData[key] || this.resultsData[key] === '') + return { invalid: true }; + else if(originalResult.toString() !== this.resultsData[key].toString()) + return { changed: true }; + else + return { unchanged: true }; + }; + + for(const party of parties) { + + let result = getPartyResult(party.id); + if(!result) result = { paperBallotsVotes: null, machineVotes: [] }; + + if(protocolType === 'paper' || protocolType === 'paper-machine') { + const originalResult = emptyStrIfNull(result.paperBallotsVotes); + fieldStatus[`party${party.id}paper`] = compareResult(`party${party.id}paper`, originalResult); + } + + for(let i = 0; i < machineCount; i++) { + const originalResult = emptyStrIfNull(result.machineVotes[i]); + fieldStatus[`party${party.id}machine${i+1}`] = compareResult(`party${party.id}machine${i+1}`, originalResult);; + } } const addStatusForResultField = fieldName => { @@ -129,7 +146,8 @@ export default class ValidationFormState { changedFields = true; } - console.log(fieldStatus); + if(protocolType === 'unset') + invalidFields = true; return { fieldStatus, invalidFields, changedFields }; } @@ -161,23 +179,26 @@ export default class ValidationFormState { return false; } - generateResults(isMachine) { - const results = {}; - - Object.keys(this.resultsData).forEach(key => { - if(key[key.length - 2] === 'n') { - let newKey = key.slice(0, key.length - 2); - if(!results[newKey]) results[newKey] = {}; - results[newKey].nonMachineVotesCount = !isMachine? null : parseInt(this.resultsData[key], 10); - } else if(key[key.length - 1] === 'm') { - let newKey = key.slice(0, key.length - 1); - if(!results[newKey]) results[newKey] = {}; - results[newKey].machineVotesCount = !isMachine? null : parseInt(this.resultsData[key], 10); - } else { - if(!results[key]) results[key] = {}; - results[key].validVotesCount = parseInt(this.resultsData[key], 10); + generateResults(parties, protocolType, machineCount) { + const results = []; + + for(const party of parties) { + const result = { + party: party.id, + paperBallotsVotes: 0, + machineVotes: [], } - }); + + if(protocolType === 'paper' || protocolType === 'paper-machine') { + result.paperBallotsVotes = parseInt(this.resultsData[`party${party.id}paper`], 10); + } + + for(let i = 0; i < machineCount; i++) { + result.machineVotes.push(parseInt(this.resultsData[`party${party.id}machine${i+1}`], 10)); + } + + results.push(result); + } return results; }