From 8d5018318d8559f8b446fe697858adbf155bf391 Mon Sep 17 00:00:00 2001 From: Anmol Singh Bhatia <121005188+anmolsinghbhatia@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:42:50 +0530 Subject: [PATCH] chore: add favorite project from sidebar (#1909) --- .../project/create-project-modal.tsx | 34 +++++++++++++- apps/app/components/project/sidebar-list.tsx | 44 ++++++++++++------- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/apps/app/components/project/create-project-modal.tsx b/apps/app/components/project/create-project-modal.tsx index c48c8ec55..b6a2be8bb 100644 --- a/apps/app/components/project/create-project-modal.tsx +++ b/apps/app/components/project/create-project-modal.tsx @@ -42,6 +42,7 @@ import { NETWORK_CHOICES } from "constants/project"; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; + setToFavorite?: boolean; user: ICurrentUserResponse | undefined; }; @@ -74,7 +75,12 @@ const IsGuestCondition: React.FC<{ return null; }; -export const CreateProjectModal: React.FC = ({ isOpen, setIsOpen, user }) => { +export const CreateProjectModal: React.FC = ({ + isOpen, + setIsOpen, + setToFavorite = false, + user, +}) => { const [isChangeInIdentifierRequired, setIsChangeInIdentifierRequired] = useState(true); const { setToastAlert } = useToast(); @@ -104,6 +110,29 @@ export const CreateProjectModal: React.FC = ({ isOpen, setIsOpen, user }) reset(defaultValues); }; + const handleAddToFavorites = (projectId: string) => { + if (!workspaceSlug) return; + + mutate( + PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all" }), + (prevData) => + (prevData ?? []).map((p) => (p.id === projectId ? { ...p, is_favorite: true } : p)), + false + ); + + projectServices + .addProjectToFavorites(workspaceSlug as string, { + project: projectId, + }) + .catch(() => + setToastAlert({ + type: "error", + title: "Error!", + message: "Couldn't remove the project from favorites. Please try again.", + }) + ); + }; + const onSubmit = async (formData: IProject) => { if (!workspaceSlug) return; @@ -125,6 +154,9 @@ export const CreateProjectModal: React.FC = ({ isOpen, setIsOpen, user }) title: "Success!", message: "Project created successfully.", }); + if (setToFavorite) { + handleAddToFavorites(res.id); + } handleClose(); }) .catch((err) => { diff --git a/apps/app/components/project/sidebar-list.tsx b/apps/app/components/project/sidebar-list.tsx index 29dafd6cf..266b05204 100644 --- a/apps/app/components/project/sidebar-list.tsx +++ b/apps/app/components/project/sidebar-list.tsx @@ -13,7 +13,7 @@ import useTheme from "hooks/use-theme"; import useUserAuth from "hooks/use-user-auth"; import useProjects from "hooks/use-projects"; // components -import { DeleteProjectModal, SingleSidebarProject } from "components/project"; +import { CreateProjectModal, DeleteProjectModal, SingleSidebarProject } from "components/project"; // services import projectService from "services/project.service"; // icons @@ -32,6 +32,7 @@ import { useMobxStore } from "lib/mobx/store-provider"; export const ProjectSidebarList: FC = () => { const store: any = useMobxStore(); + const [isProjectModalOpen, setIsProjectModalOpen] = useState(false); const [deleteProjectModal, setDeleteProjectModal] = useState(false); const [projectToDelete, setProjectToDelete] = useState(null); @@ -151,6 +152,12 @@ export const ProjectSidebarList: FC = () => { return ( <> + setDeleteProjectModal(false)} @@ -172,17 +179,25 @@ export const ProjectSidebarList: FC = () => { {({ open }) => ( <> {!store?.theme?.sidebarCollapsed && ( - - Favorites - - +
+ + Favorites + + + +
)} {orderedFavProjects.map((project, index) => ( @@ -241,10 +256,7 @@ export const ProjectSidebarList: FC = () => {