src/components/Prepaid/PrepaidList.jsx   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 196
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 144
mnd 6
bc 6
fnc 0
dl 0
loc 196
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useState } from "react";
2
import { AiOutlineRight, AiOutlineCheck, AiOutlineClose } from "react-icons/ai";
3
4
const PrepaidList = ({
5
  prepaidData,
6
  filterPhrase,
7
  handleRemoveCard,
8
  handleEditForm,
9
  handleLogForm,
10
  setSelectedCard,
11
  setLogData,
12
}) => {
13
  const [isOpen, setIsOpen] = useState({});
14
  const [isDelete, setIsDelete] = useState({});
15
16
  const handleDeleteButton = (id) => {
17
    setIsDelete((prevState) => ({ ...prevState, [id]: !prevState[id] }));
18
  };
19
20
  const handleClick = (id) => {
21
    setIsOpen((prevState) => ({ ...prevState, [id]: !prevState[id] }));
22
  };
23
  const rotate = (bool) => {
24
    if (bool) {
25
      return "rotate(90deg)";
26
    }
27
    return "rotate(0deg)";
28
  };
29
30
  const prepaidInfo = (card) => {
31
    return (
32
      <div className="w-full flex justify-between">
33
        <div className="p-3">
34
          <div className="flex flex-row ">
35
            <div className="flex flex-col px-2">
36
              <h2>Code</h2>
37
              <p className="border-b bg-gray-200 border-gray-800 mr-2 mb-3 w-80">
38
                {card.code}
39
              </p>
40
              <h2>Amount</h2>
41
              <p className="border-b bg-gray-200 border-gray-800 mr-2 w-52">
42
                {card.amount}
43
              </p>
44
            </div>
45
            <div className="flex flex-col px-2">
46
              <h2>Total uses</h2>
47
              <p className="border-b bg-gray-200 border-gray-800 mr-2 mb-3 w-52">
48
                {card.totalUses}
49
              </p>
50
              <h2>Uses left</h2>
51
              <p className="border-b bg-gray-200 border-gray-800 mr-2 w-52">
52
                {card.usesLeft}
53
              </p>
54
            </div>
55
          </div>
56
        </div>
57
58
        <div className="flex flex-col p-4">
59
          <button
60
            onClick={() => {
61
              handleLogForm();
62
              setLogData(card.users);
63
            }}
64
            className="
65
                my-1 py-1 px-3 transition-colors bg-sidebarHover w-36
66
               hover:bg-sidebarBlue text-white rounded-full"
67
          >
68
            Logs
69
          </button>
70
          {/* <button
71
            onClick={() => {
72
              setSelectedCard(card);
73
              handleEditForm();
74
            }}
75
            className="
76
                my-1 py-1 px-3 transition-colors bg-sidebarHover w-36
77
               hover:bg-sidebarBlue text-white rounded-full"
78
          >
79
            Edit
80
          </button> */}
81
          {isDelete[card._id] ? (
82
            <div>
83
              <p>Confirm deletion?</p>
84
              <div className="flex justify-evenly">
85
                <button
86
                  onClick={() => {
87
                    handleDeleteButton(card._id);
88
                    handleRemoveCard(card._id);
89
                  }}
90
                  className="
91
                my-1 p-1 text-2xl transition-colors bg-sidebarHover
92
               hover:bg-sidebarBlue text-white rounded-full"
93
                >
94
                  <AiOutlineCheck />
95
                </button>
96
                <button
97
                  onClick={() => {
98
                    handleDeleteButton(card._id);
99
                  }}
100
                  className="
101
                my-1 p-1 text-2xl transition-colors bg-sidebarHover 
102
               hover:bg-sidebarBlue text-white rounded-full"
103
                >
104
                  <AiOutlineClose />
105
                </button>
106
              </div>
107
            </div>
108
          ) : (
109
            <button
110
              onClick={() => {
111
                handleDeleteButton(card._id);
112
              }}
113
              className="
114
                my-1 py-1 px-3 transition-colors bg-sidebarHover w-36
115
               hover:bg-sidebarBlue text-white rounded-full"
116
            >
117
              Delete
118
            </button>
119
          )}
120
        </div>
121
      </div>
122
    );
123
  };
124
125
  const prepaids = () => {
126
    return prepaidData
127
      .filter((item) => {
128
        if (filterPhrase === "") {
129
          return item;
130
        } else if (
131
          item.code.toLowerCase().includes(filterPhrase.toLowerCase())
132
        ) {
133
          return item;
134
        }
135
        return;
136
      })
137
      .map((item, index) => {
138
        return (
139
          <>
140
            <tr key={index} className="border-b text-base border-gray-400">
141
              <td className="py-3 px-6">{index}</td>
142
              <td className="py-3 px-6">{item.code}</td>
143
              <td className="py-3 px-6">{item.totalUses}</td>
144
              <td className="py-3 px-6">{item.usesLeft}</td>
145
              <td className="py-3 px-6">{item.amount}</td>
146
              <td className="py-3 px-6 w-2">
147
                <button
148
                  className="transition-transform"
149
                  style={{ transform: rotate(isOpen[item._id]) }}
150
                  onClick={() => {
151
                    handleClick(item._id);
152
                  }}
153
                >
154
                  <AiOutlineRight />
155
                </button>
156
              </td>
157
            </tr>
158
            {isOpen[item._id] ? (
159
              <td colspan="6">
160
                <div className="bg-gray-200 w-full border-b border-gray-400">
161
                  {prepaidInfo(item)}
162
                </div>
163
              </td>
164
            ) : (
165
              <div></div>
166
            )}
167
          </>
168
        );
169
      });
170
  };
171
172
  if (!prepaidData) {
173
    return <div>No prepaid cards</div>;
174
  }
175
176
  return (
177
    <>
178
      <table className="w-full text-lg text-left content-between">
179
        <thead className=" bg-sidebarBlue text-gray-200">
180
          <tr>
181
            <th className="font-normal px-6">ID</th>
182
            <th className="font-normal px-6">Code</th>
183
            <th className="font-normal px-6"> Total uses</th>
184
            <th className="font-normal px-6">Uses left</th>
185
            <th className="font-normal px-6">Amount</th>
186
            <th className="font-normal px-6">View</th>
187
          </tr>
188
        </thead>
189
        <tbody>{prepaids()}</tbody>
190
      </table>
191
    </>
192
  );
193
};
194
195
export default PrepaidList;
196