refactor source code
This commit is contained in:
@@ -138,12 +138,12 @@ export async function PUT(
|
||||
parseInt(dependant.id) :
|
||||
getNextId(db.data!.customerDependants),
|
||||
custId: customerId,
|
||||
firstNameEn: dependant.firstNameEn,
|
||||
lastNameEn: dependant.lastNameEn,
|
||||
originAdd1: dependant.originAdd1,
|
||||
localAdd1: dependant.localAdd1,
|
||||
email: dependant.email,
|
||||
mobile: dependant.mobile,
|
||||
firstNameEn: dependant.dependantInfo.firstNameEn,
|
||||
lastNameEn: dependant.dependantInfo.lastNameEn,
|
||||
originAdd1: dependant.dependantInfo.originAdd1,
|
||||
localAdd1: dependant.dependantInfo.localAdd1,
|
||||
email: dependant.dependantContact.email,
|
||||
mobile: dependant.dependantContact.mobile,
|
||||
};
|
||||
newDependants.push(newDependant);
|
||||
db.data!.customerDependants.push(newDependant);
|
||||
|
||||
@@ -36,12 +36,12 @@ export async function POST(request: NextRequest) {
|
||||
const newDependant: CustomerDependant = {
|
||||
id: getNextId(db.data!.customerDependants),
|
||||
custId: newCustomer.id,
|
||||
firstNameEn: dependant.firstNameEn,
|
||||
lastNameEn: dependant.lastNameEn,
|
||||
email: dependant.email,
|
||||
mobile: dependant.mobile,
|
||||
originAdd1: dependant.originAdd1,
|
||||
localAdd1: dependant.localAdd1,
|
||||
firstNameEn: dependant.dependantInfo.firstNameEn,
|
||||
lastNameEn: dependant.dependantInfo.lastNameEn,
|
||||
email: dependant.dependantContact.email,
|
||||
mobile: dependant.dependantContact.mobile,
|
||||
originAdd1: dependant.dependantInfo.originAdd1,
|
||||
localAdd1: dependant.dependantInfo.localAdd1,
|
||||
};
|
||||
customerDependants.push(newDependant);
|
||||
db.data!.customerDependants.push(newDependant);
|
||||
|
||||
@@ -18,7 +18,6 @@ import {
|
||||
customerDependantFormSchema,
|
||||
type CustomerForm,
|
||||
type CustomerDependantForm,
|
||||
type CustomerDependant,
|
||||
} from "@/schemas/customer.schema";
|
||||
import axios from "axios";
|
||||
import { Header } from "@/components/common/header";
|
||||
@@ -33,7 +32,7 @@ export default function CustomerEditPage() {
|
||||
const [dependantDialogTab, setDependantDialogTab] = useState("info");
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [editingDependant, setEditingDependant] = useState<CustomerDependant | null>(null);
|
||||
const [editingDependant, setEditingDependant] = useState<CustomerDependantForm | null>(null);
|
||||
|
||||
// Main form
|
||||
const form = useForm<CustomerForm>({
|
||||
@@ -91,7 +90,7 @@ export default function CustomerEditPage() {
|
||||
form.setValue("customerContact.mobile", customerData.mobile);
|
||||
|
||||
// Convert dependants to the expected format
|
||||
const dependants: CustomerDependant[] = customerData.dependants.map((dep: {
|
||||
const dependants: CustomerDependantForm[] = customerData.dependants.map((dep: {
|
||||
id: number;
|
||||
firstNameEn: string;
|
||||
lastNameEn: string;
|
||||
@@ -101,12 +100,16 @@ export default function CustomerEditPage() {
|
||||
localAdd1: string;
|
||||
}) => ({
|
||||
id: dep.id,
|
||||
firstNameEn: dep.firstNameEn,
|
||||
lastNameEn: dep.lastNameEn,
|
||||
email: dep.email,
|
||||
mobile: dep.mobile,
|
||||
originAdd1: dep.originAdd1,
|
||||
localAdd1: dep.localAdd1,
|
||||
dependantInfo: {
|
||||
firstNameEn: dep.firstNameEn,
|
||||
lastNameEn: dep.lastNameEn,
|
||||
originAdd1: dep.originAdd1,
|
||||
localAdd1: dep.localAdd1,
|
||||
},
|
||||
dependantContact: {
|
||||
email: dep.email,
|
||||
mobile: dep.mobile,
|
||||
},
|
||||
}));
|
||||
|
||||
form.setValue("customerDependants", dependants);
|
||||
@@ -183,14 +186,18 @@ export default function CustomerEditPage() {
|
||||
toast.success("Dependant updated successfully!");
|
||||
} else {
|
||||
// Add new dependant - just add to the table, don't submit parent form
|
||||
const newDependant: CustomerDependant = {
|
||||
const newDependant: CustomerDependantForm = {
|
||||
id: Date.now(),
|
||||
firstNameEn: data.dependantInfo.firstNameEn,
|
||||
lastNameEn: data.dependantInfo.lastNameEn,
|
||||
originAdd1: data.dependantInfo.originAdd1,
|
||||
localAdd1: data.dependantInfo.localAdd1,
|
||||
email: data.dependantContact.email,
|
||||
mobile: data.dependantContact.mobile,
|
||||
dependantInfo: {
|
||||
firstNameEn: data.dependantInfo.firstNameEn,
|
||||
lastNameEn: data.dependantInfo.lastNameEn,
|
||||
originAdd1: data.dependantInfo.originAdd1,
|
||||
localAdd1: data.dependantInfo.localAdd1,
|
||||
},
|
||||
dependantContact: {
|
||||
email: data.dependantContact.email,
|
||||
mobile: data.dependantContact.mobile,
|
||||
},
|
||||
};
|
||||
|
||||
// Add new row to dependant table without submitting parent form
|
||||
@@ -209,14 +216,14 @@ export default function CustomerEditPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditDependant = (dependant: CustomerDependant) => {
|
||||
const handleEditDependant = (dependant: CustomerDependantForm) => {
|
||||
setEditingDependant(dependant);
|
||||
dependantForm.setValue("dependantInfo.firstNameEn", dependant.firstNameEn);
|
||||
dependantForm.setValue("dependantInfo.lastNameEn", dependant.lastNameEn);
|
||||
dependantForm.setValue("dependantInfo.originAdd1", dependant.originAdd1);
|
||||
dependantForm.setValue("dependantInfo.localAdd1", dependant.localAdd1);
|
||||
dependantForm.setValue("dependantContact.email", dependant.email);
|
||||
dependantForm.setValue("dependantContact.mobile", dependant.mobile);
|
||||
dependantForm.setValue("dependantInfo.firstNameEn", dependant.dependantInfo.firstNameEn);
|
||||
dependantForm.setValue("dependantInfo.lastNameEn", dependant.dependantInfo.lastNameEn);
|
||||
dependantForm.setValue("dependantInfo.originAdd1", dependant.dependantInfo.originAdd1);
|
||||
dependantForm.setValue("dependantInfo.localAdd1", dependant.dependantInfo.localAdd1);
|
||||
dependantForm.setValue("dependantContact.email", dependant.dependantContact.email);
|
||||
dependantForm.setValue("dependantContact.mobile", dependant.dependantContact.mobile);
|
||||
setDependantDialogOpen(true);
|
||||
setDependantDialogTab("info");
|
||||
};
|
||||
@@ -575,12 +582,12 @@ export default function CustomerEditPage() {
|
||||
{watchedDependants.map((dependant) => (
|
||||
<TableRow key={dependant.id}>
|
||||
<TableCell>
|
||||
{dependant.firstNameEn} {dependant.lastNameEn}
|
||||
{dependant.dependantInfo.firstNameEn} {dependant.dependantInfo.lastNameEn}
|
||||
</TableCell>
|
||||
<TableCell>{dependant.email}</TableCell>
|
||||
<TableCell>{dependant.mobile}</TableCell>
|
||||
<TableCell>{dependant.originAdd1}</TableCell>
|
||||
<TableCell>{dependant.localAdd1}</TableCell>
|
||||
<TableCell>{dependant.dependantContact.email}</TableCell>
|
||||
<TableCell>{dependant.dependantContact.mobile}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.originAdd1}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.localAdd1}</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex space-x-2">
|
||||
<Button
|
||||
|
||||
@@ -18,7 +18,6 @@ import {
|
||||
customerDependantFormSchema,
|
||||
type CustomerForm,
|
||||
type CustomerDependantForm,
|
||||
type CustomerDependant,
|
||||
} from "@/schemas/customer.schema";
|
||||
import axios from "axios";
|
||||
import { Header } from "@/components/common/header";
|
||||
@@ -79,6 +78,9 @@ export default function CustomerAddPage() {
|
||||
|
||||
// Reset form after successful submission
|
||||
form.reset();
|
||||
|
||||
// Redirect to customer page
|
||||
router.push("/modules/customer");
|
||||
} catch (error) {
|
||||
console.error("Error submitting form:", error);
|
||||
toast.error("Failed to add customer. Please try again.");
|
||||
@@ -95,14 +97,18 @@ export default function CustomerAddPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
const newDependant: CustomerDependant = {
|
||||
const newDependant: CustomerDependantForm = {
|
||||
id: Date.now(), // Generate unique ID
|
||||
firstNameEn: data.dependantInfo.firstNameEn,
|
||||
lastNameEn: data.dependantInfo.lastNameEn,
|
||||
email: data.dependantContact.email,
|
||||
mobile: data.dependantContact.mobile,
|
||||
originAdd1: data.dependantInfo.originAdd1,
|
||||
localAdd1: data.dependantInfo.localAdd1,
|
||||
dependantInfo: {
|
||||
firstNameEn: data.dependantInfo.firstNameEn,
|
||||
lastNameEn: data.dependantInfo.lastNameEn,
|
||||
originAdd1: data.dependantInfo.originAdd1,
|
||||
localAdd1: data.dependantInfo.localAdd1,
|
||||
},
|
||||
dependantContact: {
|
||||
email: data.dependantContact.email,
|
||||
mobile: data.dependantContact.mobile,
|
||||
},
|
||||
};
|
||||
|
||||
const currentDependants = form.getValues("customerDependants");
|
||||
@@ -534,12 +540,12 @@ export default function CustomerAddPage() {
|
||||
<TableBody>
|
||||
{watchedDependants.map((dependant) => (
|
||||
<TableRow key={dependant.id}>
|
||||
<TableCell>{dependant.firstNameEn}</TableCell>
|
||||
<TableCell>{dependant.lastNameEn}</TableCell>
|
||||
<TableCell>{dependant.email}</TableCell>
|
||||
<TableCell>{dependant.mobile}</TableCell>
|
||||
<TableCell>{dependant.originAdd1}</TableCell>
|
||||
<TableCell>{dependant.localAdd1}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.firstNameEn}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.lastNameEn}</TableCell>
|
||||
<TableCell>{dependant.dependantContact.email}</TableCell>
|
||||
<TableCell>{dependant.dependantContact.mobile}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.originAdd1}</TableCell>
|
||||
<TableCell>{dependant.dependantInfo.localAdd1}</TableCell>
|
||||
<TableCell>
|
||||
<Button
|
||||
type="button"
|
||||
|
||||
@@ -102,24 +102,16 @@ export default function UserPermissionPage() {
|
||||
try {
|
||||
setSaving(true);
|
||||
|
||||
const response = await fetch(`/api/user/${userId}/permissions`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
permissionIds: Array.from(selectedPermissions),
|
||||
}),
|
||||
const axios = (await import("axios")).default;
|
||||
|
||||
const response = await axios.put(`/api/user/${userId}/permissions`, {
|
||||
permissionIds: Array.from(selectedPermissions),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to update permissions");
|
||||
}
|
||||
|
||||
const updatedPermissions = await response.json();
|
||||
const updatedPermissions = response.data;
|
||||
setUserPermissions(updatedPermissions);
|
||||
toast.success("User permissions updated successfully!");
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error("Error updating permissions:", error);
|
||||
toast.error("Failed to update permissions");
|
||||
} finally {
|
||||
|
||||
@@ -52,7 +52,7 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
|
||||
<GalleryVerticalEnd className="size-4" />
|
||||
</div>
|
||||
<div className="flex flex-col gap-0.5 leading-none">
|
||||
<span className="font-medium">Documentation</span>
|
||||
<span className="font-medium">Cosmos Plan Prototype</span>
|
||||
<span className="">v1.0.0</span>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ShieldCheck } from "lucide-react";
|
||||
import { Key } from "lucide-react";
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
@@ -48,7 +48,7 @@ export function createUserColumns({ onEdit, onDelete, onPermissions }: {
|
||||
<div className="flex gap-2">
|
||||
{onPermissions && (
|
||||
<Button size="sm" variant="outline" onClick={() => onPermissions(row.row.original.id)}>
|
||||
<ShieldCheck className="h-4 w-4 mr-1" />
|
||||
<Key className="h-4 w-4 mr-1" />
|
||||
Permissions
|
||||
</Button>
|
||||
)}
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
{
|
||||
"customers": [],
|
||||
"customers": [
|
||||
{
|
||||
"id": 1,
|
||||
"firstNameEn": "Đỗ",
|
||||
"lastNameEn": "Tùng",
|
||||
"email": "dothanhtung196@gmail.com",
|
||||
"mobile": "0987417491",
|
||||
"originAdd1": "1231231231",
|
||||
"localAdd1": "sdfgsdfg"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"firstNameEn": "Đỗ",
|
||||
"lastNameEn": "Tùng",
|
||||
"email": "dothanhtung196@gmail.com",
|
||||
"mobile": "0987417491",
|
||||
"originAdd1": "1231231231",
|
||||
"localAdd1": "sdfgsdfg"
|
||||
}
|
||||
],
|
||||
"users": [
|
||||
{
|
||||
"id": 1,
|
||||
@@ -494,5 +513,36 @@
|
||||
"isActive": true
|
||||
}
|
||||
],
|
||||
"customerDependants": []
|
||||
"customerDependants": [
|
||||
{
|
||||
"id": 2,
|
||||
"custId": 2,
|
||||
"firstNameEn": "Đỗ",
|
||||
"lastNameEn": "Tùng",
|
||||
"email": "dothanhtung196@gmail.com",
|
||||
"mobile": "0987417491",
|
||||
"originAdd1": "1231231231",
|
||||
"localAdd1": "sdfgsdfg"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"custId": 1,
|
||||
"firstNameEn": "Đỗ",
|
||||
"lastNameEn": "Tùng",
|
||||
"originAdd1": "1231231231",
|
||||
"localAdd1": "sdfgsdfg",
|
||||
"email": "dothanhtung196@gmail.com",
|
||||
"mobile": "0987417491"
|
||||
},
|
||||
{
|
||||
"id": 1751854039116,
|
||||
"custId": 1,
|
||||
"firstNameEn": "Đỗ",
|
||||
"lastNameEn": "Thanh Tùng",
|
||||
"originAdd1": "123",
|
||||
"localAdd1": "111",
|
||||
"email": "dothanhtung196@gmail.com",
|
||||
"mobile": "0987417491"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -24,29 +24,17 @@ export const customerDependantContactSchema = z.object({
|
||||
mobile: z.string().min(1, "Mobile number is required"),
|
||||
});
|
||||
|
||||
export const customerDependantSchema = z.object({
|
||||
export const customerDependantFormSchema = z.object({
|
||||
id: z.number().optional(),
|
||||
firstNameEn: z.string(),
|
||||
lastNameEn: z.string(),
|
||||
email: z.string(),
|
||||
mobile: z.string(),
|
||||
originAdd1: z.string(),
|
||||
localAdd1: z.string(),
|
||||
dependantInfo: customerDependantInfoSchema,
|
||||
dependantContact: customerDependantContactSchema,
|
||||
});
|
||||
|
||||
export const customerFormSchema = z.object({
|
||||
customerInfo: customerInfoSchema,
|
||||
customerContact: customerContactSchema,
|
||||
customerDependants: z.array(customerDependantSchema),
|
||||
customerDependants: z.array(customerDependantFormSchema),
|
||||
});
|
||||
|
||||
export const customerDependantFormSchema = z.object({
|
||||
dependantInfo: customerDependantInfoSchema,
|
||||
dependantContact: customerDependantContactSchema,
|
||||
});
|
||||
|
||||
export type CustomerInfo = z.infer<typeof customerInfoSchema>;
|
||||
export type CustomerContact = z.infer<typeof customerContactSchema>;
|
||||
export type CustomerDependant = z.infer<typeof customerDependantSchema>;
|
||||
export type CustomerForm = z.infer<typeof customerFormSchema>;
|
||||
export type CustomerDependantForm = z.infer<typeof customerDependantFormSchema>;
|
||||
|
||||
Reference in New Issue
Block a user