src/components/Customers/CustomerList.jsx   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 0

Size

Lines of Code 209
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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