import { Ionicons } from "@expo/vector-icons"; import { useLocalSearchParams, useRouter } from "expo-router"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { ActivityIndicator, KeyboardAvoidingView, Platform, ScrollView, Switch, TextInput, TouchableOpacity, View, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { Button } from "@/components/Button"; import { Text } from "@/components/common/Text"; import { useUpdateWatchlist } from "@/hooks/useWatchlistMutations"; import { useWatchlistDetailQuery } from "@/hooks/useWatchlists"; import type { StreamystatsWatchlistAllowedItemType, StreamystatsWatchlistSortOrder, } from "@/utils/streamystats/types"; const ITEM_TYPES: Array<{ value: StreamystatsWatchlistAllowedItemType; label: string; }> = [ { value: null, label: "All Types" }, { value: "Movie", label: "Movies Only" }, { value: "Series", label: "Series Only" }, { value: "Episode", label: "Episodes Only" }, ]; const SORT_OPTIONS: Array<{ value: StreamystatsWatchlistSortOrder; label: string; }> = [ { value: "custom", label: "Custom Order" }, { value: "name", label: "Name" }, { value: "dateAdded", label: "Date Added" }, { value: "releaseDate", label: "Release Date" }, ]; export default function EditWatchlistScreen() { const { t } = useTranslation(); const router = useRouter(); const insets = useSafeAreaInsets(); const { watchlistId } = useLocalSearchParams<{ watchlistId: string }>(); const watchlistIdNum = watchlistId ? Number.parseInt(watchlistId, 10) : undefined; const { data: watchlist, isLoading } = useWatchlistDetailQuery(watchlistIdNum); const updateWatchlist = useUpdateWatchlist(); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [isPublic, setIsPublic] = useState(false); const [allowedItemType, setAllowedItemType] = useState(null); const [defaultSortOrder, setDefaultSortOrder] = useState("custom"); // Initialize form with watchlist data useEffect(() => { if (watchlist) { setName(watchlist.name); setDescription(watchlist.description ?? ""); setIsPublic(watchlist.isPublic); setAllowedItemType( (watchlist.allowedItemType as StreamystatsWatchlistAllowedItemType) ?? null, ); setDefaultSortOrder( (watchlist.defaultSortOrder as StreamystatsWatchlistSortOrder) ?? "custom", ); } }, [watchlist]); const handleSave = useCallback(async () => { if (!name.trim() || !watchlistIdNum) return; try { await updateWatchlist.mutateAsync({ watchlistId: watchlistIdNum, data: { name: name.trim(), description: description.trim() || undefined, isPublic, allowedItemType, defaultSortOrder, }, }); router.back(); } catch { // Error handled by mutation } }, [ name, description, isPublic, allowedItemType, defaultSortOrder, watchlistIdNum, updateWatchlist, router, ]); if (isLoading) { return ( ); } if (!watchlist) { return ( {t("watchlists.not_found")} ); } return ( {/* Name */} {t("watchlists.name_label")} * {/* Description */} {t("watchlists.description_label")} {/* Public Toggle */} {t("watchlists.is_public_label")} {t("watchlists.is_public_description")} {/* Content Type */} {t("watchlists.allowed_type_label")} {ITEM_TYPES.map((type) => ( setAllowedItemType(type.value)} className={`px-4 py-2 rounded-lg ${allowedItemType === type.value ? "bg-purple-600" : "bg-neutral-800"}`} > {type.label} ))} {/* Sort Order */} {t("watchlists.sort_order_label")} {SORT_OPTIONS.map((sort) => ( setDefaultSortOrder(sort.value)} className={`px-4 py-2 rounded-lg ${defaultSortOrder === sort.value ? "bg-purple-600" : "bg-neutral-800"}`} > {sort.label} ))} {/* Save Button */} ); }