Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/462 #464

Merged
merged 3 commits into from
Jul 13, 2024
Merged
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
25 changes: 0 additions & 25 deletions src/app/api/files/private/route.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/app/api/files/public/route.ts

This file was deleted.

46 changes: 46 additions & 0 deletions src/app/api/files/update/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { mongoDB } from "@/lib/MongoDB";
import { AuthMiddleware } from "@/Middleware/AuthMiddleware";
import FileModel from "@/models/file";
import { ApiUser } from "@/types/types";
import { NextResponse } from "next/server";

export async function PUT(
request: Request
) {

const result = await AuthMiddleware(request);

if (result instanceof NextResponse) {

try {
await mongoDB();

const {fileName, filePrivate, fileId, archive} = await request.json()
console.log(fileName, filePrivate, fileId, archive)

if(!fileName || filePrivate === undefined || archive === undefined || !fileId){
return NextResponse.json(`Access Denied!!`, {status:404});
}

const user: ApiUser = JSON.parse(request.headers.get("user") || "{}");

const file = await FileModel.findById({_id:fileId});

if(file.createdBy !== user._id && !file.writtenBy.includes(user._id)){
return NextResponse.json(`Access Denied!!`, {status:401});
}

await FileModel.updateOne({_id:fileId},{
fileName,
filePrivate,
archive
})

return NextResponse.json('',{status:200});
} catch (err) {
return NextResponse.json(`Err : ${err}`, {status:500});
}
} else {
return result;
}
}
62 changes: 42 additions & 20 deletions src/app/dashboard/_components/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import RenameFileModal from "@/components/shared/RenameFileModal";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import FileStatusModal from "@/components/shared/FileStatusModal";
import { RootState } from "@/config/store";
import createAxiosInstance from "@/config/AxiosProtectedRoute";
import { updateFileUrl } from "@/lib/API-URLs";

