-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aabf840
commit 9ca0298
Showing
2 changed files
with
106 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useState, useEffect } from "react"; | ||
import { Card, Flex, Heading, Icon, Text } from "@chakra-ui/react"; | ||
import { FaFlag, FaRegFlag } from "react-icons/fa"; | ||
import { toggleSongMemoPending } from "../services/songs"; | ||
|
||
export default function SongPreviewCard({ | ||
entry, | ||
isHighlighted, | ||
onUpdate, | ||
toast, | ||
}) { | ||
const [optimisticMemo, setOptimisticMemo] = useState(entry.song.song_memo); | ||
|
||
// Sync local state with incoming props | ||
useEffect(() => { | ||
setOptimisticMemo(entry.song.song_memo); | ||
}, [entry.song.song_memo]); | ||
|
||
const handleToggleMemo = async () => { | ||
const previousMemo = optimisticMemo; | ||
const hasSongMemo = optimisticMemo !== null; | ||
const hasPendingSongMemo = hasSongMemo && optimisticMemo.text === "pending"; | ||
|
||
// Optimistically update the memo | ||
setOptimisticMemo( | ||
hasPendingSongMemo | ||
? null // Remove memo if toggling off | ||
: { text: "pending" } // Add pending memo if toggling on | ||
); | ||
|
||
try { | ||
const result = await toggleSongMemoPending(entry.song.id); | ||
if (typeof result === "string") { | ||
throw new Error("Failed to toggle memo"); | ||
} | ||
onUpdate(); // Refresh parent data after a successful update | ||
} catch (error) { | ||
// Revert to previous state on failure | ||
setOptimisticMemo(previousMemo); | ||
toast({ | ||
title: "Error", | ||
description: "Could not toggle pending memo", | ||
status: "error", | ||
duration: 9000, | ||
isClosable: true, | ||
}); | ||
} | ||
}; | ||
|
||
const hasSongMemo = optimisticMemo !== null; | ||
const hasPendingSongMemo = hasSongMemo && optimisticMemo.text === "pending"; | ||
const songMemoText = hasPendingSongMemo | ||
? "Memo pending" | ||
: hasSongMemo | ||
? optimisticMemo.text | ||
: "No memo"; | ||
|
||
return ( | ||
<Card | ||
p="4" | ||
m="4" | ||
borderRadius="lg" | ||
bgColor={!isHighlighted ? "gray.100" : ""} | ||
minH={isHighlighted ? "30vh" : "16vh"} | ||
> | ||
<Flex direction={"row"} justifyContent={"space-between"}> | ||
<Heading size="md" mb="4"> | ||
"{entry.song.title}" by {entry.song.artist} | ||
</Heading> | ||
{(!hasSongMemo || hasPendingSongMemo) && ( | ||
<Icon | ||
as={hasPendingSongMemo ? FaFlag : FaRegFlag} | ||
color="blue.300" | ||
fontSize={"48px"} | ||
onClick={handleToggleMemo} | ||
/> | ||
)} | ||
</Flex> | ||
{hasSongMemo && !hasPendingSongMemo ? ( | ||
<Text fontSize="xl">{songMemoText}</Text> | ||
) : ( | ||
<Text fontSize="xl" fontStyle="italic"> | ||
{songMemoText} | ||
</Text> | ||
)} | ||
</Card> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters