team-member-bulk-edit-page.tsx
9.37 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React, { useEffect, useState } from "react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { Switch } from "../ui/switch";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "../ui/select";
import { toast } from "sonner";
import { ApiError } from "../../lib/apiClient";
import { getRoles } from "../../services/roleService";
import { updateTeamMembersBulk, type TeamMemberBulkUpdateItemVo } from "../../services/teamMemberService";
import type { RoleDto } from "../../types/role";
import type { TeamMemberDto } from "../../types/teamMember";
const ZERO = "00000000-0000-0000-0000-000000000000";
/** 列表/接口可能把 phone 等字段打成数字;统一成字符串再 trim,避免白屏 */
function trimStr(v: unknown): string {
if (v == null) return "";
return String(v).trim();
}
function isValidBulkId(id: string): boolean {
const s = (id ?? "").trim();
if (!s) return false;
return s.toLowerCase() !== ZERO;
}
function toPhoneNumber(v: string): number | null {
const s = v.trim();
if (!s) return null;
const num = Number(s.replace(/\D/g, "")) || 0;
return num;
}
export type TeamMemberBulkEditPageProps = {
seed: TeamMemberDto[];
onBack: () => void;
onSaved: () => void;
};
type RowState = {
id: string;
fullName: string;
userName: string;
password: string;
email: string;
phone: string;
roleId: string;
locationIdsCsv: string;
state: boolean;
};
function memberToRow(m: TeamMemberDto): RowState {
const lids = Array.isArray(m.locationIds) ? m.locationIds : [];
return {
id: trimStr(m.id),
fullName: trimStr(m.fullName),
userName: trimStr(m.userName),
password: "",
email: trimStr(m.email),
phone: trimStr(m.phone),
roleId: trimStr(m.roleId),
locationIdsCsv: lids.map((x) => trimStr(x)).filter(Boolean).join(","),
state: m.state !== false,
};
}
function parseIdsCsv(s: string): string[] {
return s
.split(/[,;|\s]+/)
.map((x) => x.trim())
.filter(Boolean);
}
export function TeamMemberBulkEditPage({ seed, onBack, onSaved }: TeamMemberBulkEditPageProps) {
const [rows, setRows] = useState<RowState[]>([]);
const [roles, setRoles] = useState<RoleDto[]>([]);
const [saving, setSaving] = useState(false);
useEffect(() => {
let c = false;
(async () => {
try {
const out: RoleDto[] = [];
let page = 1;
const size = 100;
for (;;) {
const res = await getRoles({ skipCount: page, maxResultCount: size });
out.push(...(res.items ?? []));
if (!res.items || res.items.length < size) break;
page += 1;
if (page > 50) break;
}
if (!c) setRoles(out);
} catch {
if (!c) setRoles([]);
}
})();
return () => {
c = true;
};
}, []);
useEffect(() => {
setRows(seed.map(memberToRow));
}, [seed]);
const updateRow = (idx: number, patch: Partial<RowState>) => {
setRows((prev) => {
const next = [...prev];
next[idx] = { ...next[idx], ...patch };
return next;
});
};
const handleSave = async () => {
const items: TeamMemberBulkUpdateItemVo[] = rows
.filter((r) => isValidBulkId(r.id))
.map((r) => {
const item: TeamMemberBulkUpdateItemVo = {
id: r.id.trim(),
fullName: r.fullName.trim(),
userName: r.userName.trim(),
email: r.email.trim() || null,
phone: toPhoneNumber(r.phone),
roleId: r.roleId.trim(),
locationIds: parseIdsCsv(r.locationIdsCsv),
state: r.state !== false,
};
const pw = r.password.trim();
if (pw) item.password = pw;
return item;
});
if (items.length === 0) {
toast.error("No valid rows", { description: "Select team members in the list first." });
return;
}
setSaving(true);
try {
const res = await updateTeamMembersBulk({ items });
toast.success("Bulk update finished", {
description: `Success: ${res.successCount}, failed: ${res.failCount}`,
});
onSaved();
onBack();
} catch (e) {
const msg = e instanceof ApiError ? e.message : e instanceof Error ? e.message : "Save failed.";
toast.error("Bulk save failed", { description: msg });
} finally {
setSaving(false);
}
};
return (
<div className="flex flex-col h-full min-h-0 bg-white">
<div className="flex items-center justify-between gap-3 px-4 py-3 border-b border-gray-200 shrink-0">
<Button type="button" variant="outline" onClick={onBack}>
Back
</Button>
<h1 className="text-base font-semibold text-gray-900 flex-1 text-center truncate px-2">
Team member bulk edit
</h1>
<Button
type="button"
className="bg-green-600 hover:bg-green-700 text-white shrink-0"
disabled={saving}
onClick={() => void handleSave()}
>
{saving ? "Saving…" : "Save All"}
</Button>
</div>
<div className="overflow-auto flex-1 min-h-0 px-2 py-3">
<table className="w-full text-xs border-collapse border border-gray-200">
<thead className="bg-gray-100 sticky top-0 z-10">
<tr>
<th className="border p-1 w-9 text-center text-gray-600 font-semibold">#</th>
<th className="border p-1 whitespace-nowrap">Full name *</th>
<th className="border p-1 whitespace-nowrap">User name *</th>
<th className="border p-1 whitespace-nowrap">Password</th>
<th className="border p-1 whitespace-nowrap">Email</th>
<th className="border p-1 whitespace-nowrap">Phone</th>
<th className="border p-1 whitespace-nowrap">Role *</th>
<th className="border p-1 whitespace-nowrap">Location IDs</th>
<th className="border p-1 whitespace-nowrap">Active</th>
</tr>
</thead>
<tbody>
{rows.map((r, idx) => (
<tr key={`${r.id || "e"}-${idx}`}>
<td className="border p-1 text-center align-middle text-gray-700 tabular-nums text-xs font-medium">
{idx + 1}
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[100px]"
value={r.fullName}
onChange={(e) => updateRow(idx, { fullName: e.target.value })}
/>
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[100px]"
value={r.userName}
onChange={(e) => updateRow(idx, { userName: e.target.value })}
/>
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[80px]"
type="password"
placeholder="(unchanged)"
value={r.password}
onChange={(e) => updateRow(idx, { password: e.target.value })}
/>
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[120px]"
value={r.email}
onChange={(e) => updateRow(idx, { email: e.target.value })}
/>
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[88px]"
value={r.phone}
onChange={(e) => updateRow(idx, { phone: e.target.value })}
/>
</td>
<td className="border p-1 align-top min-w-[140px]">
<Select value={r.roleId || "__none__"} onValueChange={(v) => updateRow(idx, { roleId: v === "__none__" ? "" : v })}>
<SelectTrigger className="h-7 text-xs">
<SelectValue placeholder="Role" />
</SelectTrigger>
<SelectContent>
<SelectItem value="__none__">(select)</SelectItem>
{roles.map((role) => (
<SelectItem key={role.id} value={role.id}>
{role.roleName ?? role.id}
</SelectItem>
))}
</SelectContent>
</Select>
</td>
<td className="border p-1 align-top">
<Input
className="h-7 text-xs min-w-[140px]"
value={r.locationIdsCsv}
onChange={(e) => updateRow(idx, { locationIdsCsv: e.target.value })}
placeholder="guid1,guid2"
/>
</td>
<td className="border p-1 text-center align-middle">
<Switch checked={r.state !== false} onCheckedChange={(c) => updateRow(idx, { state: !!c })} />
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="px-4 py-3 border-t border-gray-100 text-center text-xs text-gray-500 shrink-0 space-y-1">
<p>Leave password empty to keep the current password.</p>
<p>Location IDs: comma-separated location primary keys.</p>
</div>
</div>
);
}