Skip to content

Commit

Permalink
πŸ―πŸ› ↝ [SSM-110 SSM-111 SSM-108]: My classifications/locations updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Feb 10, 2025
1 parent 4093a35 commit c0ba32b
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 15 deletions.
183 changes: 183 additions & 0 deletions app/planets/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
"use client";

import React, { useEffect, useState } from "react";
import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react";
import SimplePlanetGenerator from "@/components/Data/Generator/Astronomers/PlanetHunters/SimplePlanetGenerator";
import Navbar from "@/components/Layout/Navbar";
import { PostCardSingleWithGenerator } from "@/content/Posts/PostWithGen";

interface Classification {
id: number;
content: string | null;
author: string | null;
anomaly: Anomaly | null;
media: (string | { uploadUrl?: string })[] | null;
classificationtype: string | null;
classificationConfiguration?: any;
created_at: string;
title?: string;
votes?: number;
category?: string;
tags?: string[];
images?: string[];
relatedClassifications?: Classification[];
}

type Anomaly = {
id: number;
content: string | null;
anomalytype: string | null;
mass: number | null;
radius: number | null;
density: number | null;
gravity: number | null;
temperature: number | null;
orbital_period: number | null;
avatar_url: string | null;
created_at: string;
};

export default function ClassificationDetail({ params }: { params: { id: string } }) {
const supabase = useSupabaseClient();
const session = useSession();

const [classification, setClassification] = useState<Classification | null>(null);
const [anomaly, setAnomaly] = useState<Anomaly | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
if (!params.id) return;

const fetchClassification = async () => {
if (!params.id || !session) return; // Check if session is available

const { data, error } = await supabase
.from("classifications")
.select("*, anomaly:anomalies(*)")
.eq("id", params.id)
.single();

if (error) {
setError("Failed to fetch classification.");
setLoading(false);
return;
}

// Set the fetched classification data
setClassification(data);
setAnomaly(data.anomaly);

// Fetch related classifications if anomaly is available
const parentPlanetLocation = data.anomaly;
if (parentPlanetLocation) {
const { data: relatedData, error: relatedError } = await supabase
.from("classifications")
.select("*")
.eq("classificationConfiguration->>parentPlanetLocation", parentPlanetLocation.toString())
.eq("author", session.user.id);

if (!relatedError && relatedData) {
// Ensure we're safely updating the classification state
setClassification((prevState) => {
if (prevState) {
return {
...prevState,
relatedClassifications: relatedData,
};
}
return prevState;
});
}
}

setLoading(false);
};

fetchClassification();
}, [params.id, supabase, session]);

if (loading) return <p>Loading classification data...</p>;
if (error) return <p className="text-red-500">{error}</p>;
if (!classification) return <p>Classification not found.</p>;

