Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/oss-slu/lrda_website into 1…
Browse files Browse the repository at this point in the history
…68-intro-skip
  • Loading branch information
amychen108 committed Nov 4, 2024
2 parents c44800f + 089cfa5 commit e841a7f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/lib/components/noteElements/note_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,14 @@ export default function NoteEditor({
<AlertDialogTrigger asChild>
<button className="hover:text-red-500 flex justify-center items-center w-full">
<FileX2 className="text-current"/>
<div className="ml-2" ref = {deleteRef}>Delete</div>
<div className="ml-2" ref = {deleteRef}>Archive</div>
</button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete
This action cannot be undone. This will permanently archive
this note.
</AlertDialogDescription>
</AlertDialogHeader>
Expand Down
31 changes: 20 additions & 11 deletions app/lib/components/noteElements/note_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,46 @@ export const handleEditorChange = (
setEditorContent(content);
};

export const handleDeleteNote = async (
export const handleDeleteNote = async ( //supposed to be archive but named as delete
note: Note | undefined,
user: User,
setNote: React.Dispatch<React.SetStateAction<Note | undefined>>
) => {
if (note?.id) {
try {
const userId = await user.getId();
const success = await ApiService.deleteNoteFromAPI(
note!.id,
userId || ""
);
if (success) {

// Step 1: Add an `isArchived` flag and `archivedAt` timestamp to the note
const updatedNote = {
...note,
isArchived: true, // Mark the note as archived; this IS happening
archivedAt: new Date().toISOString(), // Add a timestamp for archiving
};

// update the note
const response = await ApiService.overwriteNote(updatedNote);

if (response.ok) {
toast("Success", {
description: "Note successfully deleted.",
description: "Note successfully archived.",
duration: 4000,
});
setNote(undefined); // Clear the note after successful deletion
setNote(undefined); // Clear the note from the state after archiving
return true;
} else {
throw new Error("Archiving failed");
}
} catch (error) {
toast("Error", {
description: "Failed to delete note. System failure. Try again later.",
description: "Failed to archive note. System failure. Try again later.",
duration: 4000,
});
console.error("Error deleting note:", error);
console.error("Error archiving note:", error);
return false;
}
} else {
toast("Error", {
description: "You must first save your note before deleting it.",
description: "You must first save your note before archiving it.",
duration: 4000,
});
return false;
Expand Down
12 changes: 7 additions & 5 deletions app/lib/components/note_listview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ const NoteListView: React.FC<NoteListViewProps> = ({ notes, onNoteSelect }) => {
const [selectedNoteId, setSelectedNoteId] = useState<string | null>(null);
const [fresh, setFresh] = useState(true);

const visibleNotes = notes.filter(note => !note.isArchived); //filter ?

useEffect(() => {
if (notes.length > 0 && fresh) {
onNoteSelect(notes[0], false);
setSelectedNoteId(notes[0].id);
if (visibleNotes.length > 0 && fresh) {
onNoteSelect(visibleNotes[0], false);
setSelectedNoteId(visibleNotes[0].id);
setFresh(false);
}
}, [notes, onNoteSelect, fresh]);
}, [visibleNotes, onNoteSelect, fresh]);

const handleLoadText = (note: Note) => {
onNoteSelect(note, false);
Expand All @@ -49,7 +51,7 @@ const NoteListView: React.FC<NoteListViewProps> = ({ notes, onNoteSelect }) => {

return (
<div id="notes-list" className="my-4 flex flex-col">
{notes.map((note) => {
{visibleNotes.map((note) => {
const noteTextContent = extractTextFromHtml(note.text);

return (
Expand Down
16 changes: 10 additions & 6 deletions app/lib/components/side_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Sidebar: React.FC<SidebarProps> = ({ onNoteSelect }) => {
longitude: "",
published: undefined,
tags: [],
isArchived: false
};
onNoteSelect(newBlankNote, true); // Notify that a new note is being added
} else {
Expand Down Expand Up @@ -75,21 +76,24 @@ const Sidebar: React.FC<SidebarProps> = ({ onNoteSelect }) => {
try {
const userId = await user.getId();
if (userId) {
const userNotes = await ApiService.fetchUserMessages(userId);
const convertedNotes =
DataConversion.convertMediaTypes(userNotes).reverse();
setNotes(convertedNotes);
setFilteredNotes(convertedNotes);
const userNotes = (await ApiService.fetchUserMessages(userId)).filter((note) => !note.isArchived); // filter here?
const convertedNotes = DataConversion.convertMediaTypes(userNotes).reverse();

const unarchivedNotes = convertedNotes.filter((note) => !note.isArchived); //filter out archived notes

setNotes(unarchivedNotes);
setFilteredNotes(unarchivedNotes);
} else {
console.error("User not logged in");
}
} catch (error) {
console.error("Error fetching user messages:", error);
}
};

fetchUserMessages();
}, []);


const handleSearch = (searchQuery: string) => {
if (!searchQuery.trim()) {
Expand Down
24 changes: 16 additions & 8 deletions app/lib/pages/map/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,32 @@ const Page = () => {
const fetchNotes = async () => {
try {
const userId = await user.getId();

let personalNotes: Note[] = [];
let globalNotes: Note[] = [];
if (userId) {
setIsLoggedIn(true);
personalNotes = await ApiService.fetchUserMessages(userId);
personalNotes =
DataConversion.convertMediaTypes(personalNotes).reverse();
personalNotes = (await ApiService.fetchUserMessages(userId)).filter(note => !note.isArchived); //filter here?

// Convert media types and filter out archived notes for personal notes
personalNotes = DataConversion.convertMediaTypes(personalNotes)
.reverse()
.filter(note => !note.isArchived); // Filter out archived personal notes
}
globalNotes = await ApiService.fetchPublishedNotes();
globalNotes = DataConversion.convertMediaTypes(globalNotes).reverse();


globalNotes = (await ApiService.fetchPublishedNotes()).filter(note => !note.isArchived);

// Convert media types and filter out archived notes for global notes
globalNotes = DataConversion.convertMediaTypes(globalNotes)
.reverse()
.filter(note => !note.isArchived); // Filter out archived global notes

return { personalNotes, globalNotes };
} catch (error) {
console.error("Error fetching messages:", error);
return { personalNotes: [], globalNotes: [] };
}
};
};

const handleMarkerClick = (note: Note) => {
if (currentPopup) {
Expand Down
1 change: 1 addition & 0 deletions app/lib/utils/api_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ static async fetchPublishedNotes(limit: number = 150, skip: number = 0): Promise
published: note.published,
tags: note.tags,
time: note.time,
isArchived: note.isArchived,
}),
});
}
Expand Down
2 changes: 2 additions & 0 deletions app/lib/utils/data_conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default class DataConversion {
published: message.published || false,
tags: message.tags || [],
uid: message.uid, // Add the uid property here
//isArchived: message.isArchived
};
});

Expand Down Expand Up @@ -157,6 +158,7 @@ export default class DataConversion {
published: note?.published || false,
tags: note?.tags || [],
uid: note.uid, // Add the uid property here
//isArchived: note.isArchived
},
};
}
Expand Down
2 changes: 2 additions & 0 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type Note = {
published: boolean | undefined;
tags: Tag[];
uid: string;
isArchived?: boolean; //add property of archived, then filter for it
};

export type CombinedResult =
Expand All @@ -56,6 +57,7 @@ export type newNote = {
longitude: string;
published: boolean | undefined;
tags: Tag[];
isArchived?: boolean;
};

export type RootStackParamList = {
Expand Down

0 comments on commit e841a7f

Please sign in to comment.