From bbadca6652814caca2c21d19e3cdc9cccb308a4b Mon Sep 17 00:00:00 2001 From: Jerry Abadi Date: Mon, 22 Jan 2024 11:21:34 +0100 Subject: [PATCH 1/2] fixing errors --- .../workspace/[workspaceId]/project/[projectId]/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/(platform)/(dashboard)/workspace/[workspaceId]/project/[projectId]/page.tsx b/app/(platform)/(dashboard)/workspace/[workspaceId]/project/[projectId]/page.tsx index 300e2d3..89ab40d 100644 --- a/app/(platform)/(dashboard)/workspace/[workspaceId]/project/[projectId]/page.tsx +++ b/app/(platform)/(dashboard)/workspace/[workspaceId]/project/[projectId]/page.tsx @@ -269,7 +269,7 @@ const ProjectIdPage = () => { From 9e0d8d319652ca0f8ba76587c6d973667ddf97b9 Mon Sep 17 00:00:00 2001 From: Jerry Abadi Date: Wed, 31 Jan 2024 15:27:24 +0100 Subject: [PATCH 2/2] worked on deleting a workspace --- .../workspace/[workspaceId]/page.tsx | 43 +++++++----- .../workspace/[workspaceId]/settings/page.tsx | 10 +-- app/not-found.tsx | 28 ++++---- components/shared/Protected.tsx | 23 +++++-- hooks/useProject.ts | 34 +++++----- hooks/useWorkspace.ts | 67 +++++++++++-------- 6 files changed, 114 insertions(+), 91 deletions(-) diff --git a/app/(platform)/(dashboard)/workspace/[workspaceId]/page.tsx b/app/(platform)/(dashboard)/workspace/[workspaceId]/page.tsx index fa3c0c6..aaac8c8 100644 --- a/app/(platform)/(dashboard)/workspace/[workspaceId]/page.tsx +++ b/app/(platform)/(dashboard)/workspace/[workspaceId]/page.tsx @@ -1,14 +1,20 @@ -'use client'; +"use client"; -import Header from '@/components/workspace-components/header'; -import ProjectCard from '@/components/project-components/project-card'; -import { getProjectsByWorkspaceId } from '@/db/mock.json'; -import { FetchWorkspaceProjectData } from '@/services/projectService'; -import { Skeleton } from '@/components/ui/skeleton'; +import Header from "@/components/workspace-components/header"; +import ProjectCard from "@/components/project-components/project-card"; +import { getProjectsByWorkspaceId } from "@/db/mock.json"; +import { FetchWorkspaceProjectData } from "@/services/projectService"; +import { Skeleton } from "@/components/ui/skeleton"; -import useProject from '@/hooks/useProject'; -import { formatDate } from '@/lib/utils'; -import { Ghost } from 'lucide-react'; +import useProject from "@/hooks/useProject"; +import { formatDate } from "@/lib/utils"; +import { Ghost } from "lucide-react"; +import { notFound, useRouter } from "next/navigation"; +import NotFound from "@/app/not-found"; +import { use, useEffect } from "react"; +import { AxiosError } from "axios"; +import { toast } from "sonner"; +import useWorkspaces from "@/hooks/useWorkspace"; type WorkspaceIdPageProps = { params: { workspaceId: string }; @@ -16,6 +22,7 @@ type WorkspaceIdPageProps = { const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => { const { FetchWorkspaceProjectsQuery } = useProject(); + const projectsResponse = FetchWorkspaceProjectsQuery(params?.workspaceId); const isFetching = projectsResponse.isFetching; const workspaceId = params?.workspaceId; @@ -26,13 +33,13 @@ const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => { <>
-
-

My Projects

+
+

My Projects

{isFetching && ( -
- - - +
+ + +
)} @@ -52,9 +59,9 @@ const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => { )) : // No Projects Message !isFetching && ( -
- -

+
+ +

Pretty empty around here.

Add a project and get scheduling!😉

diff --git a/app/(platform)/(dashboard)/workspace/[workspaceId]/settings/page.tsx b/app/(platform)/(dashboard)/workspace/[workspaceId]/settings/page.tsx index ef917d6..18a3185 100644 --- a/app/(platform)/(dashboard)/workspace/[workspaceId]/settings/page.tsx +++ b/app/(platform)/(dashboard)/workspace/[workspaceId]/settings/page.tsx @@ -61,15 +61,7 @@ const router = useRouter(); const handleDeleteWorkspace = async () => { try { await deleteWorkspaceMutation.mutateAsync({ workspaceId }); - // Redirect after successful deletion - if (isSuccessWorkspace && workspaces && workspaces.length > 0) { - router.push(`/workspace/${workspaces[0].id}`); - } else { - // Redirect to create-workspace route only if workspaces query has successfully fetched data - if (!workspaces) { - router.replace('/create-workspace'); // Use router.replace instead of router.push - } - } + } catch (error) { console.error("Error deleting workspace:", error); } diff --git a/app/not-found.tsx b/app/not-found.tsx index 669754e..3dea08f 100644 --- a/app/not-found.tsx +++ b/app/not-found.tsx @@ -1,26 +1,28 @@ -'use client'; +"use client"; -import { Button } from '@/components/ui/button'; -import Image from 'next/image'; -import { useRouter } from 'next/navigation'; +import { Button } from "@/components/ui/button"; +import Image from "next/image"; +import { useRouter } from "next/navigation"; const NotFound = () => { const router = useRouter(); return ( -
-
-
-
404
-

+

+
+
+
404
+

Sorry we couldn't find this page.

-

+

But dont worry, you can find plenty of other things on our homepage.

- +
-
- 404 +
+ 404
diff --git a/components/shared/Protected.tsx b/components/shared/Protected.tsx index 4ec8f4e..efc0ee5 100644 --- a/components/shared/Protected.tsx +++ b/components/shared/Protected.tsx @@ -1,14 +1,27 @@ -'use client'; -import useAuth from '@/hooks/useAuth'; -import { notFound, redirect } from 'next/navigation'; -import { useEffect } from 'react'; +"use client"; +import useAuth from "@/hooks/useAuth"; +import useWorkspaces from "@/hooks/useWorkspace"; +import { notFound, redirect, useRouter } from "next/navigation"; +import { useEffect } from "react"; const ProtectedRoute = ({ children }: { children: React.ReactNode }) => { const { user, token } = useAuth(); + const { getMyWorkspacesQuery } = useWorkspaces(); + const workspaces = getMyWorkspacesQuery?.data?.data; + const router = useRouter(); + useEffect(() => { + if ( + workspaces && + !getMyWorkspacesQuery.isFetching && + workspaces.length < 1 + ) { + router.push("/create-workspace"); + } + }, [workspaces]); useEffect(() => { if (!token) { - redirect('/login'); + redirect("/login"); } if (!user) { diff --git a/hooks/useProject.ts b/hooks/useProject.ts index 1b790ef..ea81267 100644 --- a/hooks/useProject.ts +++ b/hooks/useProject.ts @@ -1,20 +1,24 @@ -import axiosResponseMessage from '@/lib/axiosResponseMessage'; +import NotFound from "@/app/not-found"; +import axiosResponseMessage from "@/lib/axiosResponseMessage"; import ProjectService, { CreateProjectRequest, EditWorkspaceProjectRequest, -} from '@/services/projectService'; +} from "@/services/projectService"; +import { useWorkspaceStore } from "@/store/workspaceStore"; import { InvalidateQueryFilters, useMutation, useQuery, useQueryClient, -} from '@tanstack/react-query'; -import { AxiosError } from 'axios'; -import { toast } from 'sonner'; +} from "@tanstack/react-query"; +import { AxiosError } from "axios"; +import { toast } from "sonner"; const useProject = () => { const queryClient = useQueryClient(); + const activeWorkspace = useWorkspaceStore((state) => state.activeWorkspace); + const CreateProjectMutation = ( workspaceId: string, setIsopen?: (bool: boolean) => void @@ -40,7 +44,7 @@ const useProject = () => { setIsopen && setIsopen(false); const queryKey: InvalidateQueryFilters = { - queryKey: ['workspaceProjects', workspaceId], + queryKey: ["workspaceProjects", workspaceId], }; queryClient.invalidateQueries(queryKey); }, @@ -48,16 +52,12 @@ const useProject = () => { const FetchWorkspaceProjectsQuery = (workspaceId: string) => useQuery({ - queryKey: ['workspaceProjects', workspaceId], + queryKey: ["workspaceProjects", workspaceId], queryFn: async () => { - try { - const response = await ProjectService.fetchWorkspaceProjects( - workspaceId - ); - return response?.data; - } catch (error) { - console.log(error); - } + const response = await ProjectService.fetchWorkspaceProjects( + workspaceId + ); + return response?.data; }, }); @@ -66,7 +66,7 @@ const useProject = () => { projectId: string ) => useQuery({ - queryKey: ['workspaceProject', workspaceId, projectId], + queryKey: ["workspaceProject", workspaceId, projectId], queryFn: async () => { try { const response = await ProjectService.showAWorkspaceProject( @@ -104,7 +104,7 @@ const useProject = () => { setIsopen && setIsopen(false); const queryKey: InvalidateQueryFilters = { - queryKey: ['workspaceProjects', workspaceId], + queryKey: ["workspaceProjects", workspaceId], }; queryClient.invalidateQueries(queryKey); }, diff --git a/hooks/useWorkspace.ts b/hooks/useWorkspace.ts index 3eece42..eaa820a 100644 --- a/hooks/useWorkspace.ts +++ b/hooks/useWorkspace.ts @@ -1,15 +1,24 @@ -'use client'; +"use client"; -import axiosResponseMessage from '@/lib/axiosResponseMessage'; +import axiosResponseMessage from "@/lib/axiosResponseMessage"; import WorkspaceService, { - CreateWorkspaceRequest, UpdateWorkspaceRequest, -} from '@/services/workspaceService'; -import { InvalidateQueryFilters, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { AxiosError } from 'axios'; -import { toast } from 'sonner'; + CreateWorkspaceRequest, + UpdateWorkspaceRequest, +} from "@/services/workspaceService"; +import { + InvalidateQueryFilters, + useMutation, + useQuery, + useQueryClient, +} from "@tanstack/react-query"; +import { AxiosError } from "axios"; +import { useRouter } from "next/navigation"; +import { toast } from "sonner"; const useWorkspaces = () => { -const queryClient = useQueryClient(); + const queryClient = useQueryClient(); + + const router = useRouter(); const createWorkspaceMutation = useMutation({ mutationFn: async (data: CreateWorkspaceRequest) => { @@ -27,7 +36,7 @@ const queryClient = useQueryClient(); }); const getMyWorkspacesQuery = useQuery({ - queryKey: ['getMyWorkspace'], + queryKey: ["getMyWorkspace"], queryFn: async () => { try { const response = await WorkspaceService.getMyWorkspaces(); @@ -41,7 +50,7 @@ const queryClient = useQueryClient(); const GetShowAWorkspaceQuery = (workspaceId: string) => { return useQuery({ - queryKey: ['getShowAWorkspace', workspaceId], + queryKey: ["getShowAWorkspace", workspaceId], queryFn: async () => { try { const response = await WorkspaceService.showAWorkspace(workspaceId); @@ -54,9 +63,7 @@ const queryClient = useQueryClient(); }); }; - const DeleteWorkspaceMutation = ( - workspaceId: string - ) => + const DeleteWorkspaceMutation = (workspaceId: string) => useMutation({ mutationFn: async ({ workspaceId }: { workspaceId: string }) => { const response = await WorkspaceService.deleteWorkspace(workspaceId); @@ -66,37 +73,39 @@ const queryClient = useQueryClient(); const { status } = data; toast.success(status); const queryKey: InvalidateQueryFilters = { - queryKey: ['getMyWorkspace'], + queryKey: ["getMyWorkspace"], }; queryClient.invalidateQueries(queryKey); + + router.refresh(); }, onError: (error: AxiosError) => { toast.error(error.message); console.log(axiosResponseMessage(error)); }, }); - - const UpdateWorkspaceMutation = () => - useMutation({ - mutationFn: async ({ - workspaceId, - data - }: { - workspaceId: string; - data: UpdateWorkspaceRequest; - }) => { - const response = await WorkspaceService.updateWorkspace(workspaceId, data); - return response?.data; - }, - onError: (error: AxiosError) => { + useMutation({ + mutationFn: async ({ + workspaceId, + data, + }: { + workspaceId: string; + data: UpdateWorkspaceRequest; + }) => { + const response = await WorkspaceService.updateWorkspace( + workspaceId, + data + ); + return response?.data; + }, + onError: (error: AxiosError) => { toast.error(error.message); console.log(axiosResponseMessage(error)); }, }); - return { createWorkspaceMutation, getMyWorkspacesQuery,