diff --git a/src/components/admin/SuggestionsPanel.jsx b/src/components/admin/SuggestionsPanel.jsx index b12c14c..abb5756 100644 --- a/src/components/admin/SuggestionsPanel.jsx +++ b/src/components/admin/SuggestionsPanel.jsx @@ -27,6 +27,7 @@ export const SuggestionsPanel = () => { const [newCategory, setNewCategory] = React.useState("feature"); const [submitting, setSubmitting] = React.useState(false); const [message, setMessage] = React.useState(""); + const [myVotes, setMyVotes] = React.useState(new Set()); const getSupabase = React.useCallback(async () => { const { createClient } = await import("@supabase/supabase-js"); @@ -42,14 +43,23 @@ export const SuggestionsPanel = () => { const { data } = await supabase .from("suggestions") .select("*") + .order("vote_count", { ascending: false }) .order("created_at", { ascending: false }); + + // Fetch my votes + const { data: votesData } = await supabase + .from("suggestion_votes") + .select("suggestion_id") + .eq("user_id", user?.id); + setMyVotes(new Set((votesData || []).map((v) => v.suggestion_id))); + setSuggestions(data || []); } catch (e) { console.error("fetch suggestions error", e); } finally { setLoading(false); } - }, [getSupabase]); + }, [getSupabase, user?.id]); React.useEffect(() => { fetchSuggestions(); }, [fetchSuggestions]); @@ -90,6 +100,40 @@ export const SuggestionsPanel = () => { } }; + const handleVote = async (suggestionId) => { + const hasVoted = myVotes.has(suggestionId); + try { + const supabase = await getSupabase(); + if (hasVoted) { + await supabase + .from("suggestion_votes") + .delete() + .eq("suggestion_id", suggestionId) + .eq("user_id", user?.id); + setMyVotes((prev) => { + const next = new Set(prev); + next.delete(suggestionId); + return next; + }); + } else { + await supabase + .from("suggestion_votes") + .insert({ suggestion_id: suggestionId, user_id: user?.id }); + setMyVotes((prev) => new Set(prev).add(suggestionId)); + } + fetchSuggestions(); + } catch (e) { + console.error("vote error", e); + // Revert optimistic update + setMyVotes((prev) => { + const next = new Set(prev); + if (hasVoted) next.add(suggestionId); + else next.delete(suggestionId); + return next; + }); + } + }; + return (
- Есть идея? Опишите — админы рассмотрят. + Есть идея? Опишите — другие смогут проголосовать, админы рассмотрят.
Загрузка...
) : suggestions.length === 0 ? ( @@ -152,6 +201,7 @@ export const SuggestionsPanel = () => { {suggestions.map((s) => { const cat = CATEGORY_OPTIONS.find((c) => c.value === s.category) || CATEGORY_OPTIONS[3]; const st = STATUS_MAP[s.status] || STATUS_MAP.new; + const hasVoted = myVotes.has(s.id); return ({s.content}
-