From 0c7c06e675f717fb81c42e2f171bd904693b2d86 Mon Sep 17 00:00:00 2001 From: Rohan Sachdeva Date: Thu, 14 Mar 2024 18:16:14 -0700 Subject: [PATCH] Changes .js file to .tsx file and introduces Bookmark type for linter fixes and better type handling --- dfm-sideline-sidekick-app/App.tsx | 3 - .../components/bookmark.tsx | 10 +-- .../components/bookmarkRoutes.js | 70 --------------- .../components/bookmarkRoutes.tsx | 85 +++++++++++++++++++ .../pages/SearchPage.tsx | 1 - 5 files changed, 90 insertions(+), 79 deletions(-) delete mode 100644 dfm-sideline-sidekick-app/components/bookmarkRoutes.js create mode 100644 dfm-sideline-sidekick-app/components/bookmarkRoutes.tsx diff --git a/dfm-sideline-sidekick-app/App.tsx b/dfm-sideline-sidekick-app/App.tsx index 4a84b92..7ec1237 100644 --- a/dfm-sideline-sidekick-app/App.tsx +++ b/dfm-sideline-sidekick-app/App.tsx @@ -1,10 +1,7 @@ import { NavigationContainer, useNavigation } from "@react-navigation/native"; import { createNativeStackNavigator } from "@react-navigation/native-stack"; import { StackNavigationProp } from "@react-navigation/stack"; -// eslint-disable-next-line import/order import { StatusBar } from "expo-status-bar"; - -// eslint-disable-next-line @typescript-eslint/no-unused-vars import React from "react"; import { StyleSheet } from "react-native"; diff --git a/dfm-sideline-sidekick-app/components/bookmark.tsx b/dfm-sideline-sidekick-app/components/bookmark.tsx index 43ad2ef..0b51f0a 100644 --- a/dfm-sideline-sidekick-app/components/bookmark.tsx +++ b/dfm-sideline-sidekick-app/components/bookmark.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from "react"; -import { TouchableOpacity } from "react-native"; +import { Pressable } from "react-native"; import { BookmarkIcon, BookmarkTag } from "../icons/bookmarkIcon"; @@ -12,7 +12,7 @@ type bookmarkProps = { export const Bookmark: React.FC = ({ item }) => { const [selectedItemId, setSelectedItemId] = useState(0); - useEffect( ()=>{ + useEffect(() => { async function checkExistence() { const exists = await findBookmark(item); if (exists) { @@ -29,11 +29,11 @@ export const Bookmark: React.FC = ({ item }) => { } else { void deleteBookmark(item); setSelectedItemId(0); - }; + } }; return ( - { handleBookmarkClick(); }} @@ -43,6 +43,6 @@ export const Bookmark: React.FC = ({ item }) => { ) : ( )} - + ); }; diff --git a/dfm-sideline-sidekick-app/components/bookmarkRoutes.js b/dfm-sideline-sidekick-app/components/bookmarkRoutes.js deleted file mode 100644 index 985b614..0000000 --- a/dfm-sideline-sidekick-app/components/bookmarkRoutes.js +++ /dev/null @@ -1,70 +0,0 @@ -// import {AsyncStorage} from '@react-native-async-storage/async-storage'; -// -const AsyncStorage = require("@react-native-async-storage/async-storage").default; - -export const createBookmark = async (item) => { - try { - let bookmarks = JSON.parse(await AsyncStorage.getItem("bookmarks")) || []; - const exists = bookmarks.includes(item); - if (exists) { - throw new Error("Bookmark already exists!"); - } - bookmarks.push(item); - console.log(item); - console.log(bookmarks); - await AsyncStorage.setItem("bookmarks", JSON.stringify(bookmarks)); - console.log("Bookmark created:", item); - } catch (error) { - throw new Error("Error creating bookmark: " + error.message); - } -}; - -export const deleteBookmark = async (item) => { - try { - const existingBookmarks = JSON.parse(await AsyncStorage.getItem("bookmarks")) || []; - const index = existingBookmarks.findIndex((bookmark) => bookmark.title === item.title); - if (index !== -1) { - existingBookmarks.splice(index, 1); - await AsyncStorage.setItem("bookmarks", JSON.stringify(existingBookmarks)); - console.log("Bookmark deleted!"); - } else { - console.log("Bookmark not found!"); - } - } catch (error) { - console.error("Error deleting bookmark:", error); - } -}; - -export const findBookmark = async (item) => { - try { - const existingBookmarks = JSON.parse(await AsyncStorage.getItem("bookmarks")) || []; - const index = existingBookmarks.findIndex((bookmark) => bookmark.title === item.title); - if (index !== -1) { - return true; - } else { - return false; - } - } catch (error) { - console.error("Error finding bookmark:", error); - } -} - -export const getAllBookmarks = async () => { - try { - const existingBookmarks = JSON.parse(await AsyncStorage.getItem("bookmarks")) || []; - return existingBookmarks; - } catch (error) { - console.error("Error getting bookmarks:", error); - return []; - } -}; - -// Function for testing to clear all bookmarks from AsyncStorage -export const clearBookmarks = async () => { - try { - await AsyncStorage.removeItem("bookmarks"); - console.log("Bookmarks cleared successfully."); - } catch (error) { - console.error("Error clearing bookmarks:", error); - } -}; diff --git a/dfm-sideline-sidekick-app/components/bookmarkRoutes.tsx b/dfm-sideline-sidekick-app/components/bookmarkRoutes.tsx new file mode 100644 index 0000000..f2df891 --- /dev/null +++ b/dfm-sideline-sidekick-app/components/bookmarkRoutes.tsx @@ -0,0 +1,85 @@ +import AsyncStorage, { AsyncStorageStatic } from "@react-native-async-storage/async-storage"; + +const asyncStorage: AsyncStorageStatic = AsyncStorage; + +type Bookmark = { + _id: string; + subtitle: string; + title: string; + overview?: object; + treatment?: object; + content?: object; +}; + +export const createBookmark = async (item: Bookmark) => { + try { + const bookmarksJSON: string | null = await asyncStorage.getItem("bookmarks"); + const bookmarks: Bookmark[] = bookmarksJSON ? (JSON.parse(bookmarksJSON) as Bookmark[]) : []; + const exists = bookmarks.some((bookmark) => bookmark._id === item._id); + if (exists) { + throw new Error("Bookmark already exists!"); + } + bookmarks.push(item); + console.log(item); + console.log(bookmarks); + await asyncStorage.setItem("bookmarks", JSON.stringify(bookmarks)); + console.log("Bookmark created:", item); + } catch (error) { + console.error("Error creating bookmark: ", error); + throw new Error("Error creating bookmark: " + error); + } +}; + +export const deleteBookmark = async (item: Bookmark) => { + try { + const bookmarksJSON: string | null = await asyncStorage.getItem("bookmarks"); + const existingBookmarks: Bookmark[] = bookmarksJSON + ? (JSON.parse(bookmarksJSON) as Bookmark[]) + : []; + const index = existingBookmarks.findIndex((bookmark) => bookmark.title === item.title); + if (index !== -1) { + existingBookmarks.splice(index, 1); + await asyncStorage.setItem("bookmarks", JSON.stringify(existingBookmarks)); + console.log("Bookmark deleted!"); + } else { + console.log("Bookmark not found!"); + } + } catch (error) { + console.error("Error deleting bookmark:", error); + } +}; + +export const findBookmark = async (item: Bookmark) => { + try { + const bookmarksJSON: string | null = await asyncStorage.getItem("bookmarks"); + const existingBookmarks: Bookmark[] = bookmarksJSON + ? (JSON.parse(bookmarksJSON) as Bookmark[]) + : []; + const index = existingBookmarks.findIndex((bookmark) => bookmark.title === item.title); + return index !== -1; + } catch (error) { + console.error("Error finding bookmark:", error); + } +}; + +export const getAllBookmarks = async (): Promise => { + try { + const bookmarksJSON: string | null = await asyncStorage.getItem("bookmarks"); + const existingBookmarks: Bookmark[] = bookmarksJSON + ? (JSON.parse(bookmarksJSON) as Bookmark[]) + : []; + return existingBookmarks; + } catch (error) { + console.error("Error getting bookmarks:", error); + return []; + } +}; + +export const clearBookmarks = async () => { + try { + await asyncStorage.removeItem("bookmarks"); + console.log("Bookmarks cleared successfully."); + } catch (error) { + console.error("Error clearing bookmarks:", error); + } +}; diff --git a/dfm-sideline-sidekick-app/pages/SearchPage.tsx b/dfm-sideline-sidekick-app/pages/SearchPage.tsx index 5ec01c1..41b08a2 100644 --- a/dfm-sideline-sidekick-app/pages/SearchPage.tsx +++ b/dfm-sideline-sidekick-app/pages/SearchPage.tsx @@ -43,7 +43,6 @@ const SearchPage: React.FC = ({ onPage = true, setShowing }) => const allDocuments = [...emergencies, ...generalPrinciples]; const matchedDocuments = searchDocuments(allDocuments, text).map((doc) => ({ ...doc, - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment _id: doc._id ?? "fallback-id", subtitle: doc.subtitle ?? "Lorem ipsum dolor sit amet, consectetur adip iscing elit, sed do.",