Skip to content

Commit 4509c81

Browse files
authored
Merge pull request #23 from ClassConnect-org/dev
Dev
2 parents 0d2d2e8 + 5d351e4 commit 4509c81

File tree

5 files changed

+71
-26
lines changed

5 files changed

+71
-26
lines changed

src/components/AIChat.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ const AIChatManagment: React.FC = () => {
148148
};
149149

150150
const handlePost = async (refine: Refine) => {
151-
await postRefine([{
152-
question: refine.question,
153-
answer: refine.answer,
154-
id: refine.id,
155-
}]);
156-
fetchRefines();
151+
if (window.confirm("Are you sure you want to submit this chat?")) {
152+
await postRefine([{
153+
question: refine.question,
154+
answer: refine.answer,
155+
id: refine.id,
156+
}]);
157+
fetchRefines();
158+
}
157159
};
158160

159161
const handleSave = (refine: Refine) => {
@@ -167,7 +169,7 @@ const AIChatManagment: React.FC = () => {
167169
};
168170

169171
const handleDelete = async (id: string) => {
170-
if (window.confirm("Are you sure you want to delete this rule?")) {
172+
if (window.confirm("Are you sure you want to delete this chat?")) {
171173
await deleteChat(id);
172174
fetchRefines();
173175
if (expandedRefineId === id) {
@@ -176,7 +178,7 @@ const AIChatManagment: React.FC = () => {
176178
}
177179
};
178180

179-
const toggleExpandRule = (id: string) => {
181+
const toggleExpandChat = (id: string) => {
180182
setExpandedRefineId(expandedRefineId === id ? null : id);
181183
};
182184

@@ -189,7 +191,7 @@ return (
189191
<div key={refine.id} className="border border-gray-200 rounded-lg overflow-hidden shadow-sm hover:shadow-md transition-shadow">
190192
<div
191193
className="p-4 bg-white hover:bg-gray-50 cursor-pointer flex justify-between items-center"
192-
onClick={() => toggleExpandRule(refine.id)}
194+
onClick={() => toggleExpandChat(refine.id)}
193195
>
194196
<div>
195197
<span className="text-gray-500 text-sm mr-2">User Message:</span>

src/components/Admins.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const AdminsManagementView: React.FC = () => {
1010
const [showModal, setShowModal] = useState(false);
1111
const [searchTerm, setSearchTerm] = useState("");
1212

13-
const ITEMS_PER_PAGE = 10;
14-
1513
const fetchAdmins = async (page = 1, email = "") => {
1614
const { admins, total } = await getAdmins({ page, email });
1715
setAdmins(admins);
1816
setTotal(total);
1917
};
2018

19+
const PAGE_SIZE = 10
20+
2121
useEffect(() => {
2222
fetchAdmins(currentPage, searchTerm);
2323
}, [currentPage, searchTerm]);
@@ -74,12 +74,12 @@ const AdminsManagementView: React.FC = () => {
7474
</table>
7575
</div>
7676

77-
{total > ITEMS_PER_PAGE && (
77+
{total > PAGE_SIZE && (
7878
<div className="flex items-center justify-between border-t border-gray-200 pt-4">
7979
<div>
8080
<p className="text-sm text-gray-700">
81-
Showing <span className="font-medium">{(currentPage - 1) * ITEMS_PER_PAGE + 1}</span> to{' '}
82-
<span className="font-medium">{Math.min(currentPage * ITEMS_PER_PAGE, total)}</span> of{' '}
81+
Showing <span className="font-medium">{(currentPage - 1) * PAGE_SIZE + 1}</span> to{' '}
82+
<span className="font-medium">{Math.min(currentPage * PAGE_SIZE, total)}</span> of{' '}
8383
<span className="font-medium">{total}</span> results
8484
</p>
8585
</div>
@@ -92,11 +92,11 @@ const AdminsManagementView: React.FC = () => {
9292
Previous
9393
</button>
9494
<span className="text-sm text-gray-600">
95-
Page {currentPage} of {Math.ceil(total / ITEMS_PER_PAGE)}
95+
Page {currentPage} of {Math.ceil(total / PAGE_SIZE)}
9696
</span>
9797
<button
9898
onClick={() => setCurrentPage((p) => p + 1)}
99-
disabled={currentPage >= Math.ceil(total / ITEMS_PER_PAGE)}
99+
disabled={currentPage >= Math.ceil(total / PAGE_SIZE)}
100100
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 hover:bg-gray-50"
101101
>
102102
Next

src/components/Rules.tsx

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ import type { Rule, RulePayload } from "../@types/rules";
33
import { getRules, createRule, updateRule, deleteRule } from "../services/rules";
44
import RuleFormModal from "./RuleFormModal";
55

6+
const PAGE_SIZE = 3;
7+
68
const RulesAdminView: React.FC = () => {
79
const [rules, setRules] = useState<Rule[]>([]);
8-
const [_, setTotal] = useState(0);
10+
const [currentPage, setCurrentPage] = useState(1);
11+
const [total, setTotal] = useState(0);
912
const [selectedRule, setSelectedRule] = useState<Rule | null>(null);
1013
const [showModal, setShowModal] = useState(false);
1114
const [expandedRuleId, setExpandedRuleId] = useState<string | null>(null);
1215

13-
const fetchRules = async () => {
14-
const { rules, total } = await getRules();
16+
const fetchRules = async (page = 1) => {
17+
const { rules, total } = await getRules({ page });
1518
setRules(rules);
1619
setTotal(total);
1720
};
1821

1922
useEffect(() => {
20-
fetchRules();
21-
}, []);
23+
fetchRules(currentPage);
24+
}, [currentPage]);
2225

2326
const handleCreate = () => {
2427
setSelectedRule(null);
@@ -33,7 +36,14 @@ const RulesAdminView: React.FC = () => {
3336
const handleDelete = async (id: string) => {
3437
if (window.confirm("Are you sure you want to delete this rule?")) {
3538
await deleteRule(id);
36-
fetchRules();
39+
40+
const newTotal = total - 1;
41+
const newTotalPages = Math.ceil(newTotal / PAGE_SIZE);
42+
const newPage = currentPage > newTotalPages ? Math.max(1, currentPage - 1) : currentPage;
43+
44+
setCurrentPage(newPage);
45+
await fetchRules(newPage);
46+
3747
if (expandedRuleId === id) {
3848
setExpandedRuleId(null);
3949
}
@@ -43,11 +53,13 @@ const RulesAdminView: React.FC = () => {
4353
const handleSubmit = async (data: RulePayload) => {
4454
if (selectedRule) {
4555
await updateRule(selectedRule.id, data);
56+
await fetchRules(currentPage);
4657
} else {
4758
await createRule(data);
59+
setCurrentPage(1);
60+
await fetchRules(1);
4861
}
4962
setShowModal(false);
50-
fetchRules();
5163
};
5264

5365
const toggleExpandRule = (id: string) => {
@@ -136,6 +148,35 @@ const RulesAdminView: React.FC = () => {
136148
))}
137149
</div>
138150

151+
<div className="flex items-center justify-between border-t border-gray-200 pt-4">
152+
<div>
153+
<p className="text-sm text-gray-700">
154+
Showing <span className="font-medium">{(currentPage - 1) * PAGE_SIZE + 1}</span> to{' '}
155+
<span className="font-medium">{Math.min(currentPage * PAGE_SIZE, total)}</span> of{' '}
156+
<span className="font-medium">{total}</span> results
157+
</p>
158+
</div>
159+
<div className="flex justify-end items-center space-x-2 pt-4">
160+
<button
161+
onClick={() => setCurrentPage((p) => Math.max(1, p - 1))}
162+
disabled={currentPage === 1}
163+
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 hover:bg-gray-50"
164+
>
165+
Previous
166+
</button>
167+
<span className="text-sm text-gray-600">
168+
Page {currentPage} of {Math.ceil(total / PAGE_SIZE)}
169+
</span>
170+
<button
171+
onClick={() => setCurrentPage((p) => p + 1)}
172+
disabled={currentPage >= Math.ceil(total / PAGE_SIZE)}
173+
className="px-3 py-1 border border-gray-300 rounded text-sm disabled:opacity-50 hover:bg-gray-50"
174+
>
175+
Next
176+
</button>
177+
</div>
178+
</div>
179+
139180
{showModal && (
140181
<RuleFormModal
141182
initialData={selectedRule}

src/components/Users.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const UsersManagmentView: React.FC = () => {
6969
};
7070

7171
const handleDelete = async (id: string) => {
72-
if (window.confirm("Are you sure you want to delete this rule?")) {
72+
if (window.confirm("Are you sure you want to delete this user?")) {
7373
await deletePlatformUser(id);
7474
fetchUsers();
7575
}

src/services/rules.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const BASE_URL = "/rules";
55

66
const adminServiceUrl = import.meta.env.VITE_ADMIN_SERVICE_URL;
77

8-
export const getRules = async (): Promise<{ rules: Rule[], total: number }> => {
9-
const res = await axios.get(adminServiceUrl + BASE_URL, { withCredentials: true });
8+
export const getRules = async ({
9+
page = 1,
10+
}: { page?: number; email?: string } = {}): Promise<{ rules: Rule[], total: number }> => {
11+
const res = await axios.get(adminServiceUrl + BASE_URL, { params: {page},withCredentials: true });
1012
return { rules: res.data.rules, total: res.data.total };
1113
};
1214

0 commit comments

Comments
 (0)