191 lines
5.5 KiB
TypeScript
191 lines
5.5 KiB
TypeScript
import { Button } from "@nextui-org/button";
|
|
import { Input, Textarea } from "@nextui-org/input";
|
|
import React, { useState } from "react";
|
|
import { Controller, useForm } from "react-hook-form";
|
|
import * as z from "zod";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { otpRequest, otpValidation } from "@/service/master-user";
|
|
import { error } from "@/config/swal";
|
|
|
|
const commentSchema = z.object({
|
|
name: z.string().min(1, {
|
|
message: "Judul harus diisi",
|
|
}),
|
|
email: z
|
|
.string()
|
|
.email({
|
|
message: "Email tidak valid",
|
|
})
|
|
.min(1, {
|
|
message: "Harus diisi",
|
|
}),
|
|
comment: z.string().min(1, {
|
|
message: "Deskripsi harus diisi",
|
|
}),
|
|
});
|
|
|
|
export default function Comment() {
|
|
const [needOtp, setNeedOtp] = useState(false);
|
|
const [otpValue, setOtpValue] = useState("");
|
|
const formOptions = {
|
|
resolver: zodResolver(commentSchema),
|
|
};
|
|
type UserSettingSchema = z.infer<typeof commentSchema>;
|
|
const {
|
|
control,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
setValue,
|
|
} = useForm<UserSettingSchema>(formOptions);
|
|
|
|
const onSubmit = async (values: z.infer<typeof commentSchema>) => {
|
|
if (!needOtp) {
|
|
const res = await otpRequest(values.email);
|
|
if (res?.error) {
|
|
error(res.message);
|
|
return false;
|
|
}
|
|
setNeedOtp(true);
|
|
} else {
|
|
const validation = await otpValidation(values.email, otpValue);
|
|
if (validation?.error) {
|
|
error("OTP Tidak Sesuai");
|
|
return false;
|
|
}
|
|
const req = {
|
|
email: values.email,
|
|
name: values.name,
|
|
comment: values.comment,
|
|
};
|
|
console.log("req", req);
|
|
}
|
|
};
|
|
return (
|
|
<form className="p-3 flex flex-col gap-3" onSubmit={handleSubmit(onSubmit)}>
|
|
<b>Tinggalkan balasan</b>
|
|
<p className="text-xs">
|
|
Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai{" "}
|
|
<span className="text-red-600">*</span>
|
|
</p>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">Komentar</p>
|
|
<Controller
|
|
control={control}
|
|
name="comment"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Textarea
|
|
type="text"
|
|
id="comment"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.comment && (
|
|
<p className="text-red-400 text-sm mb-3">{errors.comment?.message}</p>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">
|
|
Nama <span className="text-red-600">*</span>
|
|
</p>
|
|
|
|
<Controller
|
|
control={control}
|
|
name="name"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Input
|
|
type="text"
|
|
id="name"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.name && (
|
|
<p className="text-red-400 text-sm mb-3">{errors.name?.message}</p>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<p className="text-sm">
|
|
Email <span className="text-red-600">*</span>
|
|
</p>
|
|
<Controller
|
|
control={control}
|
|
name="email"
|
|
render={({ field: { onChange, value } }) => (
|
|
<Input
|
|
type="email"
|
|
id="email"
|
|
placeholder=""
|
|
label=""
|
|
value={value}
|
|
onChange={onChange}
|
|
labelPlacement="outside"
|
|
className="w-full "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors?.email && (
|
|
<p className="text-red-400 text-sm mb-3">{errors.email?.message}</p>
|
|
)}
|
|
</div>
|
|
{needOtp && (
|
|
<div className="flex flex-col gap-1">
|
|
<p>OTP</p>
|
|
<Input
|
|
type="number"
|
|
id="otp"
|
|
placeholder=""
|
|
label=""
|
|
value={otpValue}
|
|
onValueChange={setOtpValue}
|
|
labelPlacement="outside"
|
|
className="w-[100px] "
|
|
classNames={{
|
|
inputWrapper: [
|
|
"border-1 rounded-lg",
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
],
|
|
}}
|
|
variant="bordered"
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
<Button className="bg-[#DD8306] text-white" radius="md" type="submit">
|
|
Kirim
|
|
</Button>
|
|
</form>
|
|
);
|
|
}
|