All files / components/Commons AdminCommonsCard.jsx

100% Statements 146/146
100% Branches 23/23
100% Functions 12/12
100% Lines 146/146

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 2101x 1x 1x 1x 1x 1x   1x 34x   34x 34x   34x   34x 34x 34x 1x 1x 1x 1x   34x 34x 34x 34x 34x 34x     34x 1x 1x   34x 2x 2x   31x 1x 1x   31x 4x 4x   31x 1x 1x   31x 1x 1x   31x 2x 2x 2x   31x   31x 31x 31x 31x   31x       31x 31x 31x 31x 31x   31x 31x     31x   31x 31x 31x 31x 31x 31x       31x 31x 31x 31x 31x             31x 31x 31x 31x 31x 31x 34x 34x 34x 1x 30x 34x 34x 34x   34x 34x 34x     34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x   34x   34x 34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x       34x 34x 34x 34x 34x 34x             34x     34x  
import React, { useState } from "react";
import { Card, Button, Modal, Row, Col } from "react-bootstrap";
import { useNavigate } from "react-router";
import { useBackendMutation } from "main/utils/useBackend";
import { hasRole } from "main/utils/currentUser";
import { onDeleteSuccess } from "main/utils/commonsUtils";
 
export default function AdminCommonsCard({ commonItem, currentUser }) {
  const navigate = useNavigate();
 
  const [showModal, setShowModal] = useState(false);
  const [isHovered, setIsHovered] = useState(false);
 
  const commons = commonItem?.commons;
 
  const axiosParamsDelete =
    /* istanbul ignore next */
    (id) => ({
      url: "/api/commons",
      method: "DELETE",
      params: { id },
    });
 
  const deleteMutation = useBackendMutation(
    axiosParamsDelete,
    {
      onSuccess: onDeleteSuccess,
    },
    ["/api/commons/allplus"],
  );
 
  if (!hasRole(currentUser, "ROLE_ADMIN")) {
    return null;
  }
 
  if (!commons) {
    return null;
  }
 
  const handleEdit = () => {
    navigate(`/admin/editcommons/${commons.id}`);
  };
 
  const handleDelete = () => {
    setShowModal(true);
  };
 
  const handleLeaderboard = () => {
    navigate(`/leaderboard/${commons.id}`);
  };
 
  const handleChat = () => {
    navigate(`/admin/chat/${commons.id}`);
  };
 
  const handleDeleteConfirm = () => {
    deleteMutation.mutate(commons.id);
    setShowModal(false);
  };
 
  const formatDate = (dateString) => String(dateString).slice(0, 10);
 
  const renderField = (label, value) => (
    <Row className="mb-2">
      <Col sm={6}>
        <strong>{label}:</strong>
      </Col>
      <Col sm={6}>{value}</Col>
    </Row>
  );
 
  const deleteModal = (
    <Modal
      show={showModal}
      onHide={() => setShowModal(false)}
      data-testid={`AdminCommonsCard-Modal-${commons.id}`}
    >
      <Modal.Header closeButton>
        <Modal.Title>Confirm Deletion</Modal.Title>
      </Modal.Header>
 
      <Modal.Body>Are you sure you want to delete this commons?</Modal.Body>
 
      <Modal.Footer>
        <Button
          variant="secondary"
          onClick={() => setShowModal(false)}
          data-testid={`AdminCommonsCard-Modal-Cancel-${commons.id}`}
        >
          Keep this Commons
        </Button>
 
        <Button
          variant="danger"
          onClick={handleDeleteConfirm}
          data-testid={`AdminCommonsCard-Modal-Delete-${commons.id}`}
        >
          Permanently Delete
        </Button>
      </Modal.Footer>
    </Modal>
  );
 
  return (
    <>
      <Card
        className="mb-3"
        data-testid={`AdminCommonsCard-${commons.id}`}
        style={{
          transform: isHovered ? "scale(1.02)" : "scale(1)",
          transition: "transform 0.2s",
          boxShadow: isHovered
            ? "0 4px 8px rgba(0,0,0,0.17)"
            : "0 2px 4px rgba(0,0,0,0.1)",
        }}
        onMouseEnter={() => setIsHovered(true)}
        onMouseLeave={() => setIsHovered(false)}
      >
        <Card.Body>
          <Card.Title>
            {commons.name} (ID: {commons.id})
          </Card.Title>
 
          {renderField("Cow Price", commons.cowPrice)}
          {renderField("Milk Price", commons.milkPrice)}
          {renderField("Start Balance", commons.startingBalance)}
          {renderField("Starting Date", formatDate(commons.startingDate))}
          {renderField("Last Date", formatDate(commons.lastDate))}
          {renderField("Degrad Rate", commons.degradationRate)}
          {renderField("Show Leaderboard", String(commons.showLeaderboard))}
          {renderField("Show Chat", String(commons.showChat))}
          {renderField("Total Cows", commonItem.totalCows || 0)}
          {renderField("Cap / User", commons.capacityPerUser)}
          {renderField("Carry Cap", commons.carryingCapacity)}
          {renderField("Eff Cap", commonItem.effectiveCapacity || 0)}
 
          <hr />
 
          <div className="d-flex flex-wrap gap-2">
            <Button
              variant="primary"
              size="sm"
              onClick={handleEdit}
              data-testid={`AdminCommonsCard-Edit-${commons.id}`}
            >
              Edit
            </Button>
 
            <Button
              variant="danger"
              size="sm"
              onClick={handleDelete}
              data-testid={`AdminCommonsCard-Delete-${commons.id}`}
            >
              Delete
            </Button>
 
            <Button
              variant="secondary"
              size="sm"
              onClick={handleLeaderboard}
              data-testid={`AdminCommonsCard-Leaderboard-${commons.id}`}
            >
              Leaderboard
            </Button>
 
            <Button
              variant="success"
              size="sm"
              href={`/api/commonstats/download?commonsId=${commons.id}`}
              data-testid={`AdminCommonsCard-StatsCSV-${commons.id}`}
            >
              Stats CSV
            </Button>
 
            <Button
              variant="info"
              size="sm"
              href={`/admin/announcements/${commons.id}`}
              data-testid={`AdminCommonsCard-Announcements-${commons.id}`}
            >
              Announcements
            </Button>
 
            <Button
              variant="primary"
              size="sm"
              onClick={handleChat}
              data-testid={`AdminCommonsCard-Chat-${commons.id}`}
            >
              Chat
            </Button>
 
            <Button
              variant="info"
              size="sm"
              href={`/admin/dashboard/${commons.id}`}
              data-testid={`AdminCommonsCard-Dashboard-${commons.id}`}
            >
              Dashboard
            </Button>
          </div>
        </Card.Body>
      </Card>
 
      {deleteModal}
    </>
  );
}