Skip to content

Commit

Permalink
Merge pull request #159 from kimthu09/anhthu
Browse files Browse the repository at this point in the history
fix: number format
  • Loading branch information
NLNM-0-0 authored Jan 16, 2024
2 parents 766f340 + d005bcc commit 6c8c71c
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 71 deletions.
57 changes: 47 additions & 10 deletions frontend/app/product/books/[bookId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { Label } from "@/components/ui/label";
import { useToast } from "@/components/ui/use-toast";
import Link from "next/link";
import { useEffect, useState } from "react";
import { SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form";
import {
Controller,
SubmitErrorHandler,
SubmitHandler,
useForm,
} from "react-hook-form";
import { LuCheck } from "react-icons/lu";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
Expand All @@ -38,6 +43,7 @@ import { BookTitle } from "@/types";
import BookTitleSelectEdit from "@/components/book-manage/book-title-select-edit";
import { useLoading } from "@/hooks/loading-context";
import BookDetailSkeleton from "@/components/skeleton/book-detail-skeleton";
import { NumericFormat } from "react-number-format";

const FormSchema = z.object({
bookTitleId: z.string().min(1, "Vui lòng chọn một đầu sách"),
Expand Down Expand Up @@ -194,7 +200,7 @@ const EditBook = ({ params }: { params: { bookId: string } }) => {

const { currentUser } = useCurrentUser();
if (!currentUser || isLoading) {
return <BookDetailSkeleton/>;
return <BookDetailSkeleton />;
} else if (
currentUser &&
!includesRoles({
Expand Down Expand Up @@ -335,10 +341,26 @@ const EditBook = ({ params }: { params: { bookId: string } }) => {
<div className="flex lg:gap-4 gap-3 xl:flex-col sm:flex-row flex-col">
<div className="flex-1">
<Label>Giá niêm yết (VNĐ)</Label>
<Input
type="number"
readOnly={readOnly}
{...register("listedPrice")}
<Controller
name="listedPrice"
control={control}
render={({ field }) => (
<NumericFormat
value={field.value}
onValueChange={(values) => {
const numericValue = parseFloat(
values.value.replace(/,/g, "")
);

field.onChange(numericValue);
}}
readOnly={readOnly}
thousandSeparator="."
decimalSeparator=","
valueIsNumericString
customInput={Input}
/>
)}
/>
{errors.listedPrice && (
<span className="error___message">
Expand All @@ -348,10 +370,25 @@ const EditBook = ({ params }: { params: { bookId: string } }) => {
</div>
<div className="flex-1">
<Label>Đơn giá (VNĐ)</Label>
<Input
type="number"
{...register("sellPrice")}
readOnly={readOnly}
<Controller
name="sellPrice"
control={control}
render={({ field }) => (
<NumericFormat
value={field.value}
onValueChange={(values) => {
const numericValue = parseFloat(
values.value.replace(/,/g, "")
);
field.onChange(numericValue);
}}
readOnly={readOnly}
thousandSeparator="."
decimalSeparator=","
valueIsNumericString
customInput={Input}
/>
)}
/>
{errors.sellPrice && (
<span className="error___message">
Expand Down
60 changes: 53 additions & 7 deletions frontend/app/product/books/add/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { Label } from "@/components/ui/label";
import { useToast } from "@/components/ui/use-toast";
import Link from "next/link";
import { useState } from "react";
import { SubmitHandler, useFieldArray, useForm } from "react-hook-form";
import {
Controller,
SubmitHandler,
useFieldArray,
useForm,
} from "react-hook-form";
import { LuCheck } from "react-icons/lu";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
Expand All @@ -32,6 +37,7 @@ import { includesRoles } from "@/lib/utils";
import NoRole from "@/components/no-role";
import { useLoading } from "@/hooks/loading-context";
import BookAddSkeleton from "@/components/skeleton/book-add-skeleton";
import { NumericFormat } from "react-number-format";

const FormSchema = z.object({
bookTitleId: z.string().min(1, "Vui lòng chọn một đầu sách"),
Expand Down Expand Up @@ -80,6 +86,7 @@ const InsertNewBook = () => {
setValue,
reset,
trigger,
clearErrors,
formState: { errors },
} = form;

Expand Down Expand Up @@ -139,20 +146,23 @@ const InsertNewBook = () => {
title: "Thành công",
description: "Thêm mới sách thành công",
});
router.refresh();

setPublisherId("");

reset({
bookTitleId: data.bookTitleId,
idBook: "",
edition: 1,
publisherId: "",
listedPrice: 0,
sellPrice: 0,
listedPrice: 1,
sellPrice: 1,
image: "/no-image.jpg",
});
setPublisherId("");
clearErrors();
setImage(null);

mutate(`${endPoint}/v1/books/all`);
router.refresh();
}
};

Expand Down Expand Up @@ -264,7 +274,25 @@ const InsertNewBook = () => {
<div className="flex lg:gap-4 gap-3 xl:flex-col sm:flex-row flex-col">
<div className="flex-1">
<Label>Giá niêm yết (VNĐ)</Label>
<Input {...register("listedPrice")} />
<Controller
name="listedPrice"
control={control}
render={({ field }) => (
<NumericFormat
value={field.value}
onValueChange={(values) => {
const numericValue = parseFloat(
values.value.replace(/,/g, "")
);
field.onChange(numericValue);
}}
thousandSeparator="."
decimalSeparator=","
valueIsNumericString
customInput={Input}
/>
)}
/>
{errors.listedPrice && (
<span className="error___message">
{errors.listedPrice.message}
Expand All @@ -273,7 +301,25 @@ const InsertNewBook = () => {
</div>
<div className="flex-1">
<Label>Đơn giá (VNĐ)</Label>
<Input {...register("sellPrice")} />
<Controller
name="sellPrice"
control={control}
render={({ field }) => (
<NumericFormat
value={field.value}
onValueChange={(values) => {
const numericValue = parseFloat(
values.value.replace(/,/g, "")
);
field.onChange(numericValue);
}}
thousandSeparator="."
decimalSeparator=","
valueIsNumericString
customInput={Input}
/>
)}
/>
{errors.sellPrice && (
<span className="error___message">
{errors.sellPrice.message}
Expand Down
7 changes: 7 additions & 0 deletions frontend/app/staff/[staffId]/detail-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { useCurrentUser } from "@/hooks/use-user";
import { includesRoles, isAdmin } from "@/lib/utils";
import { useLoading } from "@/hooks/loading-context";
import StaffDetailSkeleton from "@/components/skeleton/staff-detail";
import { useRouter } from "next/navigation";

const FormSchema = z.object({
name: required,
Expand All @@ -50,6 +51,7 @@ const PasswordSchema = z.object({
});

const EditStaff = ({ params }: { params: { staffId: string } }) => {
const router = useRouter();
const [role, setRole] = useState("");
const { showLoading, hideLoading } = useLoading();

Expand Down Expand Up @@ -134,6 +136,7 @@ const EditStaff = ({ params }: { params: { staffId: string } }) => {
description: "Đặt lại mật khẩu nhân viên thành công",
});
setOpen(false);
router.refresh();
}
};
const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (data) => {
Expand All @@ -160,6 +163,7 @@ const EditStaff = ({ params }: { params: { staffId: string } }) => {
description: "Chỉnh sửa thông tin nhân viên thành công",
});
mutate();
router.refresh();
}
};
const handleOpen = (value: boolean) => {
Expand Down Expand Up @@ -207,6 +211,7 @@ const EditStaff = ({ params }: { params: { staffId: string } }) => {
description: "Thay đổi ảnh nhân viên thành công",
});
mutate();
router.refresh();
}
}
};
Expand Down Expand Up @@ -248,6 +253,7 @@ const EditStaff = ({ params }: { params: { staffId: string } }) => {
});
setOpen(false);
mutate();
router.refresh();
}
};

Expand All @@ -272,6 +278,7 @@ const EditStaff = ({ params }: { params: { staffId: string } }) => {
});
setOpen(false);
mutate();
router.refresh();
}
};
const { currentUser } = useCurrentUser();
Expand Down
1 change: 1 addition & 0 deletions frontend/app/stockmanage/import/[importId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const ImportDetail = ({ params }: { params: { importId: string } }) => {
description: "Chuyển trạng thái thành công",
});
mutate();
router.refresh();
}
};
const { currentUser } = useCurrentUser();
Expand Down
61 changes: 49 additions & 12 deletions frontend/app/supplier/[supplierId]/detail-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Card, CardContent } from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useState } from "react";
import { ChangeEvent, forwardRef, useEffect, useRef, useState } from "react";
import { ImportTable } from "@/components/supplier-manage/import-table";
import { DebtTable } from "@/components/supplier-manage/debt-table";
import { Button } from "@/components/ui/button";
Expand All @@ -17,30 +17,41 @@ import {
import getSupplier from "@/lib/supplier/getSupplier";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { SubmitHandler, useForm } from "react-hook-form";
import { Controller, SubmitHandler, useForm } from "react-hook-form";
import { toast } from "@/components/ui/use-toast";
import paySupplier from "@/lib/supplier/paySupplier";
import EditDialog from "@/components/supplier-manage/edit";
import { useSWRConfig } from "swr";
import { endPoint } from "@/constants";
import { withAuth } from "@/lib/role/withAuth";
import { useCurrentUser } from "@/hooks/use-user";
import { includesRoles } from "@/lib/utils";
import NoRole from "@/components/no-role";
import { includesRoles, toVND } from "@/lib/utils";
import { useLoading } from "@/hooks/loading-context";
import { Skeleton } from "@/components/ui/skeleton";
import DropdownSkeleton from "@/components/skeleton/dropdown-skeleton";
import { useRouter } from "next/navigation";
import { NumericFormat } from "react-number-format";
import React from "react";

const FormSchema = z.object({
quantity: z.coerce.number().gte(1, "Giá trị phải lớn hơn 0"), // Force it to be a number
quantity: z.coerce
.number({ invalid_type_error: "Giá trị phải là một số" })
.gte(1, "Giá trị phải lớn hơn 0")
.refine((value) => Number.isInteger(value), {
message: "Giá trị phải là số nguyên",
}), // Force it to be a number
});

const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: { quantity: 0 },
});
const {
register,
handleSubmit,
control,
reset,
formState: { errors },
} = form;
const {
Expand All @@ -49,7 +60,9 @@ const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
isError,
mutate: mutateSupplier,
} = getSupplier(params.supplierId);
const inputRef = useRef<HTMLInputElement>(null);

const router = useRouter();
const { mutate } = useSWRConfig();
const { showLoading, hideLoading } = useLoading();
const onSubmit: SubmitHandler<z.infer<typeof FormSchema>> = async (
Expand All @@ -76,6 +89,7 @@ const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
});
mutate(`${endPoint}/v1/suppliers/${data.id}/debts?page=${pageIndex}`);
mutateSupplier();
router.refresh();
}
setOpenDialog(false);
};
Expand Down Expand Up @@ -169,7 +183,15 @@ const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
/>
) : null}

