Skip to content

Commit

Permalink
Added types for the metadata, and cards returned.
Browse files Browse the repository at this point in the history
I defined the types inside the route and the CreateGameForm
instead of in a separate file just to not clash with what a
Card is in the rest of the app.
  • Loading branch information
wanderwayward committed Nov 26, 2024
1 parent c809a0d commit 9ac43ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ interface CreateGameFormProps {
setFormat?: (format: string) => void;
}

interface DeckMetadata {
name: string;
author: string;
}

interface DeckCard {
id: string;
count: number;
}

interface DeckData {
metadata: DeckMetadata;
leader: DeckCard;
secondleader: DeckCard | null;
base: DeckCard;
deck: DeckCard[];
sideboard: DeckCard[];
}

const deckOptions: string[] = [
"Vader Green Ramp",
"Obi-Wan Blue Control",
Expand Down Expand Up @@ -100,7 +119,7 @@ const CreateGameForm: React.FC<CreateGameFormProps> = ({
useState<string>("Vader Green Ramp");
const [deckLink, setDeckLink] = useState<string>("");
const [saveDeck, setSaveDeck] = useState<boolean>(false);
const [deckData, setDeckData] = useState<any>(null); // State to store fetched deck data
const [deckData, setDeckData] = useState<DeckData | null>(null);

// Additional State for Non-Creategame Path
const [gameName, setGameName] = useState<string>("");
Expand All @@ -116,11 +135,14 @@ const CreateGameForm: React.FC<CreateGameFormProps> = ({
throw new Error(`Failed to fetch deck: ${response.statusText}`);
}

const data = await response.json();
const data: DeckData = await response.json();
setDeckData(data);
console.log(data);
} catch (error: any) {
console.error("Error fetching deck:", error.message);
} catch (error) {
if (error instanceof Error) {
console.error("Error fetching deck:", error.message);
} else {
console.error("Unexpected error:", error);
}
}
};

Expand All @@ -131,7 +153,7 @@ const CreateGameForm: React.FC<CreateGameFormProps> = ({
console.log("SWUDB Deck Link:", deckLink);
console.log("beginning fetch for deck link");
fetchDeckData(deckLink);
console.log("fetch complete");
console.log("fetch complete, deck data:", deckData);
console.log("Save Deck To Favourites:", saveDeck);

if (!isCreateGamePath) {
Expand Down
40 changes: 32 additions & 8 deletions src/app/api/swudbdeck/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { NextResponse } from "next/server";

interface DeckMetadata {
name: string;
author: string;
}

interface DeckCard {
id: string;
count: number;
}

interface DeckData {
metadata: DeckMetadata;
leader: DeckCard;
secondleader: DeckCard | null;
base: DeckCard;
deck: DeckCard[];
sideboard: DeckCard[];
}

export async function GET(req: Request) {
try {
const { searchParams } = new URL(req.url);
Expand All @@ -10,8 +29,7 @@ export async function GET(req: Request) {
return NextResponse.json({ error: "Missing deckLink" }, { status: 400 });
}

// regex to get deck, i found two link styles one with deck/id and other with deck/view/id so i used this regex to get the id either way
const match = deckLink.match(/\/deck\/(?:view\/)?([^/?]+)/); // Matches /deck/[id] or /deck/view/[id]
const match = deckLink.match(/\/deck\/(?:view\/)?([^/?]+)/);
const deckId = match ? match[1] : null;

if (!deckId) {
Expand All @@ -22,8 +40,6 @@ export async function GET(req: Request) {
);
}

console.log("Extracted deckId:", deckId);

const apiUrl = `https://swudb.com/deck/view/${deckId}?handler=JsonFile`;

const response = await fetch(apiUrl, { method: "GET" });
Expand All @@ -33,11 +49,19 @@ export async function GET(req: Request) {
throw new Error(`SWUDB API error: ${response.statusText}`);
}

const data = await response.json();
const data: DeckData = await response.json();

return NextResponse.json(data);
} catch (error: any) {
console.error("Internal Server Error:", error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
} catch (error) {
if (error instanceof Error) {
console.error("Internal Server Error:", error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}

console.error("Unexpected error:", error);
return NextResponse.json(
{ error: "An unexpected error occurred" },
{ status: 500 }
);
}
}

0 comments on commit 9ac43ac

Please sign in to comment.