Skip to content

Commit

Permalink
Changes .js file to .tsx file and introduces Bookmark type for linter…
Browse files Browse the repository at this point in the history
… fixes and better type handling
  • Loading branch information
r800360 committed Mar 15, 2024
1 parent 4228306 commit 0c7c06e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 79 deletions.
3 changes: 0 additions & 3 deletions dfm-sideline-sidekick-app/App.tsx
Original file line number Diff line number Diff line change
@@ -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";

Check failure on line 5 in dfm-sideline-sidekick-app/App.tsx

View workflow job for this annotation

GitHub Actions / Frontend lint and style check

'React' is defined but never used. Allowed unused vars must match /^_/u
import { StyleSheet } from "react-native";

Expand Down
10 changes: 5 additions & 5 deletions dfm-sideline-sidekick-app/components/bookmark.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,7 +12,7 @@ type bookmarkProps = {
export const Bookmark: React.FC<bookmarkProps> = ({ item }) => {
const [selectedItemId, setSelectedItemId] = useState(0);

useEffect( ()=>{
useEffect(() => {
async function checkExistence() {
const exists = await findBookmark(item);
if (exists) {
Expand All @@ -29,11 +29,11 @@ export const Bookmark: React.FC<bookmarkProps> = ({ item }) => {
} else {
void deleteBookmark(item);
setSelectedItemId(0);
};
}
};

return (
<TouchableOpacity
<Pressable
onPress={() => {
handleBookmarkClick();
}}
Expand All @@ -43,6 +43,6 @@ export const Bookmark: React.FC<bookmarkProps> = ({ item }) => {
) : (
<BookmarkIcon fillColor={"#001F3F"} />
)}
</TouchableOpacity>
</Pressable>
);
};
70 changes: 0 additions & 70 deletions dfm-sideline-sidekick-app/components/bookmarkRoutes.js

This file was deleted.

85 changes: 85 additions & 0 deletions dfm-sideline-sidekick-app/components/bookmarkRoutes.tsx
Original file line number Diff line number Diff line change
@@ -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<Bookmark[]> => {
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);
}
};
1 change: 0 additions & 1 deletion dfm-sideline-sidekick-app/pages/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const SearchPage: React.FC<SearchPageProps> = ({ 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.",
Expand Down

0 comments on commit 0c7c06e

Please sign in to comment.