faq sectin connected to backedn

This commit is contained in:
nabijonovdavronbek619@gmail.com
2026-01-14 19:24:19 +05:00
parent e94b7678f3
commit 8c5499dac1
2 changed files with 51 additions and 23 deletions

View File

@@ -1,39 +1,55 @@
"use client"; "use client";
import { useState } from "react"; import { useEffect, useState } from "react";
import { motion, AnimatePresence } from "framer-motion"; import { motion, AnimatePresence } from "framer-motion";
import { ChevronDown } from "lucide-react"; import { ChevronDown } from "lucide-react";
import { useLanguage } from "@/context/language-context"; import { useLanguage } from "@/context/language-context";
import { getAllFaq } from "@/lib/api";
interface FaqItem { interface FaqItem {
questionKey: string; questionKey: string;
answerKey: string; answerKey: string;
} }
interface FaqProps { export interface FaqBackendItem {
items?: FaqItem[]; id: number;
question_ru: string;
question_uz: string;
answer_ru: string;
answer_uz: string;
} }
export function FAQ({ items }: FaqProps) { const defaultItems: FaqItem[] = [
const {t} = useLanguage(); {
questionKey: "faq.items.0.question",
answerKey: "faq.items.0.answer",
},
{
questionKey: "faq.items.1.question",
answerKey: "faq.items.1.answer",
},
{
questionKey: "faq.items.2.question",
answerKey: "faq.items.2.answer",
},
];
export function FAQ() {
const { t, language } = useLanguage();
const [openIndex, setOpenIndex] = useState<number | null>(0); const [openIndex, setOpenIndex] = useState<number | null>(0);
const [faqItems, setFaqItems] = useState<FaqItem[]>([]);
const defaultItems: FaqItem[] = [ useEffect(() => {
{ async function fetchFaq() {
questionKey: "faq.items.0.question", const allFaq = await getAllFaq();
answerKey: "faq.items.0.answer", const faqItems: FaqItem[] = allFaq.map((item) => ({
}, questionKey: language === "uz" ? item.question_uz : item.question_ru,
{ answerKey: language === "uz" ? item.answer_uz : item.answer_ru,
questionKey: "faq.items.1.question", }));
answerKey: "faq.items.1.answer", faqItems.length === 0 ? setFaqItems(defaultItems) : setFaqItems(faqItems);
}, }
{ fetchFaq();
questionKey: "faq.items.2.question", }, [language]);
answerKey: "faq.items.2.answer",
},
];
const faqItems = items || defaultItems;
const containerVariants = { const containerVariants = {
hidden: { opacity: 0 }, hidden: { opacity: 0 },
@@ -81,7 +97,7 @@ export function FAQ({ items }: FaqProps) {
> >
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<h3 className="text-lg font-semibold text-gray-900 flex-1"> <h3 className="text-lg font-semibold text-gray-900 flex-1">
{t.faq.items[idx].question} {item.questionKey}
</h3> </h3>
<motion.div <motion.div
animate={{ rotate: openIndex === idx ? 180 : 0 }} animate={{ rotate: openIndex === idx ? 180 : 0 }}
@@ -104,7 +120,7 @@ export function FAQ({ items }: FaqProps) {
> >
<div className="bg-primary/20 p-6 rounded-b-lg border-t border-gray-200"> <div className="bg-primary/20 p-6 rounded-b-lg border-t border-gray-200">
<p className="text-gray-700 leading-relaxed"> <p className="text-gray-700 leading-relaxed">
{t.faq.items[idx].answer} {item.answerKey}
</p> </p>
</div> </div>
</motion.div> </motion.div>

View File

@@ -1,3 +1,4 @@
import { FaqBackendItem } from "@/components/FAQ";
import { Product } from "@/lib/products"; import { Product } from "@/lib/products";
export async function getAllProducts(): Promise<Product[]> { export async function getAllProducts(): Promise<Product[]> {
@@ -12,3 +13,14 @@ export async function getAllProducts(): Promise<Product[]> {
return res.json(); return res.json();
} }
export async function getAllFaq(): Promise<FaqBackendItem[]> {
const res = await fetch("https://admin.promtechno.uz/api/faqs/");
if (!res.ok) {
console.log("Failed to fetch faqs");
return [];
}
return res.json();
}