251209:1453 Frontend: progress nest = UAT & Bug Fixing
This commit is contained in:
@@ -13,9 +13,17 @@ import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { useCreateUser, useUpdateUser } from "@/hooks/use-users";
|
||||
import { useCreateUser, useUpdateUser, useRoles } from "@/hooks/use-users";
|
||||
import { useOrganizations } from "@/hooks/use-master-data";
|
||||
import { useEffect } from "react";
|
||||
import { User } from "@/types/user";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
|
||||
const userSchema = z.object({
|
||||
username: z.string().min(3),
|
||||
@@ -24,7 +32,9 @@ const userSchema = z.object({
|
||||
last_name: z.string().min(1),
|
||||
password: z.string().min(6).optional(),
|
||||
is_active: z.boolean().default(true),
|
||||
role_ids: z.array(z.number()).default([]), // Using role_ids array
|
||||
line_id: z.string().optional(),
|
||||
primary_organization_id: z.coerce.number().optional(),
|
||||
role_ids: z.array(z.number()).default([]),
|
||||
});
|
||||
|
||||
type UserFormData = z.infer<typeof userSchema>;
|
||||
@@ -38,6 +48,8 @@ interface UserDialogProps {
|
||||
export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
const createUser = useCreateUser();
|
||||
const updateUser = useUpdateUser();
|
||||
const { data: roles = [] } = useRoles();
|
||||
const { data: organizations = [] } = useOrganizations();
|
||||
|
||||
const {
|
||||
register,
|
||||
@@ -47,53 +59,65 @@ export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
reset,
|
||||
formState: { errors },
|
||||
} = useForm<UserFormData>({
|
||||
resolver: zodResolver(userSchema),
|
||||
resolver: zodResolver(userSchema) as any,
|
||||
defaultValues: {
|
||||
is_active: true,
|
||||
role_ids: []
|
||||
}
|
||||
username: "",
|
||||
email: "",
|
||||
first_name: "",
|
||||
last_name: "",
|
||||
is_active: true,
|
||||
role_ids: [] as number[],
|
||||
line_id: "",
|
||||
primary_organization_id: undefined as number | undefined,
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
reset({
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
is_active: user.is_active,
|
||||
role_ids: user.roles?.map(r => r.role_id) || []
|
||||
});
|
||||
reset({
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
is_active: user.is_active,
|
||||
line_id: user.line_id || "",
|
||||
primary_organization_id: user.primary_organization_id,
|
||||
role_ids: user.roles?.map((r: any) => r.roleId) || [],
|
||||
});
|
||||
} else {
|
||||
reset({
|
||||
username: "",
|
||||
email: "",
|
||||
first_name: "",
|
||||
last_name: "",
|
||||
is_active: true,
|
||||
role_ids: []
|
||||
});
|
||||
reset({
|
||||
username: "",
|
||||
email: "",
|
||||
first_name: "",
|
||||
last_name: "",
|
||||
is_active: true,
|
||||
line_id: "",
|
||||
primary_organization_id: undefined,
|
||||
role_ids: [],
|
||||
});
|
||||
}
|
||||
}, [user, reset, open]); // Reset when open changes or user changes
|
||||
|
||||
const availableRoles = [
|
||||
{ role_id: 1, role_name: "ADMIN", description: "System Administrator" },
|
||||
{ role_id: 2, role_name: "USER", description: "Regular User" },
|
||||
{ role_id: 3, role_name: "APPROVER", description: "Document Approver" },
|
||||
];
|
||||
}, [user, reset, open]);
|
||||
|
||||
const selectedRoleIds = watch("role_ids") || [];
|
||||
|
||||
const onSubmit = (data: UserFormData) => {
|
||||
// If password is empty (and editing), exclude it
|
||||
if (user && !data.password) {
|
||||
delete data.password;
|
||||
}
|
||||
|
||||
if (user) {
|
||||
updateUser.mutate({ id: user.user_id, data }, {
|
||||
onSuccess: () => onOpenChange(false)
|
||||
});
|
||||
updateUser.mutate(
|
||||
{ id: user.user_id, data },
|
||||
{
|
||||
onSuccess: () => onOpenChange(false),
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
createUser.mutate(data as any, {
|
||||
onSuccess: () => onOpenChange(false)
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
createUser.mutate(data as any, {
|
||||
onSuccess: () => onOpenChange(false),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -109,13 +133,17 @@ export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
<div>
|
||||
<Label>Username *</Label>
|
||||
<Input {...register("username")} disabled={!!user} />
|
||||
{errors.username && <p className="text-sm text-red-500">{errors.username.message}</p>}
|
||||
{errors.username && (
|
||||
<p className="text-sm text-red-500">{errors.username.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Email *</Label>
|
||||
<Input type="email" {...register("email")} />
|
||||
{errors.email && <p className="text-sm text-red-500">{errors.email.message}</p>}
|
||||
{errors.email && (
|
||||
<p className="text-sm text-red-500">{errors.email.message}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -131,37 +159,76 @@ export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<Label>Line ID</Label>
|
||||
<Input {...register("line_id")} />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label>Primary Organization</Label>
|
||||
<Select
|
||||
value={watch("primary_organization_id")?.toString()}
|
||||
onValueChange={(val) =>
|
||||
setValue("primary_organization_id", parseInt(val))
|
||||
}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select Organization" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{organizations?.map((org: any) => (
|
||||
<SelectItem
|
||||
key={org.id}
|
||||
value={org.id.toString()}
|
||||
>
|
||||
{org.organizationCode} - {org.organizationName}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!user && (
|
||||
<div>
|
||||
<Label>Password *</Label>
|
||||
<Input type="password" {...register("password")} />
|
||||
{errors.password && <p className="text-sm text-red-500">{errors.password.message}</p>}
|
||||
{errors.password && (
|
||||
<p className="text-sm text-red-500">
|
||||
{errors.password.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<Label className="mb-3 block">Roles</Label>
|
||||
<div className="space-y-2 border p-3 rounded-md">
|
||||
{availableRoles.map((role) => (
|
||||
<div key={role.role_id} className="flex items-start space-x-2">
|
||||
<div className="space-y-2 border p-3 rounded-md max-h-[200px] overflow-y-auto">
|
||||
{roles.length === 0 && <p className="text-sm text-muted-foreground">Loading roles...</p>}
|
||||
{roles.map((role: any) => (
|
||||
<div key={role.roleId} className="flex items-start space-x-2">
|
||||
<Checkbox
|
||||
id={`role-${role.role_id}`}
|
||||
checked={selectedRoleIds.includes(role.role_id)}
|
||||
id={`role-${role.roleId}`}
|
||||
checked={selectedRoleIds.includes(role.roleId)}
|
||||
onCheckedChange={(checked) => {
|
||||
const current = selectedRoleIds;
|
||||
if (checked) {
|
||||
setValue("role_ids", [...current, role.role_id]);
|
||||
setValue("role_ids", [...current, role.roleId]);
|
||||
} else {
|
||||
setValue("role_ids", current.filter(id => id !== role.role_id));
|
||||
setValue(
|
||||
"role_ids",
|
||||
current.filter((id) => id !== role.roleId)
|
||||
);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div className="grid gap-1.5 leading-none">
|
||||
<label
|
||||
htmlFor={`role-${role.role_id}`}
|
||||
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||
htmlFor={`role-${role.roleId}`}
|
||||
className="text-sm font-medium leading-none cursor-pointer"
|
||||
>
|
||||
{role.role_name}
|
||||
{role.roleName}
|
||||
</label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{role.description}
|
||||
@@ -174,17 +241,17 @@ export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
|
||||
{user && (
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id="is_active"
|
||||
checked={watch("is_active")}
|
||||
onCheckedChange={(chk) => setValue("is_active", chk === true)}
|
||||
/>
|
||||
<label
|
||||
htmlFor="is_active"
|
||||
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||
>
|
||||
Active User
|
||||
</label>
|
||||
<Checkbox
|
||||
id="is_active"
|
||||
checked={watch("is_active")}
|
||||
onCheckedChange={(chk) => setValue("is_active", chk === true)}
|
||||
/>
|
||||
<label
|
||||
htmlFor="is_active"
|
||||
className="text-sm font-medium leading-none cursor-pointer"
|
||||
>
|
||||
Active User
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -196,7 +263,10 @@ export function UserDialog({ open, onOpenChange, user }: UserDialogProps) {
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={createUser.isPending || updateUser.isPending}>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={createUser.isPending || updateUser.isPending}
|
||||
>
|
||||
{user ? "Update User" : "Create User"}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user