eleven-solutions-common-website-unique-web 2.0.3 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- declare function UserForm(): import("react/jsx-runtime").JSX.Element;
1
+ declare const UserForm: () => import("react/jsx-runtime").JSX.Element;
2
2
  export default UserForm;
@@ -1,5 +1,111 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- function UserForm() {
3
- return _jsx("div", { children: "UserForm" });
4
- }
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
+ import { useState, useEffect } from "react";
12
+ import { addUserApi, updateUserApi, fetchUserByIdApi } from "../api/user";
13
+ const UserForm = () => {
14
+ const [userName, setUserName] = useState("");
15
+ const [email, setEmail] = useState("");
16
+ const [mobile, setMobile] = useState("");
17
+ const [address, setAddress] = useState("");
18
+ const [roleType, setRoleType] = useState("");
19
+ const [, setUser] = useState(null);
20
+ const [isEditMode, setIsEditMode] = useState(false);
21
+ const validateForm = () => {
22
+ if (!userName || !email || !roleType) {
23
+ alert("Please fill in all required fields: User Name, Email, and Role Type.");
24
+ return false;
25
+ }
26
+ return true;
27
+ };
28
+ // const location = useLocation();
29
+ // // const navigate = useNavigate();
30
+ // const { id } = location.state || {};
31
+ const queryParams = new URLSearchParams(window.location.search);
32
+ const id = queryParams.get("id");
33
+ useEffect(() => {
34
+ if (id) {
35
+ const fetchUserData = () => __awaiter(void 0, void 0, void 0, function* () {
36
+ try {
37
+ const fetchedUser = yield fetchUserByIdApi(id);
38
+ if (fetchedUser) {
39
+ const user = fetchedUser;
40
+ setIsEditMode(true);
41
+ setUser(user);
42
+ setUserName(user.name);
43
+ setEmail(user.email);
44
+ setMobile(user.mobile);
45
+ setAddress(user.address);
46
+ setRoleType(user.roleType);
47
+ }
48
+ }
49
+ catch (error) {
50
+ console.error("Unable to fetch user data", error);
51
+ }
52
+ });
53
+ fetchUserData();
54
+ }
55
+ }, [id]);
56
+ // const handleSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
57
+ // event.preventDefault();
58
+ // if (!validateForm()) return;
59
+ // try {
60
+ // if (isEditMode) {
61
+ // await updateUserApi(id, userName, mobile, address, roleType);
62
+ // alert("User updated successfully");
63
+ // } else {
64
+ // await addUserApi(userName, email, mobile, address, roleType);
65
+ // alert("User added successfully");
66
+ // }
67
+ // navigate("/admin/user");
68
+ // } catch (error) {
69
+ // console.error("Error adding/updating User:", error);
70
+ // alert("Failed to add/update User");
71
+ // }
72
+ // };
73
+ const handleSubmit = (event) => __awaiter(void 0, void 0, void 0, function* () {
74
+ event.preventDefault();
75
+ if (!validateForm())
76
+ return;
77
+ try {
78
+ if (isEditMode) {
79
+ yield updateUserApi(id, userName, mobile, address, roleType);
80
+ alert("User updated successfully");
81
+ }
82
+ else {
83
+ yield addUserApi(userName, email, mobile, address, roleType);
84
+ alert("User added successfully");
85
+ }
86
+ window.location.href = "/admin/users";
87
+ }
88
+ catch (error) {
89
+ console.error("Error adding/updating User:", error);
90
+ alert("Failed to add/update User");
91
+ }
92
+ });
93
+ // const handleCancelClick = () => {
94
+ // setUserName("");
95
+ // setEmail("");
96
+ // setMobile("");
97
+ // setAddress("");
98
+ // setRoleType("");
99
+ // navigate("/admin/user");
100
+ // };
101
+ const handleCancelClick = () => {
102
+ setUserName("");
103
+ setEmail("");
104
+ setMobile("");
105
+ setAddress("");
106
+ setRoleType("");
107
+ window.location.href = "/admin/users";
108
+ };
109
+ return (_jsx("div", { className: "max-w-4xl p-6 mx-auto dark:bg-gray-800", children: _jsx("div", { children: _jsxs("form", { children: [_jsx("h1", { className: "text-3xl font-bold text-center text-blue-600 capitalize dark:text-white mb-4", children: isEditMode ? "Edit User" : "Add User" }), _jsxs("div", { className: "mb-6 w-full", children: [_jsxs("label", { className: "text-gray-900 dark:text-gray-200 font-semibold", children: ["User Name ", _jsx("span", { className: "text-red-500", children: "*" })] }), _jsx("input", { required: true, value: userName, onChange: (e) => setUserName(e.target.value), type: "text", className: "w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring", placeholder: "Enter User Name" })] }), _jsxs("div", { className: "mb-6 w-full", children: [_jsxs("label", { className: "text-gray-900 dark:text-gray-200 font-semibold", children: ["Email ", _jsx("span", { className: "text-red-500", children: "*" })] }), _jsx("input", { required: true, value: email, onChange: (e) => setEmail(e.target.value), type: "email", className: "w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring", placeholder: "Enter Email", disabled: isEditMode })] }), _jsxs("div", { className: "mb-6 w-full", children: [_jsx("label", { className: "text-gray-900 dark:text-gray-200 font-semibold", children: "Moblie Number" }), _jsx("input", { required: true, value: mobile, onChange: (e) => setMobile(e.target.value), type: "number", className: "w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring", placeholder: "Enter Mobile Number" })] }), _jsxs("div", { className: "mb-6 w-full", children: [_jsx("label", { className: "text-gray-900 dark:text-gray-200 font-semibold", children: "Address" }), _jsx("input", { required: true, value: address, onChange: (e) => setAddress(e.target.value), type: "text", className: "w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring", placeholder: "Address" })] }), _jsxs("div", { className: "mb-6 w-full", children: [_jsxs("label", { className: "text-gray-900 dark:text-gray-200 font-semibold", children: ["Role Type ", _jsx("span", { className: "text-red-500", children: "*" })] }), _jsxs("select", { required: true, value: roleType, onChange: (e) => setRoleType(e.target.value), className: "w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring", children: [_jsx("option", { value: "", children: "Select a Role Type" }), _jsx("option", { value: "1", children: "Guest" }), _jsx("option", { value: "2", children: "Admin" })] })] }), _jsxs("div", { className: "flex space-x-4 mt-6 justify-end", children: [_jsx("button", { type: "submit", onClick: handleSubmit, className: "px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600", children: isEditMode ? "Edit" : "Add" }), _jsx("button", { type: "button", onClick: handleCancelClick, className: "px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600", children: "Cancel" })] })] }) }) }));
110
+ };
5
111
  export default UserForm;
