({ textId })
| 8 | import { useLanguage } from '@/contexts/LanguageContext'; |
| 9 | |
| 10 | const ExtendedTextSaveButton = ({ textId }) => { |
| 11 | const [isSaved, setIsSaved] = useState(false); |
| 12 | const [isLoading, setIsLoading] = useState(true); |
| 13 | const { isAuthenticated } = useAuth(); |
| 14 | const { showLimitReachedPopup, showLoginRequiredPopup } = usePopup(); |
| 15 | const { t } = useLanguage(); |
| 16 | |
| 17 | useEffect(() => { |
| 18 | if (textId && isAuthenticated) { |
| 19 | checkSavedStatus(); |
| 20 | } else { |
| 21 | setIsLoading(false); |
| 22 | } |
| 23 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 24 | }, [textId, isAuthenticated]); |
| 25 | |
| 26 | const checkSavedStatus = async () => { |
| 27 | try { |
| 28 | const response = await fetch(`/api/extended-text/${textId}/saved`); |
| 29 | const data = await response.json(); |
| 30 | if (data.success) { |
| 31 | setIsSaved(data.isSaved); |
| 32 | } |
| 33 | } catch (error) { |
| 34 | console.error('Error checking saved status:', error); |
| 35 | } finally { |
| 36 | setIsLoading(false); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | const toggleSave = async () => { |
| 41 | if (!isAuthenticated) { |
| 42 | showLoginRequiredPopup('extended texts'); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | setIsLoading(true); |
| 48 | const response = await fetch(`/api/extended-text/${textId}/save`, { |
| 49 | method: isSaved ? 'DELETE' : 'POST' |
| 50 | }); |
| 51 | const data = await response.json(); |
| 52 | |
| 53 | if (data.reachedLimit) { |
| 54 | showLimitReachedPopup('sentences'); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (data.success) { |
| 59 | setIsSaved(!isSaved); |
| 60 | } |
| 61 | } catch (error) { |
| 62 | console.error('Error toggling extended text save:', error); |
| 63 | } finally { |
| 64 | setIsLoading(false); |
| 65 | } |
| 66 | }; |
| 67 |
nothing calls this directly
no test coverage detected