Skip to content

Commit

Permalink
fixing the test cases and the build for the finish version to run
Browse files Browse the repository at this point in the history
  • Loading branch information
yashb196 committed Aug 12, 2024
1 parent 5dc6a4f commit 7940ae4
Show file tree
Hide file tree
Showing 8 changed files with 3,152 additions and 3,069 deletions.
45 changes: 28 additions & 17 deletions app/lib/components/noteElements/note_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
handleDeleteNote,
handleEditorChange,
handleLocationChange,
handleTagsChange,
handleTagsChange, // Imported from note_handler
handleTimeChange,
handlePublishChange,
} from "./note_handler";
Expand Down Expand Up @@ -103,6 +103,7 @@ export default function NoteEditor({
noteHandlers.setNote(initialNote as Note);
noteHandlers.setEditorContent(initialNote.text || "");
noteHandlers.setTitle(initialNote.title || "");

noteHandlers.setImages(
(initialNote.media.filter(
(item) => item.getType() === "image"
Expand All @@ -111,7 +112,13 @@ export default function NoteEditor({
noteHandlers.setTime(initialNote.time || new Date());
noteHandlers.setLongitude(initialNote.longitude || "");
noteHandlers.setLatitude(initialNote.latitude || "");
noteHandlers.setTags(initialNote.tags || []);

noteHandlers.setTags(
(initialNote.tags || []).map((tag) =>
typeof tag === "string" ? { label: tag, origin: "user" } : tag
)
);

noteHandlers.setAudio(initialNote.audio || []);
noteHandlers.setIsPublished(initialNote.published || false);
noteHandlers.setCounter((prevCounter) => prevCounter + 1);
Expand Down Expand Up @@ -175,11 +182,11 @@ export default function NoteEditor({
const addImageToNote = (imageUrl: string) => {
console.log("Before updating images", noteState.images);
const newImage = {
type: 'image',
type: "image",
attrs: {
src: imageUrl,
alt: 'Image description',
loading: 'lazy',
alt: "Image description",
loading: "lazy",
},
};

Expand All @@ -193,11 +200,14 @@ export default function NoteEditor({
}

noteHandlers.setImages((prevImages) => {
const newImages = [...prevImages, new PhotoType({
uuid: uuidv4(),
uri: imageUrl,
type: "image",
})];
const newImages = [
...prevImages,
new PhotoType({
uuid: uuidv4(),
uri: imageUrl,
type: "image",
}),
];
console.log("After updating images", newImages);
return newImages;
});
Expand Down Expand Up @@ -330,13 +340,14 @@ export default function NoteEditor({
</button>
</div>
<TagManager
inputTags={noteState.tags}
suggestedTags={suggestedTags}
onTagsChange={(newTags) =>
handleTagsChange(noteHandlers.setTags, newTags)
}
fetchSuggestedTags={fetchSuggestedTags}
/>
inputTags={noteState.tags}
suggestedTags={suggestedTags}
onTagsChange={(newTags) =>
handleTagsChange(noteHandlers.setTags, newTags) // Ensure it uses the updated function
}
fetchSuggestedTags={fetchSuggestedTags}
/>

{loadingTags && <p>Loading suggested tags...</p>}
</div>

Expand Down
21 changes: 14 additions & 7 deletions app/lib/components/noteElements/note_handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { Note } from "@/app/types";
import { Note, Tag } from "@/app/types";
import ApiService from "../../utils/api_service";
import { toast } from "sonner";
import { User } from "../../models/user_class";
Expand Down Expand Up @@ -36,10 +36,15 @@ export const handlePublishChange = (
};

export const handleTagsChange = (
setTags: React.Dispatch<React.SetStateAction<string[]>>,
newTags: string[]
setTags: React.Dispatch<React.SetStateAction<Tag[]>>,
newTags: (Tag | string)[]
) => {
setTags(newTags);
const formattedTags = newTags.map((tag) =>
typeof tag === "string"
? { label: tag, origin: "user" as const } // Ensure origin is correctly typed
: tag
);
setTags(formattedTags);
};

export const handleEditorChange = (
Expand All @@ -62,10 +67,11 @@ export const handleDeleteNote = async (
userId || ""
);
if (success) {
toast("Error", {
description: "Note successfully Deleted.",
toast("Success", {
description: "Note successfully deleted.",
duration: 4000,
});
setNote(undefined); // Clear the note after successful deletion
return true;
}
} catch (error) {
Expand All @@ -78,8 +84,9 @@ export const handleDeleteNote = async (
}
} else {
toast("Error", {
description: "You must first save your note, before deleting it.",
description: "You must first save your note before deleting it.",
duration: 4000,
});
return false;
}
};
6 changes: 3 additions & 3 deletions app/lib/components/noteElements/note_state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react";
import { Note } from "@/app/types";
import { AudioType, VideoType, PhotoType, Media } from "../../models/media_class";
import { Note, Tag } from "@/app/types";
import { AudioType, VideoType, PhotoType } from "../../models/media_class";

const useNoteState = (initialNote: Note | undefined) => {
const [note, setNote] = useState<Note | undefined>(initialNote);
Expand All @@ -13,7 +13,7 @@ const useNoteState = (initialNote: Note | undefined) => {
const [audio, setAudio] = useState<AudioType[]>(initialNote?.audio || []);
const [longitude, setLongitude] = useState<string>(initialNote?.longitude || "");
const [latitude, setLatitude] = useState<string>(initialNote?.latitude || "");
const [tags, setTags] = useState<string[]>(initialNote?.tags || []);
const [tags, setTags] = useState<Tag[]>(initialNote?.tags || []); // Update to Tag[]
const [isPublished, setIsPublished] = useState<boolean>(initialNote?.published || false);
const [counter, setCounter] = useState<number>(0);

Expand Down
38 changes: 19 additions & 19 deletions app/lib/components/noteElements/tag_manager.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react";
import React, { useState, useEffect, useMemo } from "react";
import { XIcon } from "lucide-react";
import { Input } from "@/components/ui/input";
import { toast } from "sonner";
Expand All @@ -24,12 +24,14 @@ const TagManager: React.FC<TagManagerProps> = ({
onTagsChange,
fetchSuggestedTags,
}) => {
// Convert old tags (strings) to new format
const convertOldTags = (tags: (Tag | string)[]): Tag[] => {
return tags.map(tag =>
typeof tag === "string" ? { label: tag, origin: "user" } : tag
);
};
// Memoize conversion of old tags to new format to avoid unnecessary recalculations
const convertOldTags = useMemo(() => {
return (tags: (Tag | string)[]): Tag[] => {
return tags.map(tag =>
typeof tag === "string" ? { label: tag, origin: "user" } : tag
);
};
}, []);

// Initialize state with converted tags
const [tags, setTags] = useState<Tag[]>(convertOldTags(inputTags));
Expand All @@ -38,11 +40,10 @@ const TagManager: React.FC<TagManagerProps> = ({
// Update local state when inputTags prop changes
useEffect(() => {
setTags(convertOldTags(inputTags));
}, [inputTags]);
}, [inputTags, convertOldTags]);

// Function to add a tag
const addTag = (tag: string, origin: "user" | "ai") => {
// Validate tag: no spaces and longer than 2 characters
if (tag.includes(" ")) {
toast("Failed to add tag", {
description: "Your tag must not contain spaces.",
Expand All @@ -56,9 +57,9 @@ const TagManager: React.FC<TagManagerProps> = ({
description: "Tags must be longer than 2 characters.",
duration: 2000,
});
setTagInput("");
return;
}
// Check for duplicate tags
if (tags.find((t) => t.label === tag)) {
toast("Failed to add tag", {
description: "Duplicate tags are not allowed.",
Expand All @@ -67,16 +68,15 @@ const TagManager: React.FC<TagManagerProps> = ({
setTagInput("");
return;
}

// Add the tag if valid
if (tag && tag.length > 2 && !tag.includes(" ")) {
const newTag = { label: tag, origin };
setTags((prevTags) => {
const updatedTags = [...prevTags, newTag];
onTagsChange(updatedTags); // Notify parent component of tag changes
return updatedTags;
});
setTagInput(""); // Clear input field
}
const newTag = { label: tag, origin };
setTags((prevTags) => {
const updatedTags = [...prevTags, newTag];
onTagsChange(updatedTags); // Notify parent component of tag changes
return updatedTags;
});
setTagInput(""); // Clear input field
};

// Function to remove a tag
Expand Down
2 changes: 1 addition & 1 deletion app/lib/components/side_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const Sidebar: React.FC<SidebarProps> = ({ onNoteSelect }) => {
const filtered = notes.filter(
(note) =>
note.title.toLowerCase().includes(query) ||
note.tags.some((tag) => tag.toLowerCase().includes(query))
note.tags.some((tag) => tag.label.toLowerCase().includes(query)) // Access the label property
);
setFilteredNotes(filtered);
};
Expand Down
Loading

0 comments on commit 7940ae4

Please sign in to comment.