Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 9 additions & 60 deletions src/api/hangman.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import api from "./axios";
import { useAuthStore } from "@/store/useAuthStore";

interface HangmanQuestion {
question: string;
Expand All @@ -25,9 +24,6 @@ export interface LeaderboardEntry {
createdAt: string;
}

/**
* Create a new hangman template/game
*/
export const createHangmanTemplate = async (data: CreateTemplateData) => {
const formData = new FormData();
formData.append("name", data.name);
Expand All @@ -44,30 +40,18 @@ export const createHangmanTemplate = async (data: CreateTemplateData) => {
formData.append("thumbnail_image", data.thumbnail);
}

const token = useAuthStore.getState().token;
const response = await api.post(`/api/game/game-type/hangman`, formData, {
headers: {
Authorization: `Bearer ${token}`,
},
headers: { "Content-Type": "multipart/form-data" },
});

return response.data;
};

/**
* Get a hangman template by ID
*/
export const getHangmanTemplate = async (gameId: string) => {
const token = useAuthStore.getState().token;
const response = await api.get(`/api/game/game-type/hangman/${gameId}`, {
headers: { Authorization: `Bearer ${token}` },
});
const response = await api.get(`/api/game/game-type/hangman/${gameId}`);
return response.data;
};

/**
* Get all hangman templates
*/
export const getAllHangmanTemplates = async (userId?: string) => {
const response = await api.get(`/api/game/game-type/hangman`, {
params: {
Expand All @@ -77,23 +61,15 @@ export const getAllHangmanTemplates = async (userId?: string) => {
return response.data;
};

/**
* Get user's hangman templates
*/
export const getUserHangmanTemplates = async () => {
const token = useAuthStore.getState().token;
const response = await api.get(`/api/game/game-type/hangman`, {
params: {
userId: localStorage.getItem("userId"),
},
headers: { Authorization: `Bearer ${token}` },
});
return response.data;
};

/**
* Update a hangman template
*/
export const updateHangmanTemplate = async (
gameId: string,
data: Partial<CreateTemplateData>,
Expand All @@ -113,71 +89,47 @@ export const updateHangmanTemplate = async (
formData.append("is_publish", String(data.is_publish_immediately));
if (data.thumbnail) formData.append("thumbnail_image", data.thumbnail);

const token = useAuthStore.getState().token;
const response = await api.patch(
`/api/game/game-type/hangman/${gameId}`,
formData,
{
headers: {
Authorization: `Bearer ${token}`,
},
headers: { "Content-Type": "multipart/form-data" },
},
);

return response.data;
};

/**
* Delete a hangman template
*/
export const deleteHangmanTemplate = async (gameId: string) => {
const token = useAuthStore.getState().token;
const response = await api.delete(`/api/game/game-type/hangman/${gameId}`, {
headers: { Authorization: `Bearer ${token}` },
});
const response = await api.delete(`/api/game/game-type/hangman/${gameId}`);
return response.data;
};

/**
* Toggle publish status of a hangman game
*/
export const togglePublishHangman = async (
gameId: string,
isPublish: boolean,
) => {
const token = useAuthStore.getState().token;
const formData = new FormData();
formData.append("is_publish", String(isPublish));

const response = await api.patch(
`/api/game/game-type/hangman/${gameId}`,
(() => {
const formData = new FormData();
formData.append("is_publish", String(isPublish));
return formData;
})(),
formData,
{
headers: { Authorization: `Bearer ${token}` },
headers: { "Content-Type": "multipart/form-data" },
},
);
return response.data;
};

/**
* Unpublish a hangman game
*/
export const unpublishHangman = async (gameId: string) => {
const token = useAuthStore.getState().token;
const response = await api.post(
`/api/game/game-type/hangman/${gameId}/unpublish`,
{},
{
headers: { Authorization: `Bearer ${token}` },
},
);
return response.data;
};

/**
* Save game result to leaderboard
*/
export const saveGameResult = async (
gameId: string,
score: number,
Expand All @@ -193,9 +145,6 @@ export const saveGameResult = async (
return response.data;
};

/**
* Get leaderboard for a game
*/
export const getHangmanLeaderboard = async (
gameId: string,
limit: number = 10,
Expand Down
45 changes: 26 additions & 19 deletions src/pages/hangman/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,33 @@ function HangmanGame() {
useEffect(() => {
const fetchGameTemplate = async () => {
try {
let endpoint = `/api/game/game-type/hangman/${id}`;

// Use public endpoint if not authenticated
if (!token) {
endpoint = `/api/game/game-type/hangman/${id}/play/public`;
let endpoint = `/api/game/game-type/hangman/${id}/play/public`;

try {
const response = await api.get(endpoint);
const gameData = response.data.data;

const questionsData = gameData.questions || [];
setQuestions(questionsData);
setGameTitle(gameData.name);
setCreatorUsername(gameData.creator_username || "Unknown");
setLoading(false);
return;
} catch (publicError) {
if (token) {
endpoint = `/api/game/game-type/hangman/${id}/play/private`;
const response = await api.get(endpoint);
const gameData = response.data.data;

const questionsData = gameData.questions || [];
setQuestions(questionsData);
setGameTitle(gameData.name);
setCreatorUsername(gameData.creator_username || "Unknown");
setLoading(false);
return;
}
throw publicError;
}

const response = await api.get(endpoint);
const gameData = response.data.data;

// Handle both full game data and public game data
const questionsData =
gameData.questions || gameData.game_json?.questions || [];
setQuestions(questionsData);
setGameTitle(gameData.name);
setCreatorUsername(
gameData.creator_username || gameData.creator?.username || "Unknown",
);
setLoading(false);
} catch (error) {
console.error("Error fetching game:", error);
toast.error("Failed to load game");
Expand All @@ -138,7 +146,6 @@ function HangmanGame() {
const fetchLeaderboard = async () => {
try {
const data = await getHangmanLeaderboard(id!);
console.log("Leaderboard data:", data); // Debug log
setLeaderboard(data);
} catch (error) {
console.error("Error fetching leaderboard:", error);
Expand Down
Loading