mediahub-fe/app/[locale]/(public)/feedback/page.tsx

139 lines
3.9 KiB
TypeScript
Raw Normal View History

"use client";
2024-12-17 14:27:48 +00:00
import { Reveal } from "@/components/landing-page/Reveal";
2024-12-20 15:52:53 +00:00
import { error, loading, successCallback } from "@/config/swal";
import { getFeedback, postUserFeedback } from "@/service/landing/landing";
import React, { useEffect, useState } from "react";
2025-02-15 14:43:19 +00:00
import { useTranslations } from "next-intl";
interface RatingProps {
label: string;
onRate: (rating: number) => void;
}
const Rating: React.FC<RatingProps> = ({ label, onRate }) => {
const [selected, setSelected] = useState<number>(0);
const handleClick = (rating: number) => {
setSelected(rating);
onRate(rating);
};
return (
<div className="flex justify-between items-center mb-4">
<span className="text-gray-800">{label}</span>
<div className="flex space-x-1">
{[1, 2, 3, 4, 5].map((star) => (
<button key={star} onClick={() => handleClick(star)} className={`text-2xl ${star <= selected ? "text-yellow-500" : "text-gray-300"}`}>
</button>
))}
</div>
</div>
);
};
const FeedbackForm: React.FC = () => {
2024-12-20 15:52:53 +00:00
const [ratings, setRatings] = useState({
accessibility: 0,
appearance: 0,
content: 0,
});
useEffect(() => {
async function initState() {
const response = await getFeedback();
console.log(response?.data?.data);
setRatings(response?.data?.data);
}
initState();
}, []);
2024-12-20 15:52:53 +00:00
type Feedback = {
feedbackId: any;
score: any;
};
2025-01-31 12:51:04 +00:00
2024-12-20 15:52:53 +00:00
const [userFeedbacks, setUserFeedbacks] = useState<Feedback[]>([]);
const [hasMounted, setHasMounted] = useState(false);
2025-02-15 14:43:19 +00:00
const t = useTranslations("LandingPage");
2024-12-20 15:52:53 +00:00
const handleRatingChange = (id: any, score: any) => {
const listData = [...userFeedbacks];
const dataIdx = userFeedbacks.findIndex((o) => o.feedbackId === id);
console.log("idx :", dataIdx);
console.log("Before :", listData);
2025-01-31 12:51:04 +00:00
2024-12-20 15:52:53 +00:00
if (dataIdx !== -1) {
console.log("update");
listData[dataIdx].score = score;
} else {
const data = {
feedbackId: id,
score,
};
2025-01-31 12:51:04 +00:00
2024-12-20 15:52:53 +00:00
listData.push(data);
}
2025-01-31 12:51:04 +00:00
2024-12-20 15:52:53 +00:00
setUserFeedbacks(listData);
console.log("After :", userFeedbacks);
};
const handleSubmit = async () => {
2024-12-20 15:52:53 +00:00
loading();
console.log("Save Feedback :", userFeedbacks);
const response = await postUserFeedback();
2024-12-20 15:52:53 +00:00
close();
2025-01-01 17:48:57 +00:00
if (response?.error) {
error(response?.message);
2024-12-20 15:52:53 +00:00
return false;
}
successCallback("Terima kasih, feedback Anda telah terkirim");
};
2024-12-20 15:52:53 +00:00
async function onSubmit() {
// loading();
// let finalData = {
// name: data.name,
// email: data.email,
// title : data.title,
// message: data.title
// }
// console.log(finalData);
// const response = await sendMessage(finalData);
// close();
// successCallback();
}
// Hooks
useEffect(() => {
setHasMounted(true);
}, []);
// Render
if (!hasMounted) return null;
return (
2024-12-17 14:27:48 +00:00
<Reveal>
<div className="max-w-6xl flex flex-col mx-auto p-4 lg:p-40 gap-5">
<div className="flex items-center justify-center mb-6">
<img src="/assets/icons-feedback.png" alt="Feedback" />
2025-02-15 14:43:19 +00:00
<h2 className="ml-4 text-[15px] lg:text-[32px] font-bold text-gray-800">{t("userFeedback")}</h2>
2024-12-17 14:27:48 +00:00
</div>
2025-02-15 14:43:19 +00:00
<Rating label={t("ratings")} onRate={(rating) => handleRatingChange("accessibility", rating)} />
<Rating label={t("ratings2")} onRate={(rating) => handleRatingChange("appearance", rating)} />
<Rating label={t("ratings3")} onRate={(rating) => handleRatingChange("content", rating)} />
2024-12-17 14:27:48 +00:00
<div className="flex justify-center">
2025-01-31 12:51:04 +00:00
<button onClick={handleSubmit} className="w-fit lg:w-32 bg-[#2F80ED] text-white py-2 px-4 gap-4 rounded-md hover:bg-blue-600 transition text-sm lg:text-base">
2025-02-15 14:43:19 +00:00
{t("send")}
2024-12-17 14:27:48 +00:00
</button>
</div>
</div>
2024-12-17 14:27:48 +00:00
</Reveal>
);
};
export default FeedbackForm;