Skip to content

Commit

Permalink
🍊🐞 ↝ [SSC-86 SSM-132 SSM-115 SSG-153]: New vote/surveyor interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Feb 27, 2025
1 parent 23787b3 commit 56be28c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 121 deletions.
30 changes: 16 additions & 14 deletions app/planets/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,22 @@ export default function ClassificationDetail({ params }: { params: { id: string
/>
)}
{anomaly && classification.author === session?.user?.id && (
<PostCardSingleWithGenerator
key={classification.id}
classificationId={classification.id}
title={classification.title || "Untitled"}
author={classification.author || "Unknown"}
content={classification.content || "No content available"}
votes={classification.votes || 0}
category={classification.category || "Uncategorized"}
tags={classification.tags || []}
images={classification.images || []}
anomalyId={classification.anomaly ? String(classification.anomaly.id) : ""}
classificationConfig={classification.classificationConfiguration}
classificationType={classification.classificationtype || "Unknown"}
/>
<div>
<PostCardSingleWithGenerator
key={classification.id}
classificationId={classification.id}
title={classification.title || "Untitled"}
author={classification.author || "Unknown"}
content={classification.content || "No content available"}
votes={classification.votes || 0}
category={classification.category || "Uncategorized"}
tags={classification.tags || []}
images={classification.images || []}
anomalyId={classification.anomaly ? String(classification.anomaly.id) : ""}
classificationConfig={classification.classificationConfiguration}
classificationType={classification.classificationtype || "Unknown"}
/>
</div>
)}
{/* {anomaly && (
<div className="mt-6 p-4 bg-[#1E3A47] border border-gray-300 rounded-md">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ export default function PlanetGenerator({ classificationConfig, author, classifi
onTypeOverride={handleTypeOverride}
/>
<PlanetImportExport stats={stats} onImport={handleImport} onSave={handleSave} />
{author === session?.user.id && (
{/* {author === session?.user.id && (
<button
onClick={handleSave}
className="mt-4 bg-blue-500 text-white py-2 px-4 rounded"
>
Save Configuration
</button>
)}
)} */}
</div>
</div>
</div>
Expand Down
114 changes: 66 additions & 48 deletions components/Structures/Missions/Astronomers/PlanetHunters/PHVote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,37 @@ interface Classification {
classificationConfiguration: any | null;
};

export default function VotePlanetClassifictions() {
export default function VotePlanetClassifications() {
const supabase = useSupabaseClient();
const session = useSession();

const [classifications, setClassifications] = useState<any[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const [currentIndex, setCurrentIndex] = useState<number>(0);

const fetchClassifications = async () => {
if (!session?.user) {
setError("User session not found.");
setLoading(false);
return;
};

setLoading(true);
setError(null);
try {
const { data, error } = await supabase
.from('classifications')
.select('*')
// .eq('author', session.user.id)
.eq('classificationtype', 'planet')
.order('created_at', { ascending: false }) as { data: Classification[]; error: any };

if (error) throw error;

// const processedData = data.map((classification) => {
// const media = classification.media;
// let images: string[] = [];

// if (Array.isArray(media) && media.length === 2 && typeof media[1] === "string") {
// images.push(media[1]);
// } else if (media && media.uploadUrl) {
// images.push(media.uploadUrl);
// }

// const votes = classification.classificationConfiguration?.votes || 0;

// return { ...classification, images, votes };
// });


const processedData = data.map((classification) => {
const media = classification.media;
let image: string | null = null;

if (Array.isArray(media)) {
for (const subArray of media) {
if (Array.isArray(subArray) && subArray.length > 0) {
Expand All @@ -72,39 +57,39 @@ export default function VotePlanetClassifictions() {
} else if (media && typeof media.uploadUrl === "string") {
image = media.uploadUrl;
}

const votes = classification.classificationConfiguration?.votes || 0;

return { ...classification, image, votes };
});
});

setClassifications(processedData);
} catch (error) {
console.error("Error fetching classifications:", error);
setError("Failed to load classifications.");
} finally {
setLoading(false);
};
};
};

useEffect(() => {
fetchClassifications();
}, [session])
}, [session]);

const handleVote = async (classificationId: number, currentConfig: any) => {
try {
const currentVotes = currentConfig?.votes || 0;

const updatedConfig = {
...currentConfig,
votes: currentVotes + 1,
};

const { error } = await supabase
.from("classifications")
.update({ classificationConfiguration: updatedConfig })
.eq("id", classificationId);

if (error) {
console.error("Error updating classificationConfiguration:", error);
} else {
Expand All @@ -121,32 +106,65 @@ export default function VotePlanetClassifictions() {
}
};

const nextPost = () => {
setCurrentIndex((prevIndex) => Math.min(prevIndex + 1, classifications.length - 1));
};

const prevPost = () => {
setCurrentIndex((prevIndex) => Math.max(prevIndex - 1, 0));
};

return (
<div className="space-y-8">
<div className="relative">
<div className="flex overflow-x-scroll space-x-4 py-4">
{loading ? (
<p>Loading classifications...</p>
) : error ? (
<p>{error}</p>
) : (
classifications.map((classification) => (
<PostCardSingle
classifications.map((classification, index) => (
<div
key={classification.id}
classificationId={classification.id}
title={classification.title}
author={classification.author}
content={classification.content}
votes={classification.votes || 0}
category={classification.category}
tags={classification.tags || []}
images={classification.image ? [classification.image] : []}
// images={classification.images || []}
anomalyId={classification.anomaly}
classificationConfig={classification.classificationConfiguration}
classificationType={classification.classificationtype}
onVote={() => handleVote(classification.id, classification.classificationConfiguration)}
/>
className={`flex-shrink-0 w-full max-w-screen-md ${index === currentIndex ? "block" : "hidden"}`}
>
<PostCardSingle
classificationId={classification.id}
title={classification.title}
author={classification.author}
content={classification.content}
votes={classification.votes || 0}
category={classification.category}
tags={classification.tags || []}
images={classification.image ? [classification.image] : []}
anomalyId={classification.anomaly}
classificationConfig={classification.classificationConfiguration}
classificationType={classification.classificationtype}
onVote={() => handleVote(classification.id, classification.classificationConfiguration)}
/>
</div>
))
)}
</div>
</div>

<div className="absolute top-1/2 left-0 transform -translate-y-1/2">
<button
onClick={prevPost}
className="bg-gray-800 text-white p-2 rounded-l-md hover:bg-gray-600"
disabled={currentIndex === 0}
>
Prev
</button>
</div>

<div className="absolute top-1/2 right-0 transform -translate-y-1/2">
<button
onClick={nextPost}
className="bg-gray-800 text-white p-2 rounded-r-md hover:bg-gray-600"
disabled={currentIndex === classifications.length - 1}
>
Next
</button>
</div>
</div>
);
};
Loading

0 comments on commit 56be28c

Please sign in to comment.