/** * Component: Add Hardcover Shelf Modal * Documentation: documentation/frontend/components.md */ 'use client'; import React, { useState } from 'react'; import { Modal } from './Modal'; import { Input } from './Input'; import { Button } from './Button'; import { useAddHardcoverShelf } from '@/lib/hooks/useHardcoverShelves'; interface AddHardcoverShelfModalProps { isOpen: boolean; onClose: () => void; } export function AddHardcoverShelfModal({ isOpen, onClose, }: AddHardcoverShelfModalProps) { const [apiToken, setApiToken] = useState(''); const [listId, setListId] = useState(''); const [validationError, setValidationError] = useState(''); const [success, setSuccess] = useState(false); const [successMessage, setSuccessMessage] = useState(''); const { addShelf, isLoading, error } = useAddHardcoverShelf(); const validateInput = (): boolean => { if (!apiToken.trim()) { setValidationError('Hardcover API Token is required'); return false; } if (!listId.trim()) { setValidationError('Hardcover List ID or Status ID is required'); return false; } setValidationError(''); return true; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateInput()) return; try { const shelf = await addShelf(apiToken.trim(), listId.trim()); setSuccess(true); setSuccessMessage(`Added list "${shelf.name}" successfully!`); setApiToken(''); setListId(''); setTimeout(() => { setSuccess(false); onClose(); }, 2000); } catch { // Error is handled by the hook } }; const handleClose = () => { setApiToken(''); setListId(''); setValidationError(''); setSuccess(false); setSuccessMessage(''); onClose(); }; return (
{/* Visual header */}

Provides your Hardcover API token and the ID of the list you want to sync.

{/* Success alert */} {success && (

{successMessage}

)} {/* Error alert */} {error && (

{error}

)} {/* Form */}
{ setApiToken(e.target.value); if (validationError) setValidationError(''); }} placeholder="eyJhb..." disabled={isLoading || success} /> { setListId(e.target.value); if (validationError) setValidationError(''); }} placeholder="1234 or uuid" error={validationError} disabled={isLoading || success} />
); }