@@ -25,13 +25,6 @@ function Users() {
25
25
  useEffect(() => {
26
26
  fetchUsersData();
27
27
  }, []);
28
- // const handleNav = (e: React.FormEvent) => {
29
- // e.preventDefault();
30
- // window.history.pushState({}, "", "/admin/users/userform");
31
- // // Trigger React's routing logic manually if required
32
- // const navEvent = new PopStateEvent("popstate");
33
- // window.dispatchEvent(navEvent);
34
- // };
35
28
  const handleNavigation = (event, path) => {
36
29
  event.preventDefault();
37
30
  window.history.pushState({}, "", path);
@@ -68,7 +61,9 @@ function Users() {
68
61
  // navigate("/admin/user/userform", { state: { id } });
69
62
  // };
70
63
  const handleEditClick = (id) => {
71
- return (_jsx("a", { href: `/admin/users/userform?id=${id}`, onClick: (e) => e.preventDefault(), children: "Edit User" }));
64
+ const path = `/admin/users/userform?id=${id}`;
65
+ window.history.pushState({}, "", path);
66
+ window.dispatchEvent(new PopStateEvent("popstate"));
72
67
  };
73
68
  return (_jsxs("div", { className: "container px-4 mx-auto mt-6", children: [_jsx("div", { className: "w-3/4", children: _jsx("h1", { className: "text-3xl font-bold text-blue-600 mb-5", children: "Users" }) }), _jsx("div", { className: "flex-grow ml-0 w-full mt-10", children: _jsxs("form", { className: "w-full ", children: [_jsx("label", { htmlFor: "default-search", className: "mb-2 text-sm font-medium text-gray-900 sr-only dark:text-white", children: "Search" }), _jsxs("div", { className: "relative w-full", children: [_jsx("div", { className: "absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none", children: _jsx("svg", { className: "w-4 h-4 text-gray-500 dark:text-gray-400", "aria-hidden": "true", xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 20 20", children: _jsx("path", { stroke: "currentColor", "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", d: "m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z" }) }) }), _jsx("input", { type: "search", id: "default-search", className: "inline-block min-w-full p-3 ps-10 outline-none text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500", placeholder: "Search Users", required: true }), _jsx("button", { type: "submit", className: "text-white absolute end-1.5 bottom-1.5 bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800", children: "Search" })] })] }) }), _jsx("div", { className: "flex-grow ml-0 w-3/4", children: _jsx("form", { className: "w-auto" }) }), _jsxs("div", { className: "flex-grow ml-0 mt-4 w-full", children: [_jsx("div", { className: "overflow-hidden border border-gray-200 dark:border-gray-700 md:rounded-lg mt-6", children: _jsxs("table", { className: "min-w-full divide-y divide-gray-200 dark:divide-gray-700", children: [_jsx("thead", { className: "bg-gray-50 dark:bg-gray-800", children: _jsxs("tr", { children: [_jsx("th", { scope: "col", className: "px-4 py-3.5 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: "Edit/Delete" }), _jsx("th", { scope: "col", className: "py-3.5 px-4 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: _jsx("div", { className: "flex items-center gap-x-3", children: _jsx("span", { children: "Name" }) }) }), _jsx("th", { scope: "col", className: "px-4 py-3.5 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: _jsx("button", { className: "flex items-center gap-x-2", children: _jsx("span", { children: "Email" }) }) }), _jsx("th", { scope: "col", className: "px-4 py-3.5 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: _jsx("button", { className: "flex items-center gap-x-2", children: _jsx("span", { children: "Mobile" }) }) }), _jsx("th", { scope: "col", className: "px-4 py-3.5 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: "Address" }), _jsx("th", { scope: "col", className: "px-4 py-3.5 text-md font-normal text-left rtl:text-right text-gray-500 dark:text-gray-400", children: "Role Type" })] }) }), _jsx("tbody", { className: "bg-white divide-y divide-gray-200 dark:divide-gray-700 dark:bg-gray-900", children: paginatedUsers.map((user, index) => (_jsxs("tr", { children: [_jsx("td", { className: "px-4 py-4 text-sm whitespace-nowrap", children: _jsxs("div", { className: "flex items-center gap-x-6", children: [_jsx("button", { onClick: () => handleDeleteClick(user.id), className: "text-gray-500 transition-colors duration-200 dark:hover:text-red-600 dark:text-gray-300 hover:text-red-500 focus:outline-none", children: _jsx("svg", { xmlns: "http://www.w3.org/2000/svg", fill: "none", viewBox: "0 0 24 24", "stroke-width": "1.5", stroke: "currentColor", className: "w-5 h-5", children: _jsx("path", { "stroke-linecap": "round", "stroke-linejoin": "round", d: "M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" }) }) }), _jsx("a", { href: `/admin/users/userform?id=${user.id}`, onClick: (e) => {
74
69
  e.preventDefault(); // Prevent default navigation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleven-solutions-common-website-unique-web",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "main": "./dist/index.js",
5
5
  "scripts": {
6
6
  "build": "tsc",
@@ -1,227 +1,227 @@
1
- // import React from "react";
2
- // import { useState, useEffect } from "react";
3
-
4
- // import { addUserApi, updateUserApi, fetchUserByIdApi } from "../api/user";
5
-
6
- // const UserForm = () => {
7
- // const [userName, setUserName] = useState("");
8
- // const [email, setEmail] = useState("");
9
- // const [mobile, setMobile] = useState("");
10
- // const [address, setAddress] = useState("");
11
- // const [roleType, setRoleType] = useState("");
12
-
13
- // const [, setUser] = useState<any>(null);
14
- // const [isEditMode, setIsEditMode] = useState<boolean>(false);
15
-
16
- // const validateForm = () => {
17
- // if (!userName || !email || !roleType) {
18
- // alert(
19
- // "Please fill in all required fields: User Name, Email, and Role Type."
20
- // );
21
- // return false;
22
- // }
23
- // return true;
24
- // };
25
-
26
- // // const location = useLocation();
27
- // // // const navigate = useNavigate();
28
-
29
- // // const { id } = location.state || {};
30
-
31
- // const queryParams = new URLSearchParams(window.location.search);
32
- // const id = queryParams.get("id");
33
-
34
- // useEffect(() => {
35
- // if (id) {
36
- // const fetchUserData = async () => {
37
- // try {
38
- // const fetchedUser = await fetchUserByIdApi(id);
39
- // if (fetchedUser) {
40
- // const user = fetchedUser as {
41
- // name: string;
42
- // email: string;
43
- // mobile: string;
44
- // address: string;
45
- // roleType: string;
46
- // };
47
- // setIsEditMode(true);
48
- // setUser(user);
49
- // setUserName(user.name);
50
- // setEmail(user.email);
51
- // setMobile(user.mobile);
52
- // setAddress(user.address);
53
- // setRoleType(user.roleType);
54
- // }
55
- // } catch (error) {
56
- // console.error("Unable to fetch user data", error);
57
- // }
58
- // };
59
- // fetchUserData();
60
- // }
61
- // }, [id]);
62
-
63
- // // const handleSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
64
- // // event.preventDefault();
65
- // // if (!validateForm()) return;
66
-
67
- // // try {
68
- // // if (isEditMode) {
69
- // // await updateUserApi(id, userName, mobile, address, roleType);
70
- // // alert("User updated successfully");
71
- // // } else {
72
- // // await addUserApi(userName, email, mobile, address, roleType);
73
- // // alert("User added successfully");
74
- // // }
75
- // // navigate("/admin/user");
76
- // // } catch (error) {
77
- // // console.error("Error adding/updating User:", error);
78
- // // alert("Failed to add/update User");
79
- // // }
80
- // // };
81
-
82
- // const handleSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
83
- // event.preventDefault();
84
- // if (!validateForm()) return;
85
-
86
- // try {
87
- // if (isEditMode) {
88
- // await updateUserApi(id, userName, mobile, address, roleType);
89
- // alert("User updated successfully");
90
- // } else {
91
- // await addUserApi(userName, email, mobile, address, roleType);
92
- // alert("User added successfully");
93
- // }
94
- // window.location.href = "/admin/users";
95
- // } catch (error) {
96
- // console.error("Error adding/updating User:", error);
97
- // alert("Failed to add/update User");
98
- // }
99
- // };
100
-
101
- // // const handleCancelClick = () => {
102
- // // setUserName("");
103
- // // setEmail("");
104
- // // setMobile("");
105
- // // setAddress("");
106
- // // setRoleType("");
107
- // // navigate("/admin/user");
108
- // // };
109
-
110
- // const handleCancelClick = () => {
111
- // setUserName("");
112
- // setEmail("");
113
- // setMobile("");
114
- // setAddress("");
115
- // setRoleType("");
116
- // window.location.href = "/admin/users";
117
- // };
118
-
119
- // return (
120
- // <div className="max-w-4xl p-6 mx-auto dark:bg-gray-800">
121
- // <div>
122
- // <form>
123
- // <h1 className="text-3xl font-bold text-center text-blue-600 capitalize dark:text-white mb-4">
124
- // {isEditMode ? "Edit User" : "Add User"}
125
- // </h1>
126
-
127
- // <div className="mb-6 w-full">
128
- // <label className="text-gray-900 dark:text-gray-200 font-semibold">
129
- // User Name <span className="text-red-500">*</span>
130
- // </label>
131
- // <input
132
- // required
133
- // value={userName}
134
- // onChange={(e) => setUserName(e.target.value)}
135
- // type="text"
136
- // className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
137
- // placeholder="Enter User Name"
138
- // />
139
- // </div>
140
- // <div className="mb-6 w-full">
141
- // <label className="text-gray-900 dark:text-gray-200 font-semibold">
142
- // Email <span className="text-red-500">*</span>
143
- // </label>
144
- // <input
145
- // required
146
- // value={email}
147
- // onChange={(e) => setEmail(e.target.value)}
148
- // type="email"
149
- // className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
150
- // placeholder="Enter Email"
151
- // disabled={isEditMode}
152
- // />
153
- // </div>
154
- // <div className="mb-6 w-full">
155
- // <label className="text-gray-900 dark:text-gray-200 font-semibold">
156
- // Moblie Number
157
- // </label>
158
- // <input
159
- // required
160
- // value={mobile}
161
- // onChange={(e) => setMobile(e.target.value)}
162
- // type="number"
163
- // className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
164
- // placeholder="Enter Mobile Number"
165
- // />
166
- // </div>
167
- // <div className="mb-6 w-full">
168
- // <label className="text-gray-900 dark:text-gray-200 font-semibold">
169
- // Address
170
- // </label>
171
- // <input
172
- // required
173
- // value={address}
174
- // onChange={(e) => setAddress(e.target.value)}
175
- // type="text"
176
- // className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
177
- // placeholder="Address"
178
- // />
179
- // </div>
180
-
181
- // <div className="mb-6 w-full">
182
- // <label className="text-gray-900 dark:text-gray-200 font-semibold">
183
- // Role Type <span className="text-red-500">*</span>
184
- // </label>
185
- // <select
186
- // required
187
- // value={roleType}
188
- // onChange={(e) => setRoleType(e.target.value)}
189
- // className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
190
- // >
191
- // <option value="">Select a Role Type</option>
192
- // <option value="1">Guest</option>
193
- // <option value="2">Admin</option>
194
- // </select>
195
- // </div>
196
-
197
- // <div className="flex space-x-4 mt-6 justify-end">
198
- // <button
199
- // type="submit"
200
- // onClick={handleSubmit}
201
- // className="px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600"
202
- // >
203
- // {isEditMode ? "Edit" : "Add"}
204
- // </button>
205
- // <button
206
- // type="button"
207
- // onClick={handleCancelClick}
208
- // className="px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600"
209
- // >
210
- // Cancel
211
- // </button>
212
- // </div>
213
- // </form>
214
- // </div>
215
- // </div>
216
- // );
217
- // };
1
+ import React from "react";
2
+ import { useState, useEffect } from "react";
3
+
4
+ import { addUserApi, updateUserApi, fetchUserByIdApi } from "../api/user";
5
+
6
+ const UserForm = () => {
7
+ const [userName, setUserName] = useState("");
8
+ const [email, setEmail] = useState("");
9
+ const [mobile, setMobile] = useState("");
10
+ const [address, setAddress] = useState("");
11
+ const [roleType, setRoleType] = useState("");
12
+
13
+ const [, setUser] = useState<any>(null);
14
+ const [isEditMode, setIsEditMode] = useState<boolean>(false);
15
+
16
+ const validateForm = () => {
17
+ if (!userName || !email || !roleType) {
18
+ alert(
19
+ "Please fill in all required fields: User Name, Email, and Role Type."
20
+ );
21
+ return false;
22
+ }
23
+ return true;
24
+ };
25
+
26
+ // const location = useLocation();
27
+ // // const navigate = useNavigate();
28
+
29
+ // const { id } = location.state || {};
30
+
31
+ const queryParams = new URLSearchParams(window.location.search);
32
+ const id = queryParams.get("id");
33
+
34
+ useEffect(() => {
35
+ if (id) {
36
+ const fetchUserData = async () => {
37
+ try {
38
+ const fetchedUser = await fetchUserByIdApi(id);
39
+ if (fetchedUser) {
40
+ const user = fetchedUser as {
41
+ name: string;
42
+ email: string;
43
+ mobile: string;
44
+ address: string;
45
+ roleType: string;
46
+ };
47
+ setIsEditMode(true);
48
+ setUser(user);
49
+ setUserName(user.name);
50
+ setEmail(user.email);
51
+ setMobile(user.mobile);
52
+ setAddress(user.address);
53
+ setRoleType(user.roleType);
54
+ }
55
+ } catch (error) {
56
+ console.error("Unable to fetch user data", error);
57
+ }
58
+ };
59
+ fetchUserData();
60
+ }
61
+ }, [id]);
62
+
63
+ // const handleSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
64
+ // event.preventDefault();
65
+ // if (!validateForm()) return;
66
+
67
+ // try {
68
+ // if (isEditMode) {
69
+ // await updateUserApi(id, userName, mobile, address, roleType);
70
+ // alert("User updated successfully");
71
+ // } else {
72
+ // await addUserApi(userName, email, mobile, address, roleType);
73
+ // alert("User added successfully");
74
+ // }
75
+ // navigate("/admin/user");
76
+ // } catch (error) {
77
+ // console.error("Error adding/updating User:", error);
78
+ // alert("Failed to add/update User");
79
+ // }
80
+ // };
81
+
82
+ const handleSubmit = async (event: React.MouseEvent<HTMLButtonElement>) => {
83
+ event.preventDefault();
84
+ if (!validateForm()) return;
85
+
86
+ try {
87
+ if (isEditMode) {
88
+ await updateUserApi(id, userName, mobile, address, roleType);
89
+ alert("User updated successfully");
90
+ } else {
91
+ await addUserApi(userName, email, mobile, address, roleType);
92
+ alert("User added successfully");
93
+ }
94
+ window.location.href = "/admin/users";
95
+ } catch (error) {
96
+ console.error("Error adding/updating User:", error);
97
+ alert("Failed to add/update User");
98
+ }
99
+ };
100
+
101
+ // const handleCancelClick = () => {
102
+ // setUserName("");
103
+ // setEmail("");
104
+ // setMobile("");
105
+ // setAddress("");
106
+ // setRoleType("");
107
+ // navigate("/admin/user");
108
+ // };
109
+
110
+ const handleCancelClick = () => {
111
+ setUserName("");
112
+ setEmail("");
113
+ setMobile("");
114
+ setAddress("");
115
+ setRoleType("");
116
+ window.location.href = "/admin/users";
117
+ };
118
+
119
+ return (
120
+ <div className="max-w-4xl p-6 mx-auto dark:bg-gray-800">
121
+ <div>
122
+ <form>
123
+ <h1 className="text-3xl font-bold text-center text-blue-600 capitalize dark:text-white mb-4">
124
+ {isEditMode ? "Edit User" : "Add User"}
125
+ </h1>
126
+
127
+ <div className="mb-6 w-full">
128
+ <label className="text-gray-900 dark:text-gray-200 font-semibold">
129
+ User Name <span className="text-red-500">*</span>
130
+ </label>
131
+ <input
132
+ required
133
+ value={userName}
134
+ onChange={(e) => setUserName(e.target.value)}
135
+ type="text"
136
+ className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
137
+ placeholder="Enter User Name"
138
+ />
139
+ </div>
140
+ <div className="mb-6 w-full">
141
+ <label className="text-gray-900 dark:text-gray-200 font-semibold">
142
+ Email <span className="text-red-500">*</span>
143
+ </label>
144
+ <input
145
+ required
146
+ value={email}
147
+ onChange={(e) => setEmail(e.target.value)}
148
+ type="email"
149
+ className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
150
+ placeholder="Enter Email"
151
+ disabled={isEditMode}
152
+ />
153
+ </div>
154
+ <div className="mb-6 w-full">
155
+ <label className="text-gray-900 dark:text-gray-200 font-semibold">
156
+ Moblie Number
157
+ </label>
158
+ <input
159
+ required
160
+ value={mobile}
161
+ onChange={(e) => setMobile(e.target.value)}
162
+ type="number"
163
+ className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
164
+ placeholder="Enter Mobile Number"
165
+ />
166
+ </div>
167
+ <div className="mb-6 w-full">
168
+ <label className="text-gray-900 dark:text-gray-200 font-semibold">
169
+ Address
170
+ </label>
171
+ <input
172
+ required
173
+ value={address}
174
+ onChange={(e) => setAddress(e.target.value)}
175
+ type="text"
176
+ className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
177
+ placeholder="Address"
178
+ />
179
+ </div>
180
+
181
+ <div className="mb-6 w-full">
182
+ <label className="text-gray-900 dark:text-gray-200 font-semibold">
183
+ Role Type <span className="text-red-500">*</span>
184
+ </label>
185
+ <select
186
+ required
187
+ value={roleType}
188
+ onChange={(e) => setRoleType(e.target.value)}
189
+ className="w-full px-4 py-2 mt-2 text-gray-800 bg-white border border-gray-400 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-800 focus:border-blue-400 focus:ring-blue-300 focus:ring-opacity-40 dark:focus:border-blue-300 focus:outline-none focus:ring"
190
+ >
191
+ <option value="">Select a Role Type</option>
192
+ <option value="1">Guest</option>
193
+ <option value="2">Admin</option>
194
+ </select>
195
+ </div>
196
+
197
+ <div className="flex space-x-4 mt-6 justify-end">
198
+ <button
199
+ type="submit"
200
+ onClick={handleSubmit}
201
+ className="px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600"
202
+ >
203
+ {isEditMode ? "Edit" : "Add"}
204
+ </button>
205
+ <button
206
+ type="button"
207
+ onClick={handleCancelClick}
208
+ className="px-8 py-2.5 leading-5 text-white transition-colors duration-300 transform bg-blue-600 rounded-md hover:bg-blue-500 focus:outline-none focus:bg-gray-600"
209
+ >
210
+ Cancel
211
+ </button>
212
+ </div>
213
+ </form>
214
+ </div>
215
+ </div>
216
+ );
217
+ };
218
218
 
219
- // export default UserForm;
219
+ export default UserForm;
220
220
 
221
- import React from "react";
221
+ // import React from "react";
222
222
 
223
- function UserForm() {
224
- return <div>UserForm</div>;
225
- }
223
+ // function UserForm() {
224
+ // return <div>UserForm</div>;
225
+ // }
226
226
 
227
- export default UserForm;
227
+ // export default UserForm;
@@ -20,14 +20,6 @@ function Users() {
20
20
  useEffect(() => {
21
21
  fetchUsersData();
22
22
  }, []);
23
- // const handleNav = (e: React.FormEvent) => {
24
- // e.preventDefault();
25
- // window.history.pushState({}, "", "/admin/users/userform");
26
-
27
- // // Trigger React's routing logic manually if required
28
- // const navEvent = new PopStateEvent("popstate");
29
- // window.dispatchEvent(navEvent);
30
- // };
31
23
 
32
24
  const handleNavigation = (event: React.MouseEvent, path: string) => {
33
25
  event.preventDefault();
@@ -77,14 +69,9 @@ function Users() {
77
69
  // };
78
70
 
79
71
  const handleEditClick = (id: string) => {
80
- return (
81
- <a
82
- href={`/admin/users/userform?id=${id}`}
83
- onClick={(e) => e.preventDefault()}
84
- >
85
- Edit User
86
- </a>
87
- );
72
+ const path = `/admin/users/userform?id=${id}`;
73
+ window.history.pushState({}, "", path);
74
+ window.dispatchEvent(new PopStateEvent("popstate"));
88
75
  };
89
76
 
90
77
  return (