export interface FILE {
archive: boolean;
Expand Down Expand Up @@ -130,8 +133,8 @@ const FileRow = ({
file: FILE;
picture: string;
pathname: string;
onArchive: (e: any, id: string) => void;
onUnarchive: (e: any, id: string) => void;
onArchive: (e: any, file: FILE) => void;
onUnarchive: (e: any, file: FILE) => void;
onDelete: (e: any, id: string) => void;
router: ReturnType<typeof useRouter>;
index: number;
Expand Down Expand Up @@ -171,8 +174,8 @@ const FileRow = ({
</td>
<td>
<FileStatusModal
fileId={file._id}
email={user.email}
file={file}
user={user}
privateFIle={file.filePrivate}
successTitle={
!file.filePrivate
Expand All @@ -188,15 +191,15 @@ const FileRow = ({
/>
</td>
<td className="flex gap-2 whitespace-nowrap px-4 py-2 text-muted-foreground">
<RenameFileModal id={file._id} />
<RenameFileModal file={file} user={user} />
{pathname === "/dashboard" && (
<ActionDialog
isSubmitted={isSubmitted}
successTitle="File Archived Successfully!!"
buttonIcon={ArchiveIcon}
dialogTitle="Are you absolutely sure?"
dialogDescription="This will add your file to the archive section."
onAction={(e) => onArchive(e, file._id)}
onAction={(e) => onArchive(e, file)}
/>
)}
{pathname === "/dashboard/archive" && (
Expand All @@ -206,7 +209,7 @@ const FileRow = ({
buttonIcon={ArchiveRestore}
dialogTitle="Are you absolutely sure?"
dialogDescription="This will unarchive your file."
onAction={(e) => onUnarchive(e, file._id)}
onAction={(e) => onUnarchive(e, file)}
buttonVariant="destructive"
/>
)}
Expand Down Expand Up @@ -243,6 +246,8 @@ function FileList({
const safeFileList = Array.isArray(fileList) ? fileList : [];
const pathname = usePathname();

const axiosInstance = createAxiosInstance(user.accessToken)

const sortedFiles = [...safeFileList];
if (sortConfig !== null) {
sortedFiles.sort((a, b) => {
Expand All @@ -263,18 +268,35 @@ function FileList({
setIsSubmitted(true);
};

const archiveFile = useMutation(api.files.addToArchive);
const archiveFunc = async (e: any, id: string) => {

const archiveFunc = async (e: any, file:FILE) => {
e.stopPropagation();
await archiveFile({ _id: id as Id<"files"> });
setIsSubmitted(true);
try {
await axiosInstance.put(updateFileUrl,{
fileName:file.fileName,
filePrivate:file.filePrivate,
fileId:file._id,
archive:true
})
setIsSubmitted(true);
} catch (err) {
console.log(err);
}
};

const unArchiveFile = useMutation(api.files.removeFromArchive);
const unarchiveFunc = async (e: any, id: string) => {
const unarchiveFunc = async (e: any, file:FILE) => {
e.stopPropagation();
await unArchiveFile({ _id: id as Id<"files"> });
setIsSubmitted(true);
try {
await axiosInstance.put(updateFileUrl,{
fileName:file.fileName,
filePrivate:file.filePrivate,
fileId:file._id,
archive:false
})
setIsSubmitted(true);
} catch (err) {
console.log(err);
}
};

const requestSort = (key: keyof FILE) => {
Expand Down Expand Up @@ -388,15 +410,15 @@ function FileList({
<div className="flex justify-between items-center mb-2">
<span className="font-bold text-xl">{file.fileName}</span>
<div className="flex gap-2">
<RenameFileModal id={file._id} />
<RenameFileModal user={user} file={file} />
{pathname === "/dashboard" && (
<ActionDialog
isSubmitted={isSubmitted}
successTitle="File Archived Successfully!!"
buttonIcon={ArchiveIcon}
dialogTitle="Are you absolutely sure?"
dialogDescription="This will add your file to the archive section."
onAction={(e) => archiveFunc(e, file._id)}
onAction={(e) => archiveFunc(e, file)}
/>
)}
{pathname === "/dashboard/archive" && (
Expand All @@ -406,7 +428,7 @@ function FileList({
buttonIcon={ArchiveRestore}
dialogTitle="Are you absolutely sure?"
dialogDescription="This will unarchive your file."
onAction={(e) => unarchiveFunc(e, file._id)}
onAction={(e) => unarchiveFunc(e, file)}
buttonVariant="destructive"
/>
)}
Expand All @@ -433,8 +455,8 @@ function FileList({
</div>
</div>
<FileStatusModal
fileId={file._id}
email={user.email}
file={file}
user={user}
privateFIle={file.filePrivate}
successTitle={
!file.filePrivate
Expand Down
33 changes: 18 additions & 15 deletions src/components/shared/FileStatusModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,41 @@ import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
import { useSelector } from "react-redux";
import { RootState } from "@/config/store";
import { changeToPrivateUrl, changeToPublicUrl } from "@/lib/API-URLs";
import axiosInstance from "@/config/AxiosInstance";
import { changeToPublicUrl, updateFileUrl } from "@/lib/API-URLs";
import { FILE } from "@/app/dashboard/_components/FileList";
import createAxiosInstance from "@/config/AxiosProtectedRoute";
import { CheckCircle2 } from "lucide-react";

type Props = {
dialogTitle: string;
dialogDescription: string;
successTitle: string;
privateFIle: boolean;
fileId: string;
email: string;
file: FILE;
user: any;
};

export default function FileStatusModal({
dialogTitle,
dialogDescription,
successTitle,
privateFIle,
fileId,
email,
file,
user,
}: Props) {
const [isSubmitted, setIsSubmitted] = useState(false);
const [isError, setError] = useState(false);
const [errorMsg, setErrorMsg] = useState("");
const teamId = useSelector((state: RootState) => state.team.teamId);
const axiosInstance = createAxiosInstance(user.accessToken)

const FileHandler = async () => {
if (!privateFIle) {
try {
const res = await axiosInstance.put(changeToPrivateUrl, {
teamId,
email,
fileId,
const res = await axiosInstance.put(updateFileUrl, {
fileName:file.fileName,
filePrivate:true,
fileId:file._id,
archive: (file.archive === undefined) ? false : file.archive
});
if (res.status === 200) {
setIsSubmitted(true);
Expand All @@ -59,10 +61,11 @@ export default function FileStatusModal({
}
} else {
try {
const res = await axiosInstance.put(changeToPublicUrl, {
teamId,
email,
fileId,
const res = await axiosInstance.put(updateFileUrl, {
fileName:file.fileName,
filePrivate:false,
fileId:file._id,
archive: (file.archive === undefined) ? false : file.archive
});
if (res.status === 200) {
setIsSubmitted(true);
Expand Down
28 changes: 19 additions & 9 deletions src/components/shared/RenameFileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { PencilIcon } from "lucide-react";
import { api } from "../../../convex/_generated/api";
import { Id } from "../../../convex/_generated/dataModel";
import { SetStateAction } from "react";
import { FILE } from "@/app/dashboard/_components/FileList";
import { updateFileUrl } from "@/lib/API-URLs";
import createAxiosInstance from "@/config/AxiosProtectedRoute";

const FormSchema = z.object({
newName: z.string().min(1, {
Expand All @@ -26,12 +29,14 @@ const FormSchema = z.object({
});

type Props = {
id:string;
file:FILE;
setIsSubmitted: React.Dispatch<SetStateAction<boolean>>;
user:any;
}

export function RenameFileForm({id,setIsSubmitted}:Props) {
const convex = useConvex();
export function RenameFileForm({file,setIsSubmitted,user}:Props) {

const axiosInstance = createAxiosInstance(user.accessToken)

const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
Expand All @@ -41,12 +46,17 @@ export function RenameFileForm({id,setIsSubmitted}:Props) {
});

async function onSubmit(data: z.infer<typeof FormSchema>) {
const result = await convex.mutation(api.files.renameFile,{
_id:id as Id<"files">,
newName:data.newName
})

setIsSubmitted(true);
try {
await axiosInstance.put(updateFileUrl,{
fileName:data.newName,
filePrivate:file.filePrivate,
fileId:file._id,
archive: (file.archive === undefined) ? false : file.archive
})
setIsSubmitted(true);
} catch (err) {
console.log(err);
}
}

return (
Expand Down
10 changes: 5 additions & 5 deletions src/components/shared/RenameFileModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import {
import { RenameFileForm } from "./RenameFileForm";
import { Button } from "../ui/button";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { FILE } from "@/app/dashboard/_components/FileList";

type Props = {
id: string;
file: FILE;
user:any;
};

export default function RenameFileModal({ id }: Props) {
const router = useRouter();
export default function RenameFileModal({ file,user }: Props) {
const [isSubmitted, setIsSubmitted] = useState(false);
return (
<Dialog>
Expand All @@ -35,7 +35,7 @@ export default function RenameFileModal({ id }: Props) {
<h1>Rename File</h1>
</DialogTitle>
<DialogDescription>
<RenameFileForm id={id} setIsSubmitted={setIsSubmitted} />
<RenameFileForm file={file} setIsSubmitted={setIsSubmitted} user={user} />
</DialogDescription>
</DialogHeader>
)}
Expand Down
Loading
Loading