import React, { useState, useEffect } from "react"; import { Modal, Pressable, StyleSheet, TextInput, View, ActivityIndicator, KeyboardAvoidingView, Platform, } from "react-native"; import { ThemedText } from "@/components/ThemedText"; import { useColorScheme } from "@/hooks/useColorScheme"; import { Colors } from "@/constants/Colors"; import { useCreateList, useUpdateList } from "@/hooks/useBookhiveQuery"; interface CreateListModalProps { visible: boolean; onClose: () => void; editList?: { uri: string; name: string; description?: string }; } export function CreateListModal({ visible, onClose, editList }: CreateListModalProps) { const colorScheme = useColorScheme(); const colors = Colors[colorScheme ?? "light"]; const [name, setName] = useState(""); const [description, setDescription] = useState(""); const createList = useCreateList(); const updateList = useUpdateList(); const isPending = createList.isPending || updateList.isPending; useEffect(() => { if (visible) { setName(editList?.name ?? ""); setDescription(editList?.description ?? ""); } }, [visible, editList]); const handleSubmit = async () => { const trimmedName = name.trim(); if (!trimmedName) return; try { if (editList) { await updateList.mutateAsync({ uri: editList.uri, name: trimmedName, description: description.trim() || undefined, }); } else { await createList.mutateAsync({ name: trimmedName, description: description.trim() || undefined, }); } onClose(); } catch (error) { console.error("Failed to save list:", error); } }; return ( {editList ? "Edit List" : "Create List"} Cancel Name Description (optional) {isPending ? ( ) : ( {editList ? "Save Changes" : "Create List"} )} ); } const styles = StyleSheet.create({ overlay: { flex: 1, justifyContent: "flex-end", }, backdrop: { flex: 1, backgroundColor: "rgba(0, 0, 0, 0.5)", }, content: { borderTopLeftRadius: 24, borderTopRightRadius: 24, borderWidth: 1, borderBottomWidth: 0, padding: 24, paddingBottom: 40, }, header: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", marginBottom: 24, }, form: { gap: 16, }, field: {}, input: { borderWidth: 1, borderRadius: 12, padding: 14, fontSize: 16, }, textArea: { minHeight: 80, textAlignVertical: "top", }, submitButton: { marginTop: 24, borderRadius: 12, paddingVertical: 14, alignItems: "center", }, submitDisabled: { opacity: 0.5, }, submitText: { color: "#fff", fontWeight: "600", fontSize: 16, }, });