🔧 Fixed SQL Syntax Errors in Favorites API
- Separated conditional SQL queries to avoid template literal issues - Added proper error handling in CollaboratorFacepile component - Added error handling for favorites functionality - Improved fallback states for failed API calls This resolves the 'syntax error at or near AND' and '' parameter errors.
This commit is contained in:
parent
faab506b25
commit
34b16e972f
3 changed files with 44 additions and 15 deletions
|
|
@ -21,9 +21,13 @@ export default function CollaboratorFacepile({ collectionId, creatorEmail }) {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setCollaborators(data.permissions || []);
|
setCollaborators(data.permissions || []);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to fetch collaborators:', response.status);
|
||||||
|
setCollaborators([]); // Fallback to empty array
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching collaborators:', error);
|
console.error('Error fetching collaborators:', error);
|
||||||
|
setCollaborators([]); // Fallback to empty array
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,24 @@ export default async function handler(req, res) {
|
||||||
// Get user's favorites
|
// Get user's favorites
|
||||||
const { type } = req.query; // Optional filter by type
|
const { type } = req.query; // Optional filter by type
|
||||||
|
|
||||||
const result = await sql`
|
let result;
|
||||||
|
if (type) {
|
||||||
|
result = await sql`
|
||||||
|
SELECT uf.*,
|
||||||
|
CASE
|
||||||
|
WHEN uf.item_type = 'collection' THEN c.name
|
||||||
|
WHEN uf.item_type = 'card' THEN cards.name
|
||||||
|
WHEN uf.item_type = 'deck' THEN d.name
|
||||||
|
END as item_name
|
||||||
|
FROM user_favorites uf
|
||||||
|
LEFT JOIN collections c ON uf.item_type = 'collection' AND uf.item_id = c.id
|
||||||
|
LEFT JOIN cards ON uf.item_type = 'card' AND uf.item_id = cards.id
|
||||||
|
LEFT JOIN decks d ON uf.item_type = 'deck' AND uf.item_id = d.id
|
||||||
|
WHERE uf.user_id = ${user.userId} AND uf.item_type = ${type}
|
||||||
|
ORDER BY uf.created_at DESC
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
result = await sql`
|
||||||
SELECT uf.*,
|
SELECT uf.*,
|
||||||
CASE
|
CASE
|
||||||
WHEN uf.item_type = 'collection' THEN c.name
|
WHEN uf.item_type = 'collection' THEN c.name
|
||||||
|
|
@ -45,9 +62,9 @@ export default async function handler(req, res) {
|
||||||
LEFT JOIN cards ON uf.item_type = 'card' AND uf.item_id = cards.id
|
LEFT JOIN cards ON uf.item_type = 'card' AND uf.item_id = cards.id
|
||||||
LEFT JOIN decks d ON uf.item_type = 'deck' AND uf.item_id = d.id
|
LEFT JOIN decks d ON uf.item_type = 'deck' AND uf.item_id = d.id
|
||||||
WHERE uf.user_id = ${user.userId}
|
WHERE uf.user_id = ${user.userId}
|
||||||
${type ? sql`AND uf.item_type = ${type}` : sql``}
|
|
||||||
ORDER BY uf.created_at DESC
|
ORDER BY uf.created_at DESC
|
||||||
`;
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
favorites: result.rows
|
favorites: result.rows
|
||||||
|
|
|
||||||
|
|
@ -77,9 +77,13 @@ export default function CollectionView() {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const isFav = data.favorites.some(fav => fav.item_id === parseInt(id));
|
const isFav = data.favorites.some(fav => fav.item_id === parseInt(id));
|
||||||
setIsFavorited(isFav);
|
setIsFavorited(isFav);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to check favorites:', response.status);
|
||||||
|
// Keep default false state
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error checking favorites:', error);
|
console.error('Error checking favorites:', error);
|
||||||
|
// Keep default false state
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -146,6 +150,8 @@ export default function CollectionView() {
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
setIsFavorited(false);
|
setIsFavorited(false);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to remove favorite:', response.status);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Add to favorites
|
// Add to favorites
|
||||||
|
|
@ -163,6 +169,8 @@ export default function CollectionView() {
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
setIsFavorited(true);
|
setIsFavorited(true);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to add favorite:', response.status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue