refactor(frontend): integrate ABSA payload and remove profession selection logic

This commit is contained in:
Mahen 2026-03-02 10:06:20 +07:00
parent 637d45a1bd
commit 125c18a000
4 changed files with 140 additions and 50 deletions

View File

@ -14,17 +14,11 @@ import z from "zod";
// "OTHER",
// ]);
const professionEnum = z.enum([
"PROGRAMMER",
"STUDENT",
"GAMER",
"DESIGNER",
"OTHER",
]);
// cony
export const analyzeSchema = z.object({
profession: professionEnum,
// brands: brandEnum,
// profession: professionEnum,
// brands: brandEnum,
url1: z.string().min(10, "Tautan 1 minimal 10 karakter"),
url2: z.string().min(10, "Tautan 2 minimal 10 karakter"),
url3: z.string().optional().or(z.literal("")),

View File

@ -36,29 +36,66 @@ export const useAnalyseText = () => {
},
});
useEffect(() => {
const fetchProfession = async () => {
try {
const user = await getAnotherUserData();
// useEffect(() => {
// const fetchProfession = async () => {
// try {
// const user = await getAnotherUserData();
const userProfession =
user?.preference?.profession || user?.preference?.profession;
// const userProfession =
// user?.preference?.profession || user?.preference?.profession;
if (userProfession) {
setValue("profession", userProfession, {
shouldValidate: true,
shouldDirty: true,
});
}
} catch (error) {
console.error("Gagal mengambil data profesi user:", error);
}
};
// if (userProfession) {
// setValue("profession", userProfession, {
// shouldValidate: true,
// shouldDirty: true,
// });
// }
// } catch (error) {
// console.error("Gagal mengambil data profesi user:", error);
// }
// };
if (session?.user) {
fetchProfession();
}
}, [session, setValue]);
// if (session?.user) {
// fetchProfession();
// }
// }, [session, setValue]);
// const onSubmit = async (data: AnalyzeFormData) => {
// if (!session?.user?.email) {
// alert("Anda harus login terlebih dahulu.");
// return;
// }
// setLoading(true);
// setResult(null);
// try {
// const urlsToScrape = [data.url1, data.url2, data.url3].filter(
// (url) => url && url.trim() !== "",
// ) as string[];
// const scrapePromises = urlsToScrape.map((url) => scrapeProduct(url));
// const scrapeResults = await Promise.all(scrapePromises);
// const candidates = scrapeResults.map((res) => ({
// name: res.data.name,
// url: res.data.url,
// reviews: res.data.reviews,
// }));
// const aiResult = await getAIRecommendation({
// user_email: session.user.email,
// // profession: data.profession,
// candidates,
// });
// setResult(aiResult);
// } catch (error: any) {
// alert("Terjadi kesalahan: " + error.message);
// } finally {
// setLoading(false);
// }
// };
const onSubmit = async (data: AnalyzeFormData) => {
if (!session?.user?.email) {
@ -67,7 +104,7 @@ export const useAnalyseText = () => {
}
setLoading(true);
setResult(null);
setResult(null);
try {
const urlsToScrape = [data.url1, data.url2, data.url3].filter(
@ -85,13 +122,21 @@ export const useAnalyseText = () => {
const aiResult = await getAIRecommendation({
user_email: session.user.email,
profession: data.profession,
candidates,
candidates: candidates,
});
setResult(aiResult);
setTimeout(() => {
document
.getElementById("analysis-result")
?.scrollIntoView({ behavior: "smooth" });
}, 100);
} catch (error: any) {
alert("Terjadi kesalahan: " + error.message);
console.error("Analysis Error:", error);
alert(
"Terjadi kesalahan: " + (error.message || "Gagal menganalisis ulasan."),
);
} finally {
setLoading(false);
}

View File

@ -1,4 +1,5 @@
import prisma from "@/lib/prisma";
import { AIRecommendationResponse } from "../types";
export const scrapeProduct = async (url: string) => {
const res = await fetch("/api/scrape", {
@ -18,24 +19,23 @@ export const scrapeProduct = async (url: string) => {
return data;
};
export const getAIRecommendation = async (payload: {
user_email: string;
profession: string;
candidates: { name: string; url: string; reviews: any[] }[];
}) => {
const aiRes = await fetch("http://localhost:8000/recommend", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
// export const getAIRecommendation = async (payload: {
// user_email: string;
// // profession: string;
// candidates: { name: string; url: string; reviews: any[] }[];
// }) => {
// const aiRes = await fetch("http://localhost:8000/recommend", {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify(payload),
// });
if (!aiRes.ok) throw new Error("Gagal melakukan analisis AI");
// if (!aiRes.ok) throw new Error("Gagal melakukan analisis AI");
return await aiRes.json();
};
// return await aiRes.json();
// };
export const getAnalysisData = async (email: string) => {
const userAnalyses = await prisma.analysis.findMany({
where: {
user: {
@ -64,3 +64,21 @@ export const getAnalysisData = async (email: string) => {
});
return userAnalyses;
};
export const getAIRecommendation = async (payload: {
user_email: string;
candidates: { name: string; url: string; reviews: string[] }[];
}): Promise<AIRecommendationResponse> => {
const aiRes = await fetch("http://localhost:8000/recommend", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!aiRes.ok) {
const errorData = await aiRes.json();
throw new Error(errorData.detail || "Gagal melakukan analisis AI");
}
return await aiRes.json();
};

View File

@ -198,10 +198,43 @@ export interface ProductDetail {
top_keywords: string[];
}
export interface AnalysisResults {
profession_target: string;
// export interface AnalysisResults {
// // profession_target: string;
// winning_product: string;
// details: ProductDetail[];
// }
export interface AspectScores {
performa: number;
layar: number;
baterai: number;
harga: number;
}
export interface ProductAnalysisResult {
name: string;
url: string;
general_score: number;
aspect_scores: AspectScores;
verdict: string;
description: string;
total_reviews: number;
positive_count: number;
negative_count: number;
}
export interface AIRecommendationResponse {
user_email: string;
analysis_type: string;
winning_product: string;
details: ProductDetail[];
details: ProductAnalysisResult[];
}
export interface AnalysisResults {
user_email: string;
analysis_type: string;
winning_product: string;
details: ProductAnalysisResult[];
}
export interface ResultProps {