Advertisement
hilmawanyr

Untitled

Feb 12th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from "react";
  2. import { Link, useParams } from "react-router-dom";
  3. import { connect } from "react-redux";
  4. import { rejectItem, approveItem } from "../store/actions/submissionAction";
  5.  
  6. import HeaderLayout from "./HeaderLayout";
  7. import Section from "../../../common/Section";
  8. import Master from "../../Master";
  9. import Datatable from "../../../common/table/Datatable";
  10. import Modal from "../../../common/Modal";
  11. import { TextArea } from "../../../common/FormGroup";
  12.  
  13. const SubmissionDetail = props => {
  14.   // get param from url
  15.   let { refnumber } = useParams();
  16.   const param = refnumber;
  17.  
  18.   // get submission detail from redux store
  19.   const getDetailSubmission = props.detailSubmission.find(submission => {
  20.     return submission.refnumber === param;
  21.   });
  22.  
  23.   // prepare hook to dispatch redux store to local state
  24.   const [submission, setSubmission] = useState([]);
  25.  
  26.   // add action button to item object if login as BAU
  27.   const prepareTableContent = getDetailSubmission.items.map((items, i) => {
  28.     let itemClone = { ...items };
  29.     if (props.usedByProcurementChecking) {
  30.       let id = i + 1;
  31.       itemClone.action = (
  32.         <>
  33.           <span data-toggle="tooltip" title="tolak barang">
  34.             <button
  35.               className="btn btn-danger"
  36.               data-toggle="modal"
  37.               data-target="#rejectModal"
  38.               onClick={() => setRejectItem(id)}
  39.             >
  40.               <i className="fa fa-ban"></i>
  41.             </button>
  42.           </span>
  43.           <button
  44.             className="btn btn-success"
  45.             data-toggle="tooltip"
  46.             title="setujui barang"
  47.             style={{ marginLeft: "3px" }}
  48.             onClick={() => proveItem(id)}
  49.           >
  50.             <i className="fa fa-check"></i>
  51.           </button>
  52.         </>
  53.       );
  54.     }
  55.     return itemClone;
  56.   });
  57.  
  58.   // set submission object after adding action button and set to state
  59.   useEffect(() => {
  60.     // adjust object to store to the table
  61.     const serveTableContent = prepareTableContent.map((items, i) => {
  62.       return {
  63.         no: i + 1,
  64.         type: items.type,
  65.         item: items.item,
  66.         qty: items.qty,
  67.         note: items.note,
  68.         isApprove: items.isApprove,
  69.         action: items.action
  70.       };
  71.     });
  72.     setSubmission(serveTableContent);
  73.   }, [getDetailSubmission]);
  74.  
  75.   // handle approve item
  76.   const proveItem = id => {
  77.     console.log(submission);
  78.     let findItem = submission.find(item => {
  79.       return item.no === id;
  80.     });
  81.     findItem.isApprove = 1;
  82.  
  83.     let approveSelectedItem = getDetailSubmission.items.find(item => {
  84.       return item.item === id;
  85.     });
  86.     approveSelectedItem.isApprove = 1;
  87.     props.approveSubmissionItem(approveSelectedItem);
  88.     // set local state
  89.     setSubmission(submission);
  90.   };
  91.  
  92.   // provide state to be identifier when give action
  93.   const [rejectedItem, setRejectedItem] = useState("");
  94.  
  95.   // handle reject item
  96.   const submitRejectReason = async () => {
  97.     if (
  98.       window.confirm(
  99.         "Proses ini akan menghapus item pada pengajuan, lanjutkan?"
  100.       )
  101.     ) {
  102.       let findItem = submission.items.find(item => {
  103.         return item.item === rejectedItem;
  104.       });
  105.       findItem.isApprove = 2;
  106.       findItem.approvalNote = document.getElementById("reason").value;
  107.       // dispatch to store
  108.       await props.rejectSubmissionItem(submission);
  109.       // update local state
  110.       setSubmission(getDetailSubmission);
  111.       // hide modal after submit
  112.       const hideModal = () => {
  113.         document
  114.           .getElementById("rejectModal")
  115.           .setAttribute("class", "modal fade");
  116.  
  117.         document
  118.           .getElementsByClassName("modal-backdrop")[0]
  119.           .setAttribute("class", "modal-backdrop fade");
  120.       };
  121.       hideModal();
  122.     }
  123.   };
  124.  
  125.   // set state to identify which item would be edited
  126.   const setRejectItem = itemIdentifier => {
  127.     setRejectedItem(itemIdentifier);
  128.   };
  129.  
  130.   // set table header
  131.   let tableHeader = [
  132.     {
  133.       column0: "No",
  134.       column2: "Tipe",
  135.       column1: "Nama Barang",
  136.       column3: "QTY",
  137.       column4: "Catatan"
  138.     }
  139.   ];
  140.  
  141.   // will show action button if login by BAU
  142.   if (props.usedByProcurementChecking) {
  143.     tableHeader.map(header => (header.column5 = "Aksi"));
  144.   }
  145.  
  146.   const refNumber = [
  147.     {
  148.       forAttr: "refnumber",
  149.       lableName: "Nomor referensi",
  150.       inputAttr: {
  151.         id: "refnumber",
  152.         type: "text",
  153.         value: getDetailSubmission.refnumber,
  154.         className: "form-control",
  155.         name: "refnumber",
  156.         readOnly: "readonly"
  157.       }
  158.     }
  159.   ];
  160.  
  161.   const submissionDate = [
  162.     {
  163.       forAttr: "date",
  164.       lableName: "Tanggal",
  165.       inputAttr: {
  166.         type: "text",
  167.         value: getDetailSubmission.date,
  168.         className: "form-control",
  169.         name: "date",
  170.         readOnly: true
  171.       }
  172.     }
  173.   ];
  174.  
  175.   const subjectForm = [
  176.     {
  177.       forAttr: "subject",
  178.       lableName: "Perihal",
  179.       inputAttr: {
  180.         id: "subject",
  181.         type: "text",
  182.         value: getDetailSubmission.subject,
  183.         className: "form-control",
  184.         name: "subject",
  185.         readOnly: true
  186.       }
  187.     }
  188.   ];
  189.  
  190.   const recipient = [
  191.     {
  192.       forAttr: "to",
  193.       lableName: "Kepada",
  194.       inputAttr: {
  195.         id: "to",
  196.         type: "text",
  197.         value: getDetailSubmission.to,
  198.         className: "form-control",
  199.         name: "to",
  200.         readOnly: true
  201.       }
  202.     }
  203.   ];
  204.  
  205.   const sender = [
  206.     {
  207.       forAttr: "from",
  208.       lableName: "Dari",
  209.       inputAttr: {
  210.         id: "from",
  211.         type: "text",
  212.         value: getDetailSubmission.from,
  213.         className: "form-control",
  214.         name: "from",
  215.         readOnly: true
  216.       }
  217.     }
  218.   ];
  219.  
  220.   let navButton = (
  221.     <>
  222.       <Link className="btn btn-warning" to="/submission">
  223.         Kembali
  224.       </Link>
  225.       {/* button will different depend on who is login */}
  226.       {props.usedByProcurementChecking ? (
  227.         <Link
  228.           className="btn btn-success pull-right"
  229.           to={`/procurement/${refnumber}/submission/create_procurement`}
  230.         >
  231.           Buat Pengadaan
  232.         </Link>
  233.       ) : (
  234.         <Link className="btn btn-danger pull-right" to="">
  235.           Batalkan Pengajuan
  236.         </Link>
  237.       )}
  238.     </>
  239.   );
  240.  
  241.   return (
  242.     <Master>
  243.       <Section
  244.         pageName={"Detail Pengajuan"}
  245.         pageSubject={`Nomor ${getDetailSubmission.refnumber}`}
  246.         box_header={navButton}
  247.       >
  248.         <HeaderLayout
  249.           formAttr={{
  250.             refNumber,
  251.             submissionDate,
  252.             subjectForm,
  253.             recipient,
  254.             sender
  255.           }}
  256.         />
  257.         <hr />
  258.         <Datatable headContent={tableHeader} content={submission} />
  259.         <div className="modal fade" id="rejectModal">
  260.           <Modal
  261.             title="Reject Item"
  262.             close={
  263.               <button
  264.                 className="btn btn-default"
  265.                 data-dismiss="modal"
  266.                 role="dialog"
  267.               >
  268.                 Close
  269.               </button>
  270.             }
  271.             save={
  272.               <button
  273.                 className="btn btn-success"
  274.                 onClick={() => submitRejectReason()}
  275.               >
  276.                 Submit
  277.               </button>
  278.             }
  279.           >
  280.             <TextArea
  281.               formProp={[
  282.                 {
  283.                   forAttr: "reason",
  284.                   lableName: "Masukan alasan penolakan",
  285.                   inputAttr: {
  286.                     className: "form-control",
  287.                     id: "reason",
  288.                     name: "reason"
  289.                   }
  290.                 }
  291.               ]}
  292.             ></TextArea>
  293.           </Modal>
  294.         </div>
  295.       </Section>
  296.     </Master>
  297.   );
  298. };
  299.  
  300. const mapDispatchToProps = dispatch => {
  301.   return {
  302.     rejectSubmissionItem: payload => dispatch(rejectItem(payload)),
  303.     approveSubmissionItem: payload => dispatch(approveItem(payload))
  304.   };
  305. };
  306.  
  307. const mapStateToProps = state => {
  308.   return {
  309.     detailSubmission: state.submission.submissionForm
  310.   };
  311. };
  312.  
  313. export default connect(mapStateToProps, mapDispatchToProps)(SubmissionDetail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement