All files / pages/admin/ManageAsset/components AssetDetailsModal.tsx

90.9% Statements 20/22
50% Branches 1/2
100% Functions 4/4
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1492x 2x   2x 2x 2x                     12x               12x     12x 12x 12x 12x     12x                 12x   2x           12x 6x           6x 1x     12x                                                                                                                                                                           2x  
import { Form, Modal, message } from "antd";
import AssignmentHistory from "./AssignmentHistory";
import { AssetResponse } from "@/types/Asset";
import { useQuery } from "react-query";
import { AssignmentAPICaller } from "@/services/apis/assignment.api";
import { useEffect, useState } from "react";
import { AssignmentHistoryDto } from "@/types/Assignment";
import APIResponse from "@/types/APIResponse";
 
interface Props {
  assetData: AssetResponse;
  show?: boolean;
  handleClose?: () => void;
}
function AssetDetailsModal({ assetData, show, handleClose }: Props) {
 
  const displayState = {
    AVAILABLE: "Available",
    NOT_AVAILABLE: "Not Available",
    ASSIGNED: "Assigned",
    WAITING_FOR_RECYCLE: "Waiting for recycle",
    RECYCLED: "Recycled",
  };
 
  const [items, setItems] = useState<AssignmentHistoryDto[]>([]);
  function formatDate(dateInput: Date) {
    // Extract day, month, and year from the date object
    const date = new Date(dateInput);
    const day = String(date.getDate()).padStart(2, "0");
    const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are zero based
    const year = date.getFullYear();
 
    // Return the formatted date string in DD/MM/YYYY format
    return `${day}/${month}/${year}`;
  }
 
  const {
    data: queryData,
    isSuccess,
    isError,
    isLoading,
    error,
  } = useQuery(
    ["getAssetHistory", { assetData }],
    () => AssignmentAPICaller.getHistory(assetData.id),
    {
      enabled: show,
    }
  );
 
  useEffect(() => {
    Iif (isError) {
      const errorResponse = (error as { response: { data: APIResponse } })
        .response.data;
      message.error(errorResponse.message);
    }
 
    if (isSuccess) {
      setItems(queryData?.data?.result.data);
    }
  }, [error, isError, isSuccess, queryData]);
  return (
    <Modal
      title={
        <>
          <p className="text-lg font-semibold primary-color">
            Detailed Asset Information
          </p>
          <br />
          <hr />
        </>
      }
      open={show}
      footer={null}
      onCancel={handleClose}
      width={900}
    >
        <Form layout="horizontal">
          <Form.Item
            label="Asset Code"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{assetData.assetCode}</p>
          </Form.Item>
          <Form.Item
            label="Asset name"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{assetData.name}</p>
          </Form.Item>
          <Form.Item
            label="Category"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{assetData.category}</p>
          </Form.Item>
          <Form.Item
            label="Installed date"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{formatDate(assetData.installDate)}</p>
          </Form.Item>
          <Form.Item
            label="State"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{displayState[assetData.state as "AVAILABLE"| "NOT_AVAILABLE"|"ASSIGNED"| "WAITING_FOR_RECYCLE"| "RECYCLED"]}</p>
          </Form.Item>
          <Form.Item
            label="Location"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{assetData.location.name}</p>
          </Form.Item>
          <Form.Item
            label="Specification"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <p>{assetData.specification}</p>
          </Form.Item>
          <Form.Item
            label="History"
            labelCol={{ offset: 2, span: 6 }}
            labelAlign="left"
            className="my-2"
          >
            <AssignmentHistory data={items} isLoading={isLoading} />
          </Form.Item>
        </Form>
    </Modal>
  );
}
 
export default AssetDetailsModal;