<Dialog open={openDialog} onOpenChange={setOpenDialog}>
<Dialog
open={openDialog}
onOpenChange={(open) => {
if (open) {
reset({ quantity: 0 });
}
setOpenDialog(open);
}}
>
<DialogTrigger asChild>
{!currentUser ||
(currentUser &&
Expand Down Expand Up @@ -207,11 +229,26 @@ const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
<div className="flex gap-4">
<div className="flex-1">
<Label htmlFor="price">Giá trị</Label>
<Input
id="price"
type="number"
{...register("quantity")}
></Input>
<Controller
name="quantity"
control={control}
render={({ field }) => (
<NumericFormat
value={field.value}
onValueChange={(values) => {
const numericValue = parseFloat(
values.value.replace(/,/g, "")
);
field.onChange(numericValue);
}}
thousandSeparator="."
decimalSeparator=","
valueIsNumericString
customInput={Input}
/>
)}
/>

{errors.quantity && (
<span className="error___message">
{errors.quantity.message}
Expand Down Expand Up @@ -255,7 +292,7 @@ const SupplierDetail = ({ params }: { params: { supplierId: string } }) => {
</div>
<div className="flex-1">
<Label>Nợ hiện tại</Label>
<Input type="number" value={data.debt} readOnly></Input>
<Input value={toVND(data.debt)} readOnly></Input>
</div>
</div>
</CardContent>
Expand Down
Loading

0 comments on commit 6c8c71c

Please sign in to comment.