return (
<div className="p-6 bg-black text-white border border-gray-200 rounded-md shadow-md">
<Navbar />
<div className="py-5"></div>
<h1 className="text-2xl font-bold">{classification.content || `Planet #${classification.id}`}</h1>
{classification.author && (
<SimplePlanetGenerator
classificationId={String(classification.id)}
classificationConfig={classification.classificationConfiguration}
author={classification.author}
/>
)}
{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"}
/>
)}
{anomaly && (
<div className="mt-6 p-4 bg-[#1E3A47] border border-gray-300 rounded-md">
<h2 className="text-xl font-bold">Related Planet</h2>
<p className="mt-2 text-sm">{anomaly.content || `Anomaly #${anomaly.id}`}</p>
<p className="mt-1 text-sm">Type: {anomaly.anomalytype || "Unknown"}</p>
<p className="mt-1 text-sm">Mass: {anomaly.mass ? `${anomaly.mass} kg` : "N/A"}</p>
<p className="mt-1 text-sm">Radius: {anomaly.radius ? `${anomaly.radius} km` : "N/A"}</p>
<p className="mt-1 text-sm">Density: {anomaly.density || "N/A"}</p>
<p className="mt-1 text-sm">Gravity: {anomaly.gravity || "N/A"}</p>
<p className="mt-1 text-sm">Orbital Period: {anomaly.orbital_period || "N/A"}</p>
<p className="mt-1 text-sm">Temperature: {anomaly.temperature || "N/A"}K</p>
<p className="mt-1 text-sm">Created At: {new Date(anomaly.created_at).toLocaleString()}</p>
{anomaly.avatar_url && (
<img
src={anomaly.avatar_url}
alt="Anomaly Avatar"
className="mt-4 w-32 h-32 object-cover rounded-md border"
/>
)}
</div>
)}
{classification.relatedClassifications && classification.relatedClassifications.length > 0 && (
<div className="mt-6">
<h3 className="text-xl font-bold">Related Classifications</h3>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 mt-4">
{classification.relatedClassifications.map((related: Classification) => (
<div
key={related.id}
className="p-4 border border-gray-200 rounded-md shadow-md bg-[#2C4F64]"
>
<h4 className="font-bold text-lg">Classification #{related.id}</h4>
<p className="mt-2 text-sm">{related.anomaly?.content || "No anomaly content"}</p>
{related.media && related.media.length > 0 && (
<div className="mt-2">
{related.media.map((media, index) => (
<img
key={index}
src={typeof media === "string" ? media : media.uploadUrl}
alt={`Related Classification #${related.id} - Image ${index + 1}`}
className="w-full h-auto rounded-md"
/>
))}
</div>
)}
</div>
))}
</div>
</div>
)}
</div>
);
};
3 changes: 2 additions & 1 deletion app/posts/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface Classification {

export default function SinglePostPage({ params }: { params: { id: string } }) {
const supabase = useSupabaseClient();

const [classification, setClassification] = useState<Classification | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
Expand All @@ -31,7 +32,7 @@ export default function SinglePostPage({ params }: { params: { id: string } }) {
setError("Invalid classification ID.");
setLoading(false);
return;
}
};

try {
const { data, error } = await supabase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Classification {
created_at: string;
content: string | null;
author: string | null;
anomaly: number | null;
anomaly: number | string | null;
media: any | null;
classificationtype: string | null;
classificationConfiguration: any | null;
Expand Down
30 changes: 17 additions & 13 deletions content/Classifications/UserLocations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react";
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";

interface ClassificationConfiguration {
classificationOptions: { [key: string]: any };
Expand All @@ -13,7 +14,6 @@ interface Classification {
author: string;
id: number;
content: string;
title: string;
classificationtype: string;
anomaly: number;
media: (string | { uploadUrl?: string })[] | null;
Expand All @@ -27,6 +27,7 @@ interface Classification {
export default function MySettlementsLocations() {
const supabase = useSupabaseClient();
const session = useSession();
const router = useRouter();

const [myLocations, setMyLocations] = useState<Classification[] | null>(null);
const [loading, setLoading] = useState<boolean>(true);
Expand All @@ -37,7 +38,7 @@ export default function MySettlementsLocations() {
setError("User is not logged in.");
setLoading(false);
return;
}
};

try {
const { data: locationClassificationData, error: lcError } = await supabase
Expand Down Expand Up @@ -89,7 +90,7 @@ export default function MySettlementsLocations() {
} finally {
setLoading(false);
}
}
};

useEffect(() => {
fetchUserLocationClassifications();
Expand All @@ -114,14 +115,13 @@ export default function MySettlementsLocations() {
key={location.id}
className="p-4 border border-gray-200 rounded-md shadow-md bg-[#2C4F64]"
>
<h3 className="font-bold text-lg">{location.title || `Location #${location.id}`}</h3>
<p>{location.content || "No description available."}</p>
{location.anomalyContent && (
<p className="mt-2 text-sm text-gray-300">
Related Anomaly: {location.anomalyContent}
</p>
)}
{location.relatedClassifications && location.relatedClassifications.length > 0 && (
<h3 className="font-bold text-lg">
{location.anomalyContent || `Location #${location.id}`}
</h3>
<p>{location.content || ""}</p>
{location.images && location.images.length > 0 && (
<div className="mt-2">
{location.relatedClassifications && location.relatedClassifications.length > 0 && (
<div className="mt-4">
<h4 className="font-semibold text-md">Related Classifications:</h4>
<ul className="list-disc list-inside text-sm text-gray-300">
Expand All @@ -133,8 +133,6 @@ export default function MySettlementsLocations() {
</ul>
</div>
)}
{location.images && location.images.length > 0 && (
<div className="mt-2">
{location.images.map((image, index) => (
<img
key={index}
Expand All @@ -145,6 +143,12 @@ export default function MySettlementsLocations() {
))}
</div>
)}
<button
onClick={() => router.push(`/planets/${location.id}`)}
className="mt-4 px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-700"
>
View Classification
</button>
</div>
))}
</div>
Expand Down

0 comments on commit c0ba32b

Please sign in to comment.