/**
 * React Native SQLite Demo
 * Copyright (c) 2021 Bruce Lefebvre <bruce@brucelefebvre.com>
 * https://github.com/blefebvre/react-native-sqlite-demo/blob/master/LICENSE
 */ import { useEffect } from "react" ; import { List } from "../types/List" ; import { useDatabase , useListsContext , useSetListsContext } from "../context/DatabaseContext" ; export function useLists ( ) { const lists = useListsContext ( ) ; const setLists = useSetListsContext ( ) ; const database = useDatabase ( ) ; useEffect ( ( ) => { refreshLists ( ) ; } , [ ] ) ; function refreshLists ( ) { return database . getAllLists ( ) . then ( setLists ) ; } function createList ( newListTitle ) { return database . createList ( newListTitle ) . then ( refreshLists ) ; } function deleteList ( listToDelete ) { if ( listToDelete !== undefined ) { return database . deleteList ( listToDelete ) . then ( refreshLists ) ; } return Promise . reject ( Error ( "Could not delete an undefined list" ) ) ; } return { lists , createList , deleteList , refreshLists , } ; }