mediahub-fe/app/[locale]/(public)/profile/change-password/page.tsx

155 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-12-26 11:29:22 +00:00
"use client";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { control } from "leaflet";
import React, { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import Swal from "sweetalert2";
import { getBlog, postBlog } from "@/service/blog/blog";
import { id } from "date-fns/locale";
import router from "next/router";
import { getInfoProfile, saveUser, setupPassword } from "@/service/auth";
import withReactContent from "sweetalert2-react-content";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
import { Link } from "@/components/navigation";
2025-02-15 14:43:19 +00:00
import { useTranslations } from "next-intl";
2024-12-26 11:29:22 +00:00
const profileSchema = z.object({
password: z.string().min(1, { message: "Judul diperlukan" }),
retypePassword: z.string().min(1, { message: "Judul diperlukan" }),
});
type Detail = {
id: number;
userId: any;
password: string;
retypePassword: string;
};
const ChangePassword: React.FC = () => {
const MySwal = withReactContent(Swal);
const [detail, setDetail] = useState<Detail>();
const [refresh, setRefresh] = useState(false);
2025-02-15 14:43:19 +00:00
const t = useTranslations("LandingPage");
2024-12-26 11:29:22 +00:00
type ProfileSchema = z.infer<typeof profileSchema>;
const {
control,
handleSubmit,
setValue,
formState: { errors },
} = useForm<ProfileSchema>({
resolver: zodResolver(profileSchema),
});
useEffect(() => {
async function initState() {
const response = await getInfoProfile();
2025-01-05 00:44:55 +00:00
const details = response?.data?.data;
2024-12-26 11:29:22 +00:00
setDetail(details);
console.log("data", details);
}
initState();
}, []);
const save = async (data: ProfileSchema) => {
const requestData = {
...data,
// userId: detail?.userKeycloakId,
password: data.password,
retypePassword: detail?.retypePassword,
};
const response = await setupPassword(requestData);
console.log("Form Data Submitted:", requestData);
console.log("response", response);
MySwal.fire({
title: "Sukses",
text: "Data berhasil disimpan.",
icon: "success",
confirmButtonColor: "#3085d6",
confirmButtonText: "OK",
}).then(() => {
router.push("/en/auth");
});
};
const onSubmit = (data: ProfileSchema) => {
MySwal.fire({
title: "Simpan Data",
text: "Apakah Anda yakin ingin menyimpan data ini?",
icon: "warning",
showCancelButton: true,
cancelButtonColor: "#d33",
confirmButtonColor: "#3085d6",
confirmButtonText: "Simpan",
}).then((result) => {
if (result.isConfirmed) {
save(data);
}
});
};
return (
<div className="px-4 lg:px-14 py-8">
<div className="text-center mb-8">
<div className="w-20 h-20 mx-auto rounded-full bg-red-700 flex items-center justify-center">
<span className="text-white text-3xl font-bold">👤</span>
</div>
2025-02-15 14:43:19 +00:00
<h1 className="text-2xl font-bold mt-4">{t("changePass")}</h1>
2024-12-26 11:29:22 +00:00
</div>
<div className="flex justify-center gap-4 mb-8">
<Link href={"/profile"}>
2025-02-15 14:43:19 +00:00
<button className="border border-red-700 text-red-700 px-4 py-2 rounded">{t("userProfile")}</button>
2024-12-26 11:29:22 +00:00
</Link>
<Link href={"/profile/change-profile"}>
2025-02-15 14:43:19 +00:00
<button className="border border-red-700 text-red-700 px-4 py-2 rounded">{t("changePhoto")}</button>
2024-12-26 11:29:22 +00:00
</Link>
<Link href={"/profile/change-password"}>
2025-02-15 14:43:19 +00:00
<button className="bg-red-700 text-white px-4 py-2 rounded">{t("changePass")}</button>
2024-12-26 11:29:22 +00:00
</Link>
</div>
<div className="max-w-3xl mx-auto bg-white shadow-sm p-4 rounded">
2025-02-15 14:43:19 +00:00
<p className="mb-6 text-gray-600">{t("pleaseChange")}</p>
2024-12-26 11:29:22 +00:00
<form onSubmit={handleSubmit(onSubmit)}>
<div>
<div className="mb-4">
<Label>
2025-02-15 14:43:19 +00:00
{t("password")}
<span className="text-red-500">*</span>
2024-12-26 11:29:22 +00:00
</Label>
2025-02-15 14:43:19 +00:00
<Controller control={control} name="password" render={({ field }) => <Input size="md" type="password" defaultValue={field.value} onChange={field.onChange} placeholder={t("inputPass")} />} />
{errors.password?.message && <p className="text-red-400 text-sm">{errors.password.message}</p>}
2024-12-26 11:29:22 +00:00
</div>
<div className="mb-4">
<Label>
2025-02-15 14:43:19 +00:00
{t("confirmPass")}
<span className="text-red-500">*</span>
2024-12-26 11:29:22 +00:00
</Label>
2025-02-15 14:43:19 +00:00
<Controller control={control} name="retypePassword" render={({ field }) => <Input size="md" type="password" defaultValue={field.value} onChange={field.onChange} placeholder={t("samePass")} />} />
{errors.retypePassword?.message && <p className="text-red-400 text-sm">{errors.retypePassword.message}</p>}
2024-12-26 11:29:22 +00:00
</div>
<div className="text-right">
<div className="mt-4">
2025-02-15 14:43:19 +00:00
<button type="submit" className="bg-red-700 text-white px-6 py-2 rounded hover:bg-red-800 focus:outline-none focus:ring focus:ring-red-300">
{t("save")}
2024-12-26 11:29:22 +00:00
</button>
</div>
</div>
</div>
</form>
</div>
</div>
);
};
export default ChangePassword;