Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions app/(platform)/(dashboard)/workspace/[workspaceId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
'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 };
};

const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => {
const { FetchWorkspaceProjectsQuery } = useProject();

const projectsResponse = FetchWorkspaceProjectsQuery(params?.workspaceId);
const isFetching = projectsResponse.isFetching;
const workspaceId = params?.workspaceId;
Expand All @@ -26,13 +33,13 @@ const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => {
<>
<Header workspaceId={workspaceId} />

<div className='flex flex-col gap-5'>
<h1 className='font-medium text-muted-foreground'>My Projects</h1>
<div className="flex flex-col gap-5">
<h1 className="font-medium text-muted-foreground">My Projects</h1>
{isFetching && (
<div className='space-y-3'>
<Skeleton className=' h-20 w-full bg-neutral-800/10' />
<Skeleton className=' h-20 w-full bg-neutral-800/10' />
<Skeleton className=' h-20 w-full bg-neutral-800/10' />
<div className="space-y-3">
<Skeleton className=" h-20 w-full bg-neutral-800/10" />
<Skeleton className=" h-20 w-full bg-neutral-800/10" />
<Skeleton className=" h-20 w-full bg-neutral-800/10" />
</div>
)}

Expand All @@ -52,9 +59,9 @@ const WorkspaceIdPage = ({ params }: WorkspaceIdPageProps) => {
))
: // No Projects Message
!isFetching && (
<div className=' mt-16 flex flex-col items-center gap-2'>
<Ghost size={30} className=' w-8 h-8 text-zinc-800' />
<h3 className=' font-semibold text-xl'>
<div className=" mt-16 flex flex-col items-center gap-2">
<Ghost size={30} className=" w-8 h-8 text-zinc-800" />
<h3 className=" font-semibold text-xl">
Pretty empty around here.
</h3>
<p>Add a project and get scheduling!😉</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 15 additions & 13 deletions app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className='h-screen w-screen bg-gray-100 flex items-center'>
<div className='container flex flex-col md:flex-row items-center justify-center px-5 text-gray-700'>
<div className='max-w-md'>
<div className='text-5xl font-dark font-bold'>404</div>
<p className='text-2xl md:text-3xl font-light leading-normal'>
<div className="h-screen w-screen bg-gray-100 flex items-center">
<div className="container flex flex-col md:flex-row items-center justify-center px-5 text-gray-700">
<div className="max-w-md">
<div className="text-5xl font-dark font-bold">404</div>
<p className="text-2xl md:text-3xl font-light leading-normal">
Sorry we couldn&apos;t find this page.
</p>
<p className='mb-8'>
<p className="mb-8">
But dont worry, you can find plenty of other things on our homepage.
</p>
<Button onClick={() => router.back()}>Go back</Button>
<Button onClick={() => router.push("/create-workspace")}>
Go home
</Button>
</div>
<div className='max-w-lg'>
<Image src='/error-404.svg' alt='404' width={500} height={500} />
<div className="max-w-lg">
<Image src="/error-404.svg" alt="404" width={500} height={500} />
</div>
</div>
</div>
Expand Down
23 changes: 18 additions & 5 deletions components/shared/Protected.tsx
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
34 changes: 17 additions & 17 deletions hooks/useProject.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -40,24 +44,20 @@ const useProject = () => {
setIsopen && setIsopen(false);

const queryKey: InvalidateQueryFilters = {
queryKey: ['workspaceProjects', workspaceId],
queryKey: ["workspaceProjects", workspaceId],
};
queryClient.invalidateQueries(queryKey);
},
});

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;
},
});

Expand All @@ -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(
Expand Down Expand Up @@ -104,7 +104,7 @@ const useProject = () => {
setIsopen && setIsopen(false);

const queryKey: InvalidateQueryFilters = {
queryKey: ['workspaceProjects', workspaceId],
queryKey: ["workspaceProjects", workspaceId],
};
queryClient.invalidateQueries(queryKey);
},
Expand Down
67 changes: 38 additions & 29 deletions hooks/useWorkspace.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -27,7 +36,7 @@ const queryClient = useQueryClient();
});

const getMyWorkspacesQuery = useQuery({
queryKey: ['getMyWorkspace'],
queryKey: ["getMyWorkspace"],
queryFn: async () => {
try {
const response = await WorkspaceService.getMyWorkspaces();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand Down