diff --git a/components/CaseRow.js b/components/CaseRow.js index f031dea..fb59e6e 100644 --- a/components/CaseRow.js +++ b/components/CaseRow.js @@ -1,6 +1,6 @@ -import React, { useState } from "react"; +import React, { useState, useContext, useEffect } from "react"; +import { CaseContext } from "../contexts/casecontroller"; -import chargeContainer from "../static/chargecontainer.json"; import getAnalysis from "../libs/evaluator"; // components @@ -20,21 +20,109 @@ import CardContent from "@material-ui/core/CardContent"; import TextField from "@material-ui/core/TextField"; function CaseRow(props) { - const [showForm, setShowForm] = useState(false); // default to collapsed + const value = useContext(CaseContext); + let charge = props.charges[props.title] || {}; + const [showForm, setShowForm] = useState(true); // Charge properties - const [description, setDescription] = useState(chargeContainer.description); + const [description, setDescription] = useState(charge.description); const [classification, setClassification] = useState( - chargeContainer.classification + charge.classification ); - const [isBRAFelony, setIsBRAFelony] = useState(chargeContainer.isBRAFelony); - const [convicted, setConvicted] = useState(chargeContainer.isConvicted); - const [papered, setPapered] = useState(chargeContainer.isPapered); - const [offense, setOffense] = useState(chargeContainer.offense); - const [chargeDispositionDate, setChargeDispositionDate] = useState( - chargeContainer.dispositionDate + const [isBRAFelony, setIsBRAFelony] = useState(charge.isBRAFelony); + const [convicted, setConvicted] = useState(charge.isConvicted); + const [papered, setPapered] = useState(charge.isPapered); + const [offense, setOffense] = useState(charge.offense); + const [dispositionDate, setDispositionDate] = useState( + charge.dispositionDate ); + useEffect(() => { + if(props.charges && props.charges[props.title]){ + setDescription(props.charges[props.title].description); + setClassification(props.charges[props.title].classification); + setIsBRAFelony(props.charges[props.title].isBRAFelony); + setConvicted(props.charges[props.title].isConvicted); + setPapered(props.charges[props.title].isPapered); + setOffense(props.charges[props.title].offense); + setDispositionDate(props.charges[props.title].dispositionDate); + } + }, [props]); + + const getUpdater = () => { + return { + caseData: { + ...value.caseData, + case: { + ...value.caseData.case, + charges: { + ...value.caseData.case.charges, + }, + }, + client: { + ...value.caseData.client, + } + } + }; + } + + + const persistClassification = classification => { + setClassification(classification); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.classification = classification; + value.updater(update); + } + + const persistDescription = description => { + setDescription(description); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.description = description; + value.updater(update); + } + + const persistIsBRAFelony = isBRAFelony => { + setIsBRAFelony(isBRAFelony); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.isBRAFelony = isBRAFelony; + value.updater(update); + } + + const persistConvicted = isConvicted => { + setConvicted(isConvicted); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.isConvicted = isConvicted; + value.updater(update); + } + + const persistPapered = isPapered => { + setPapered(isPapered); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.isPapered = isPapered; + value.updater(update); + } + + const persistOffense = offense => { + setOffense(offense); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.offense = offense; + value.updater(update); + } + + const persistDispositionDate = dispositionDate => { + setDispositionDate(dispositionDate); + let update = getUpdater(); + let updateCharge = update.caseData.case.charges[props.title]; + updateCharge.dispositionDate = dispositionDate; + value.updater(update); + } + function analysis() { return getAnalysis( convicted, @@ -48,7 +136,7 @@ function CaseRow(props) { return ( setOffense(e.target.value)} + onChange={e => persistOffense(e.target.value)} margin="normal" /> setChargeDispositionDate(e)} + initialDate={dispositionDate} + hoist={e => persistDispositionDate(e)} /> setClassification(e.target.value)} + onChange={e => persistClassification(e.target.value)} margin="normal" > @@ -100,14 +188,14 @@ function CaseRow(props) { autoComplete="off" label="Description" value={description} - onChange={e => setDescription(e.target.value)} + onChange={e => persistDescription(e.target.value)} margin="normal" /> setPapered(e.target.checked)} + onChange={e => persistPapered(e.target.checked)} value="Papered" inputProps={{ "aria-label": "Papered checkbox" }} /> @@ -118,7 +206,7 @@ function CaseRow(props) { control={ setIsBRAFelony(e.target.checked)} + onChange={e => persistIsBRAFelony(e.target.checked)} value="ChargeIsBRAFelony" inputProps={{ "aria-label": "ChargeIsBRAFelony checkbox" }} /> @@ -129,7 +217,7 @@ function CaseRow(props) { control={ setConvicted(e.target.checked)} + onChange={e => persistConvicted(e.target.checked)} value="Convicted" inputProps={{ "aria-label": "Convicted checkbox" }} /> diff --git a/components/CaseTable.js b/components/CaseTable.js index 41b4211..d6eaf7e 100644 --- a/components/CaseTable.js +++ b/components/CaseTable.js @@ -13,6 +13,7 @@ import TextField from "@material-ui/core/TextField"; import { FormControlLabel } from "@material-ui/core"; import ComposedDatePicker from "./ComposedDatePicker.js"; import Switch from "@material-ui/core/Switch"; +import Button from "@material-ui/core/Button"; // components import CaseRow from "./CaseRow"; @@ -49,50 +50,76 @@ function CaseTable(props) { const pushCharge = () => { let chargeNum = Object.keys(value.caseData.case.charges).length + 1; let newChargeKey = `Charge ${chargeNum}`; - persist(newChargeKey, value.chargeFormat); - // save all data, including new charge, - // to update display without changing original json caseContainer + let update = getUpdater(); + update.caseData.case.charges[newChargeKey] = value.chargeFormat; + value.updater(update); }; const persistEvaluatorName = name => { - value.updater({ - caseData: { - ...value.caseData, - evaluatorName: name, - case: { - ...value.caseData.case - }, - client: { - ...value.caseData.client - } - } - }); + setEvaluatorName(name); + let update = getUpdater(); + update.caseData.evaluatorName = name; + value.updater(update); + }; + + const persistEvaluatorComments = comments => { + setComments(comments); + let update = getUpdater(); + update.caseData.evaluatorComments = comments; + value.updater(update); + }; + + const persistTerminationDate = terminationDate => { + setTerminationDate(terminationDate); + let update = getUpdater(); + update.caseData.case.terminationDate = terminationDate; + value.updater(update); }; - const persist = (newChargeKey, newCharge) => { - value.updater({ + const persistClientName = clientName => { + setClientName(clientName); + let update = getUpdater(); + update.caseData.client.name = clientName; + value.updater(update); + } + + const persistOnProbation = isOnProbation => { + setOnProbation(isOnProbation); + let update = getUpdater(); + update.caseData.client.isOnProbation = isOnProbation; + value.updater(update); + } + + const persistPdId = pdId => { + setPdId(pdId); + let update = getUpdater(); + update.caseData.client.pdId = pdId; + value.updater(update); + } + + const getUpdater = () => { + return { caseData: { ...value.caseData, - evaluatorName: evaluatorName, - evaluatorComments: comments, case: { ...value.caseData.case, charges: { ...value.caseData.case.charges, - [newChargeKey]: newCharge }, - terminationDate: terminationDate }, client: { ...value.caseData.client, - dob: birthDate, - isOnProbation: isOnProbation, - name: clientName, - pdId: pdId } } - }); - }; + }; + } + + const persistBirthDate = dob => { + setBirthDate(dob); + let update = getUpdater(); + update.caseData.client.dob = dob; + value.updater(update); + } return ( @@ -104,7 +131,6 @@ function CaseTable(props) { label="Evaluator Name" value={evaluatorName} onChange={e => { - setEvaluatorName(e.target.value); persistEvaluatorName(e.target.value); }} /> @@ -113,7 +139,7 @@ function CaseTable(props) { multiline value={comments} onChange={e => { - setComments(e.target.value); + persistEvaluatorComments(e.target.value); }} /> { - setClientName(e.target.value); + persistClientName(e.target.value); }} /> { - setOnProbation(e.target.checked); + persistOnProbation(e.target.checked); }} inputProps={{ "aria-label": "isOnProbation checkbox" }} /> @@ -141,20 +167,20 @@ function CaseTable(props) { autoComplete="off" value={pdId} onChange={e => { - setPdId(e.target.value); + persistPdId(e.target.value); }} /> { - setBirthDate(e); + persistBirthDate(e); }} initialDate={birthDate} /> { - setTerminationDate(e); + persistTerminationDate(e); }} initialDate={terminationDate} /> @@ -162,12 +188,12 @@ function CaseTable(props) { {/* Cases in Context object */}
- {Object.keys(charges).map((charge, idx) => { + {Object.keys(charges).map((chargeKey, idx) => { return ( ); })} diff --git a/components/Nav.js b/components/Nav.js index 13d0825..a6b0c8c 100644 --- a/components/Nav.js +++ b/components/Nav.js @@ -81,9 +81,19 @@ export default function NavBar() { ); }} - + + {value => { + return ( + + ); + }} +
diff --git a/contexts/casecontroller.js b/contexts/casecontroller.js index 0c05a67..83a6c44 100644 --- a/contexts/casecontroller.js +++ b/contexts/casecontroller.js @@ -48,6 +48,69 @@ class InitializedProvider extends React.Component { }); }; + this.nextPdfLine = y => { + return y + 3; + }; + + this.keyValue = (pdf,key,value,x1,x2,y) => { + pdf.text(key, x1, y); + pdf.text(value, x2, y); + } + + this.saveDataToPDF = () => { + console.log('save to pdf',this.state.caseData); + const jsPDF = require("jspdf"); + const pdf = new jsPDF(); + pdf.setFontSize(8); + // when adding new controller code, it needs to be added to this.state = ... + let x = 10; + let y = 0; + let xColumn2 = 50; + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Evaluator Name",this.state.caseData.evaluatorName,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Evaluator Comments",this.state.caseData.evaluatorComments,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Client Name",this.state.caseData.client.name,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Client Is On Probation",this.state.caseData.client.isOnProbation+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Client PD ID",this.state.caseData.client.pdId,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Client Date Of Birth",this.state.caseData.client.dob+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Client Case Termination Date",this.state.caseData.case.terminationDate+"",x,xColumn2,y); + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + + let charges = this.state.caseData.case.charges; + Object.keys(charges).map(key => { + let charge = charges[key]; + this.keyValue(pdf,"Charge",key,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Offense",charge.offense,x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Disposition Date",charge.dispositionDate+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Classification",charge.classification+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Description",charge.description+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Is Papered",charge.isPapered+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Is BRA Felony",charge.isBRAFelony+"",x,xColumn2,y); + y = this.nextPdfLine(y); + this.keyValue(pdf,"Is Convicted",charge.isConvicted+"",x,xColumn2,y); + y = this.nextPdfLine(y); + y = this.nextPdfLine(y); + }) + pdf.save("data.pdf"); + }; + // General purpose updater -- pass an object get a state update this.updater = stateobj => { this.setState(stateobj, () => { @@ -58,7 +121,8 @@ class InitializedProvider extends React.Component { this.state = { ...caseObj, reset: this.reset, - updater: this.updater + updater: this.updater, + saveDataToPDF: this.saveDataToPDF, }; } diff --git a/package.json b/package.json index d39e76f..f01f078 100644 --- a/package.json +++ b/package.json @@ -7,19 +7,19 @@ "dev": "next dev", "build": "next build", "start": "next start", - "precommit": "lint-staged", "lint": "./node_modules/.bin/eslint ." }, "dependencies": { "@date-io/date-fns": "^1.3.11", "@material-ui/core": "^4.4.2", "@material-ui/icons": "^4.4.1", - "babel-eslint": "^10.0.3", "@material-ui/pickers": "^3.2.7", + "babel-eslint": "^10.0.3", "babel-polyfill": "^6.26.0", "date-fns": "^2.0.0-beta.5", "draft-js": "^0.11.2", - "next": "^9.0.6", + "jspdf": "^1.5.3", + "next": "^9.2.3-canary.10", "react": "^16.12.0", "react-dom": "^16.12.0", "xstate": "^4.6.7" diff --git a/static/casecontainersample.json b/static/casecontainersample.json new file mode 100644 index 0000000..96358a7 --- /dev/null +++ b/static/casecontainersample.json @@ -0,0 +1,26 @@ +{ + "evaluatorName": "sample name", + "evaluatorComments": "sample comments", + "client": { + "dob": "02/04/1990", + "isOnProbation": true, + "name": "sample client", + "pdId": "sample client pdid" + }, + "case": { + "id": "sample case id", + "isConvicted": true, + "terminationDate": "03/12/2005", + "charges": { + "Charge 01" : { + "classification": "felony", + "dispositionDate": "04/09/2010", + "description": "sample description", + "isBRAFelony": true, + "isConvicted": true, + "isPapered": true, + "offense": "sample offense" + } + } + } +} diff --git a/static/chargecontainersample.json b/static/chargecontainersample.json new file mode 100644 index 0000000..3da05ed --- /dev/null +++ b/static/chargecontainersample.json @@ -0,0 +1,11 @@ +{ + "indicatorColor": "grey", + "analysisMessage": "", + "classification": "Felony", + "dispositionDate": "04/09/2010", + "description": "sample description", + "isBRAFelony": true, + "isConvicted": true, + "isPapered": true, + "offense": "sample offense" +}