Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from 'react';
  2. import {
  3.   Input,
  4.   Button,
  5.   Radio,
  6.   Checkbox,
  7.   Row,
  8.   Col,
  9.   Alert,
  10.   Popover,
  11.   Typography,
  12.   Divider,
  13.   DatePicker,
  14.   InputNumber,
  15. } from 'antd';
  16. import { withFormik, Form, Field, FieldArray } from 'formik';
  17. import * as Yup from 'yup';
  18. import moment from 'moment';
  19. import PropTypes from 'prop-types';
  20. import '../assets/styles/default.less';
  21. import history from '../history';
  22. import { laudoDescritivo, microrganismo, conclusao } from '../assets/initialValues';
  23. import { persistUserPropertie, getUserPropertie, getUserData } from '../controllers/user';
  24.  
  25. // eslint-disable-next-line no-unused-vars
  26. const initialValueSs = [
  27.   'Esfregaço satisfatório para avaliação.',
  28.   'Esfregaço parcialmente limitado pela presença de pouca celularidade.',
  29.   'Esfregaço parcialmente obscurecido pela presença e sobreposição de numerosas hemácias.',
  30.   'Esfregaço parcialmente obscurecido pela concentração e sobreposição de numerosos polimorfonucleados.',
  31. ];
  32.  
  33. // TODO
  34. const userID = 'Alexandre Campos';
  35.  
  36. const dateFormat = 'DD/MM/YYYY';
  37. const ReportForm = ({ values, errors, touched, handleChange, setFieldValue }) => {
  38.   const [sampleSuitabilities, setSampleSuitabilities] = useState([]);
  39.   const [descReportsVals, setDescReportsVals] = useState([]);
  40.   const [microorganismsVals, setMicroorganismsVals] = useState([]);
  41.   const [conclusionsVals, setConclusionsVals] = useState([]);
  42.  
  43.   useEffect(() => {
  44.     const getUserDataValues = async () => {
  45.       const userData = await getUserData(userID);
  46.       if (userData) {
  47.         if (userData.sampleSuitabilities && userData.sampleSuitabilities.length >= 1) {
  48.           console.log(userData);
  49.           setSampleSuitabilities(userData.sampleSuitabilities);
  50.         }
  51.         if (userData.descriptiveReports) {
  52.           setDescReportsVals(userData.descriptiveReports);
  53.         }
  54.         // console.log(userData);
  55.       }
  56.     };
  57.  
  58.     getUserDataValues();
  59.   }, []);
  60.  
  61.   useEffect(() => {
  62.     const persistSampleSuitabilities = async () => {
  63.       await persistUserPropertie(userID, 'sampleSuitabilities', sampleSuitabilities, true);
  64.     };
  65.     if (sampleSuitabilities && sampleSuitabilities.length >= 1) {
  66.       persistSampleSuitabilities();
  67.     }
  68.   }, [sampleSuitabilities]);
  69.  
  70.   useEffect(() => {
  71.     const persistDescriptiveReports = async () => {
  72.       await persistUserPropertie(userID, 'descriptiveReports', descReportsVals, true);
  73.     };
  74.     if (descReportsVals.length >= 1) {
  75.       persistDescriptiveReports();
  76.     }
  77.   }, [descReportsVals]);
  78.  
  79.   useEffect(() => {
  80.     const persistMicroorganisms = async () => {
  81.       await persistUserPropertie(userID, 'microorganisms', microorganismsVals, true);
  82.     };
  83.     if (microorganismsVals.length >= 1) {
  84.       persistMicroorganisms();
  85.     }
  86.   }, [microorganismsVals]);
  87.  
  88.   useEffect(() => {
  89.     const persistConclusions = async () => {
  90.       await persistUserPropertie(userID, 'conclusions', conclusionsVals, true);
  91.     };
  92.     if (conclusionsVals >= 1) {
  93.       persistConclusions();
  94.     }
  95.   }, [conclusionsVals]);
  96.  
  97.   const handleEnter = event => {
  98.     switch (event.target.name) {
  99.       case 'sampleSuitability':
  100.         setSampleSuitabilities([...sampleSuitabilities, event.target.value]);
  101.         break;
  102.       case 'microorganisms':
  103.         setMicroorganismsVals([...microorganismsVals, event.target.value]);
  104.         break;
  105.       case 'descReports':
  106.         setDescReportsVals([...descReportsVals, event.target.value]);
  107.         break;
  108.       case 'conclusions':
  109.         setConclusionsVals([...conclusionsVals, event.target.value]);
  110.         break;
  111.       default:
  112.         break;
  113.     }
  114.     event.target.value = '';
  115.     console.log(document.getElementById('sampleSuitabilityInput'));
  116.   };
  117.  
  118.   const removeItem = async (array, index) => {
  119.     // eslint-disable-next-line no-eval
  120.     const newArray = [...eval(array)];
  121.     newArray.splice(index, 1);
  122.     switch (array) {
  123.       case 'sampleSuitabilities':
  124.         setSampleSuitabilities([...newArray]);
  125.         break;
  126.       case 'conclusions':
  127.         setConclusionsVals([...newArray]);
  128.         break;
  129.       case 'descReports':
  130.         setDescReportsVals([...newArray]);
  131.         break;
  132.       case 'microorganisms':
  133.         setMicroorganismsVals([...newArray]);
  134.         break;
  135.  
  136.       default:
  137.         break;
  138.     }
  139.   };
  140.  
  141.   const onDateChange = (date, dateString) => {
  142.     setFieldValue('received', dateString);
  143.   };
  144.  
  145.   const onAgeChange = age => {
  146.     setFieldValue('patientAge', age);
  147.   };
  148.  
  149.   return (
  150.     <Form className="frame">
  151.       {/* Beginning of Patient Section */}
  152.       <Typography.Title className="center" level={4}>
  153.         Paciente
  154.       </Typography.Title>
  155.       <label htmlFor="patientName">
  156.         Nome do Paciente
  157.         <Input
  158.           allowClear
  159.           type="name"
  160.           onChange={handleChange}
  161.           value={values.patientName}
  162.           name="patientName"
  163.           placeholder="Nome do Paciente"
  164.           style={{ marginBottom: '20px' }}
  165.         />
  166.       </label>
  167.       <div style={{ marginBottom: '15px' }}>
  168.         <label htmlFor="patientAge">
  169.           {`Idade do Paciente `}
  170.           <InputNumber
  171.             type="number"
  172.             onChange={onAgeChange}
  173.             value={values.patientAge}
  174.             name="patientAge"
  175.           />
  176.         </label>
  177.       </div>
  178.       <label htmlFor="received">
  179.         {`Recebido Em: `}
  180.         <DatePicker
  181.           onChange={onDateChange}
  182.           value={values.received !== '' ? moment(values.received, dateFormat) : null}
  183.           format={dateFormat}
  184.         />
  185.       </label>
  186.       <label htmlFor="released">
  187.         {`Liberado Em: `}
  188.         <DatePicker
  189.           onChange={onDateChange}
  190.           value={values.released !== '' ? moment(values.released, dateFormat) : null}
  191.           format={dateFormat}
  192.         />
  193.       </label>
  194.       <Divider />
  195.       {/* Ending of Patient Section */}
  196.       {/* Beginning of Adequabilidade da amostra */}
  197.       <Typography.Title className="center" level={4}>
  198.         Adequabilidade da Amostra
  199.       </Typography.Title>
  200.       <FieldArray
  201.         name="sampleSuitabilities"
  202.         render={() => (
  203.           <Row type="flex" justify="space-between">
  204.             {sampleSuitabilities && sampleSuitabilities.length > 0 ? (
  205.               <Radio.Group
  206.                 name="sampleSuitability"
  207.                 value={values.sampleSuitability}
  208.                 onChange={val => {
  209.                   values.sampleSuitability = val;
  210.                 }}
  211.               >
  212.                 {sampleSuitabilities.map((sampleSuitability, index) => (
  213.                   <Col key={sampleSuitability}>
  214.                     <Field
  215.                       type="radio"
  216.                       id={sampleSuitability}
  217.                       value={sampleSuitability}
  218.                       label={sampleSuitability}
  219.                       render={({ field }) => (
  220.                         <Popover
  221.                           placement="topRight"
  222.                           content={
  223.                             <Button
  224.                               icon="delete"
  225.                               type="danger"
  226.                               onClick={() => removeItem('sampleSuitabilities', index)}
  227.                             />
  228.                           }
  229.                         >
  230.                           <Radio {...field}>{sampleSuitability}</Radio>
  231.                         </Popover>
  232.                       )}
  233.                     />
  234.                   </Col>
  235.                 ))}
  236.               </Radio.Group>
  237.             ) : (
  238.               <Alert
  239.                 message="Você ainda não tem nenhuma Adequabilidade da Amostra adicionada. Preencha o campo de texto abaixo para que o nome do mesmo fique salvo!"
  240.                 type="info"
  241.               />
  242.             )}
  243.           </Row>
  244.         )}
  245.       />
  246.       <Input
  247.         allowClear
  248.         // TODO Fix clean
  249.         onPointerMove={e => (e.target.value = '')}
  250.         id="sampleSuitabilityInput"
  251.         type="name"
  252.         name="sampleSuitability"
  253.         placeholder="Adequabilidade da Amostra. Pressione Enter para Adicionar"
  254.         onPressEnter={handleEnter}
  255.         onKeyDown={e => (e.keyCode === 13 ? e.preventDefault() : '')}
  256.         style={{ marginTop: '15px' }}
  257.       />
  258.       <Divider />
  259.       {/* Ending of Adequabilidade da amostra */}
  260.       {/* Beginning epitheliumSamples */}
  261.       <Typography.Title className="center" level={4}>
  262.         Epitélios Representados na Amostra
  263.       </Typography.Title>
  264.       <Checkbox.Group
  265.         name="epitheliumSamples"
  266.         defaultValue={values.epitheliumSamples}
  267.         onChange={val => {
  268.           values.epitheliumSamples = val;
  269.         }}
  270.         style={{
  271.           display: 'flex',
  272.           alignItems: 'center',
  273.           justifyContent: 'center',
  274.         }}
  275.       >
  276.         <div style={{ display: 'grid', gridColumnGap: '20px' }}>
  277.           <div style={{ display: 'grid', gridColumnStart: '1' }}>
  278.             <b>Escamoso</b>
  279.             <Checkbox value="escamoso (superficial)">Superficial</Checkbox>
  280.             <Checkbox value="escamoso (intermediário)">Intermediário</Checkbox>
  281.             <Checkbox value="escamoso (basal)">Basal</Checkbox>
  282.           </div>
  283.           <div style={{ display: 'grid', gridColumnStart: '2' }}>
  284.             <b>Metaplásico</b>
  285.             <Checkbox value="metaplásico maduro">Maduro</Checkbox>
  286.             <Checkbox value="metaplásico imaturo">Imaturo</Checkbox>
  287.           </div>
  288.           <div style={{ display: 'grid', gridColumnStart: '3' }}>
  289.             <b>Glandular</b>
  290.             <Checkbox value="glandular endocervical">Endocervical</Checkbox>
  291.             <Checkbox value="glandular endometrial">Endometrial</Checkbox>
  292.           </div>
  293.         </div>
  294.       </Checkbox.Group>
  295.       <Divider />
  296.       {/* Ending of epitheliumSamples */}
  297.       <Typography.Title className="center" level={4}>
  298.         Laudo Descritivo
  299.       </Typography.Title>
  300.       <FieldArray
  301.         name="descriptiveReports"
  302.         render={() => (
  303.           <Row type="flex" justify="space-between">
  304.             {descReportsVals && descReportsVals.length > 0 ? (
  305.               <Checkbox.Group
  306.                 name="descriptiveReport"
  307.                 value={values.descriptiveReports}
  308.                 onChange={val => {
  309.                   values.descriptiveReports = val;
  310.                 }}
  311.               >
  312.                 {descReportsVals.map((descReport, index) => (
  313.                   <Col key={descReport}>
  314.                     <Field
  315.                       type="checkbox"
  316.                       id={descReport}
  317.                       value={descReport}
  318.                       label={descReport}
  319.                       render={({ field }) => (
  320.                         <Popover
  321.                           placement="topRight"
  322.                           content={
  323.                             <Button
  324.                               icon="delete"
  325.                               type="danger"
  326.                               onClick={() => removeItem('descReports', index)}
  327.                             />
  328.                           }
  329.                         >
  330.                           <Checkbox {...field}>{descReport}</Checkbox>
  331.                         </Popover>
  332.                       )}
  333.                     />
  334.                   </Col>
  335.                 ))}
  336.               </Checkbox.Group>
  337.             ) : (
  338.               <Alert
  339.                 message="Você ainda não tem nenhum Laudo Descritivo adicionado. Preencha o campo de texto abaixo para que o nome do mesmo fique salvo!"
  340.                 type="info"
  341.               />
  342.             )}
  343.           </Row>
  344.         )}
  345.       />
  346.       <Input
  347.         allowClear
  348.         // TODO Fix clean
  349.         onPointerMove={e => (e.target.value = '')}
  350.         id="descReportInput"
  351.         type="name"
  352.         name="descReports"
  353.         placeholder="Laudo Descritivo. Pressione Enter para Adicionar"
  354.         onPressEnter={handleEnter}
  355.         onKeyDown={e => (e.keyCode === 13 ? e.preventDefault() : '')}
  356.         style={{ marginTop: '15px' }}
  357.       />
  358.       <Divider />
  359.       <Typography.Title className="center" level={4}>
  360.         Microrganismo
  361.       </Typography.Title>
  362.       <FieldArray
  363.         name="microorganisms"
  364.         render={() => (
  365.           <Row type="flex" justify="space-between">
  366.             {microorganismsVals && microorganismsVals.length > 0 ? (
  367.               <Checkbox.Group
  368.                 name="microorganism"
  369.                 value={values.microorganisms}
  370.                 onChange={val => {
  371.                   values.microorganisms = val;
  372.                 }}
  373.               >
  374.                 {microorganismsVals.map((microorganism, index) => (
  375.                   <Col key={microorganism}>
  376.                     <Field
  377.                       type="checkbox"
  378.                       id={microorganism}
  379.                       value={microorganism}
  380.                       label={microorganism}
  381.                       render={({ field }) => (
  382.                         <Popover
  383.                           placement="topRight"
  384.                           content={
  385.                             <Button
  386.                               icon="delete"
  387.                               type="danger"
  388.                               onClick={() => removeItem('microorganisms', index)}
  389.                             />
  390.                           }
  391.                         >
  392.                           <Checkbox {...field}>{microorganism}</Checkbox>
  393.                         </Popover>
  394.                       )}
  395.                     />
  396.                   </Col>
  397.                 ))}
  398.               </Checkbox.Group>
  399.             ) : (
  400.               <Alert
  401.                 message="Você ainda não tem nenhum Microrganismo adicionado. Preencha o campo de texto abaixo para que o nome do mesmo fique salvo!"
  402.                 type="info"
  403.               />
  404.             )}
  405.           </Row>
  406.         )}
  407.       />
  408.       <Input
  409.         allowClear
  410.         // TODO Fix clean
  411.         onPointerMove={e => (e.target.value = '')}
  412.         id="microorganismInput"
  413.         type="name"
  414.         name="microorganisms"
  415.         placeholder="Microrganismos. Pressione Enter para Adicionar"
  416.         onPressEnter={handleEnter}
  417.         onKeyDown={e => (e.keyCode === 13 ? e.preventDefault() : '')}
  418.         style={{ marginTop: '15px' }}
  419.       />
  420.       <Divider />
  421.       <Typography.Title className="center" level={4}>
  422.         Conclusão
  423.       </Typography.Title>
  424.       <FieldArray
  425.         name="conclusions"
  426.         render={() => (
  427.           <Row type="flex" justify="space-between">
  428.             {conclusionsVals && conclusionsVals.length > 0 ? (
  429.               <Checkbox.Group
  430.                 name="conclusion"
  431.                 value={values.conclusions}
  432.                 onChange={val => {
  433.                   values.conclusions = val;
  434.                 }}
  435.               >
  436.                 {conclusionsVals.map((conclusion, index) => (
  437.                   <Col key={conclusion}>
  438.                     <Field
  439.                       type="checkbox"
  440.                       id={conclusion}
  441.                       value={conclusion}
  442.                       label={conclusion}
  443.                       render={({ field }) => (
  444.                         <Popover
  445.                           placement="topRight"
  446.                           content={
  447.                             <Button
  448.                               icon="delete"
  449.                               type="danger"
  450.                               onClick={() => removeItem('conclusions', index)}
  451.                             />
  452.                           }
  453.                         >
  454.                           <Checkbox {...field}>{conclusion}</Checkbox>
  455.                         </Popover>
  456.                       )}
  457.                     />
  458.                   </Col>
  459.                 ))}
  460.               </Checkbox.Group>
  461.             ) : (
  462.               <Alert
  463.                 message="Você ainda não tem nenhuma Conclusão adicionada. Preencha o campo de texto abaixo para que o nome do mesmo fique salvo!"
  464.                 type="info"
  465.               />
  466.             )}
  467.           </Row>
  468.         )}
  469.       />
  470.       <Input
  471.         allowClear
  472.         // TODO Fix clean
  473.         // onPointerMove={e => (e.target.value = '')}
  474.         id="conclusionInput"
  475.         type="name"
  476.         name="conclusions"
  477.         placeholder="Conclusões. Pressione Enter para Adicionar"
  478.         onPressEnter={handleEnter}
  479.         onKeyDown={e => (e.keyCode === 13 ? e.preventDefault() : '')}
  480.         style={{ marginTop: '15px' }}
  481.       />
  482.       <Button icon="file-pdf" htmlType="submit" type="primary">
  483.         Gerar Laudo
  484.       </Button>
  485.     </Form>
  486.   );
  487. };
  488.  
  489. const FormikForm = withFormik({
  490.   mapPropsToValues({
  491.     patientName,
  492.     patientAge,
  493.     received,
  494.     released,
  495.     sampleSuitability,
  496.     epitheliumSamples,
  497.     descriptiveReports,
  498.     microorganisms,
  499.     conclusions,
  500.   }) {
  501.     return {
  502.       patientName: '' || patientName,
  503.       patientAge: '' || patientAge,
  504.       // TODO set date from edit
  505.       received: '' || moment(received),
  506.       released: '' || moment(released),
  507.       sampleSuitability: '' || sampleSuitability,
  508.       epitheliumSamples: [] || [epitheliumSamples],
  509.       descriptiveReports:
  510.         [
  511.           'Negativo para lesão intra-epitelial ou malignidade (NILM).',
  512.           'Alterações celulares reativas associadas à inflamação.',
  513.         ] || descriptiveReports,
  514.       microorganisms: [] || microorganisms,
  515.       conclusions:
  516.         [
  517.           `O atual estudo citológico não evidencia sinais de lesão intra-epitelial ou malignidade.`,
  518.         ] || conclusions,
  519.     };
  520.   },
  521.   validationSchema: Yup.object().shape({}),
  522.   handleSubmit(values, { resetForm }) {
  523.     console.log('values', values);
  524.     let citoForm = { ...values };
  525.     if (moment.isMoment(citoForm.received)) {
  526.       citoForm = { ...citoForm, received: citoForm.received.format(dateFormat) };
  527.     }
  528.     if (moment.isMoment(citoForm.released)) {
  529.       citoForm = { ...citoForm, released: citoForm.released.format(dateFormat) };
  530.     }
  531.     citoForm = { ...citoForm, patientAge: citoForm.patientAge.toString() };
  532.  
  533.     if (citoForm.epitheliumSamples.length) {
  534.       let escamoso = `escamoso (`;
  535.       const escamosos = [];
  536.       let aux = ``;
  537.  
  538.       citoForm.epitheliumSamples.forEach((elem, index) => {
  539.         if (elem.startsWith('escamoso')) {
  540.           escamosos.push(elem);
  541.         }
  542.       });
  543.       citoForm.epitheliumSamples.forEach((elem, index) => {
  544.         if (elem.startsWith('escamoso')) {
  545.           citoForm.epitheliumSamples.splice(index, 1);
  546.         }
  547.       });
  548.       escamosos.forEach((elem, index) => {
  549.         if (escamosos.length === 1) {
  550.           escamoso = elem;
  551.         } else if (index + 1 === escamosos.length) {
  552.           escamoso += `e ${elem.match(/\((.*)\)/)[1]})`;
  553.         } else if (escamosos.length === 2 || index === escamosos.length - 2) {
  554.           escamoso += `${elem.match(/\((.*)\)/)[1]} `;
  555.         } else {
  556.           escamoso += `${elem.match(/\((.*)\)/)[1]}, `;
  557.         }
  558.       });
  559.  
  560.       if (citoForm.epitheliumSamples[0]) {
  561.         if (citoForm.epitheliumSamples[0].startsWith('escamoso')) {
  562.           citoForm.epitheliumSamples.splice(0, 1, escamoso);
  563.         }
  564.       } else {
  565.         citoForm.epitheliumSamples.splice(0, 0, escamoso);
  566.       }
  567.       citoForm.epitheliumSamples.forEach((elem, index) => {
  568.         if (citoForm.epitheliumSamples.length === 1) {
  569.           aux = elem;
  570.         } else if (index + 1 === citoForm.epitheliumSamples.length) {
  571.           aux += `e ${elem}.`;
  572.         } else if (
  573.           citoForm.epitheliumSamples.length === 2 ||
  574.           index === citoForm.epitheliumSamples.length - 2
  575.         ) {
  576.           aux += `${elem} `;
  577.         } else {
  578.           aux += `${elem}, `;
  579.         }
  580.       });
  581.       citoForm.epitheliumSamples = aux;
  582.       aux = ``;
  583.       citoForm.microorganisms.forEach((elem, index) => {
  584.         if (citoForm.microorganisms.length === 1) {
  585.           aux = elem;
  586.         } else if (index + 1 === citoForm.microorganisms.length) {
  587.           aux += `e ${elem}.`;
  588.         } else if (
  589.           citoForm.microorganisms.length === 2 ||
  590.           index === citoForm.microorganisms.length - 2
  591.         ) {
  592.           aux += `${elem} `;
  593.         } else {
  594.           aux += `${elem}, `;
  595.         }
  596.       });
  597.       citoForm.microorganisms = aux;
  598.  
  599.       console.log('citoForm', citoForm);
  600.       resetForm();
  601.       if (citoForm) {
  602.         history.push('/ct', citoForm);
  603.       }
  604.     }
  605.   },
  606. })(ReportForm);
  607.  
  608. ReportForm.propTypes = {
  609.   values: PropTypes.object,
  610.   errors: PropTypes.object,
  611.   touched: PropTypes.object,
  612.   handleChange: PropTypes.func,
  613.   setFieldValue: PropTypes.func,
  614. };
  615. ReportForm.defaultProps = {
  616.   values: '',
  617.   errors: '',
  618.   touched: '',
  619.   handleChange: '',
  620.   setFieldValue: '',
  621. };
  622.  
  623. export default FormikForm;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement