|
| 1 | +/* eslint-disable no-alert */ |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import { LoadingSpinner } from '@labkey/components' |
| 5 | +import './styles/birthRecordReport.scss' |
| 6 | +import { Button } from 'react-bootstrap' |
| 7 | +import BirthRecordState from './constants/BirthRecordState' |
| 8 | +import AnimalSelectionPanel from './components/AnimalSelectionPanel' |
| 9 | +import fetchNewAnimalData from './api/fetchNewAnimalData' |
| 10 | +import SummaryGridPanel from './components/SummaryGridPanel' |
| 11 | +import { getReportPath } from './services/printToPDF' |
| 12 | +import InfoPanel from './components/InfoPanel' |
| 13 | + |
| 14 | +export default class BirthRecordReport extends React.Component { |
| 15 | + state = new BirthRecordState(); |
| 16 | + |
| 17 | + componentDidMount() { |
| 18 | + Promise.resolve(this.loadLists()).catch = error => { |
| 19 | + console.log(`Error in componentDidMount: ${error}`) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + loadLists() { |
| 24 | + let animalList = [] |
| 25 | + |
| 26 | + return new Promise(() => { |
| 27 | + ( |
| 28 | + fetchNewAnimalData().then(list => { |
| 29 | + animalList = list |
| 30 | + }) |
| 31 | + ).then(() => { |
| 32 | + this.setState(prevState => ( |
| 33 | + { |
| 34 | + ...prevState, |
| 35 | + isLoading: false, |
| 36 | + animalList |
| 37 | + } |
| 38 | + )) |
| 39 | + }).catch(error => { |
| 40 | + console.log(`Error in loadLists: ${error}`) |
| 41 | + }) |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + // quit |
| 46 | + onQuitClick = () => { |
| 47 | + window.history.back() |
| 48 | + } |
| 49 | + |
| 50 | + onClickPrint = () => { |
| 51 | + if (!this.state.selectedAnimal) { |
| 52 | + this.setState(prevState => ( |
| 53 | + { |
| 54 | + ...prevState, |
| 55 | + errorMessage: 'Select an animal before clicking print.' |
| 56 | + })) |
| 57 | + } else { |
| 58 | + const reportPath = getReportPath('BirthRecord') |
| 59 | + const fullPath = `${reportPath}&rc:Parameters=Collapsed&TargetID=${this.state.selectedAnimal.Id}` |
| 60 | + const left = window.screenX + 20 |
| 61 | + |
| 62 | + window.open(fullPath, '_blank', `location=yes,height=850,width=768,status=yes, left=${left}`) |
| 63 | + |
| 64 | + // add animal to the summary list if it is not there |
| 65 | + if (!this.state.summaryData.find(o => o.Id === this.state.selectedAnimal.Id)) { |
| 66 | + this.setState(prevState => ( |
| 67 | + { |
| 68 | + ...prevState, |
| 69 | + summaryData: [ |
| 70 | + ...prevState.summaryData, |
| 71 | + { ...prevState.selectedAnimal } |
| 72 | + ] |
| 73 | + })) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + handleChange = option => { |
| 79 | + this.setState(prevState => ( |
| 80 | + { |
| 81 | + ...prevState, |
| 82 | + errorMessage: undefined, |
| 83 | + selectedAnimal: option |
| 84 | + } |
| 85 | + )) |
| 86 | + } |
| 87 | + |
| 88 | + render() { |
| 89 | + const { isLoading } = this.state |
| 90 | + |
| 91 | + if (isLoading) { |
| 92 | + return ( |
| 93 | + <LoadingSpinner msg="Loading app..." /> |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <div> |
| 99 | + <div className="split-panel"> |
| 100 | + <div className="parent-panel"> |
| 101 | + <div className="panel-heading"> |
| 102 | + <p>Select Animal</p> |
| 103 | + </div> |
| 104 | + <div className="wizard-panel"> |
| 105 | + <AnimalSelectionPanel |
| 106 | + animalList={ this.state.animalList } |
| 107 | + handleChange={ this.handleChange } |
| 108 | + selectedAnimal={ this.state.selectedAnimal } |
| 109 | + /> |
| 110 | + <InfoPanel |
| 111 | + errorMessages={ this.state.errorMessage |
| 112 | + && [{ propTest: true, colName: this.state.errorMessage }] } |
| 113 | + infoMessages={ [{ key: 1, value: 'Select an animal and press the print button to generate the Birth Record Report.' }] } |
| 114 | + /> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + <div className="right-panel"> |
| 118 | + <div className="panel-heading"> |
| 119 | + <p>Reports Printed</p> |
| 120 | + </div> |
| 121 | + <div className="wizard-right"> |
| 122 | + <SummaryGridPanel |
| 123 | + summaryData={ this.state.summaryData } |
| 124 | + /> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + |
| 129 | + <div className="wizard-footer"> |
| 130 | + <Button bsStyle="primary" onClick={ this.onClickPrint }>Print Birth Record <i aria-hidden="true" className="fa fa-print" /></Button> |
| 131 | + <Button onClick={ this.onQuitClick }>Quit</Button> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
0 commit comments