Skip to content

Commit

Permalink
feat: create file and get file
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-80 committed Jul 13, 2024
1 parent c3ca3f9 commit e7ee318
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 109 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slider": "^1.1.2",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-switch": "^1.1.0",
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "^1.0.7",
Expand Down
9 changes: 7 additions & 2 deletions src/app/api/files/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export const POST = async (req: Request) => {
if (result instanceof NextResponse) {

try {
const { fileName, filePrivate } = await req.json();
const { fileName, filePrivate, teamId } = await req.json();

console.log(fileName, filePrivate, teamId)

if(!fileName || !teamId) return NextResponse.json({ status: 401 });

await mongoDB();

Expand All @@ -22,7 +26,8 @@ export const POST = async (req: Request) => {
filePrivate,
createdBy:user._id,
readBy:[user._id],
writtenBy:[user._id]
writtenBy:[user._id],
teamId:teamId
});

return NextResponse.json({ status: 200 });
Expand Down
32 changes: 32 additions & 0 deletions src/app/api/files/get/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { mongoDB } from "@/lib/MongoDB";
import { AuthMiddleware } from "@/Middleware/AuthMiddleware";
import FileModel from "@/models/file";
import { NextResponse } from "next/server";

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {

const result = await AuthMiddleware(request);

if (result instanceof NextResponse) {

try {

const { id } = params;

if (!id) return new Response("Parameters missing!!", { status: 401 });

await mongoDB();

const files = await FileModel.find({teamId:id}).populate("createdBy")

return NextResponse.json(files,{ status: 200 });
} catch (err) {
return NextResponse.json(`Err : ${err}`, {status:500});
}
} else {
return result;
}
}
31 changes: 31 additions & 0 deletions src/app/api/files/getFileById/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mongoDB } from "@/lib/MongoDB";
import { AuthMiddleware } from "@/Middleware/AuthMiddleware";
import FileModel from "@/models/file";
import { NextResponse } from "next/server";

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {

const result = await AuthMiddleware(request);

if (result instanceof NextResponse) {

try {
const { id } = params;

if (!id) return new Response("Parameters missing!!", { status: 401 });

await mongoDB();

const files = await FileModel.findById({_id:id}).populate("createdBy")

return NextResponse.json(files,{ status: 200 });
} catch (err) {
return NextResponse.json(`Err : ${err}`, {status:500});
}
} else {
return result;
}
}
96 changes: 34 additions & 62 deletions src/app/dashboard/_components/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ import FileStatusModal from "@/components/shared/FileStatusModal";

export interface FILE {
archive: boolean;
createdBy: string;
createdBy: {
email: string;
firstName: string;
lastName: string;
};
document: string;
fileName: string;
teamId: string;
whiteboard: string;
_id: string;
_creationTime: number;
private: boolean;
filePrivate: boolean;
readBy:any[];
writtenBy:any[];
}

const ActionDialog = ({
Expand Down Expand Up @@ -119,7 +125,6 @@ const FileRow = ({
router,
index,
isSubmitted,
authorData,
user,
}: {
file: FILE;
Expand All @@ -131,7 +136,6 @@ const FileRow = ({
router: ReturnType<typeof useRouter>;
index: number;
isSubmitted: boolean;
authorData: any[];
user: any;
}) => (
<tr key={file._id} className="odd:bg-muted/50 cursor-pointer">
Expand All @@ -157,31 +161,27 @@ const FileRow = ({
className="whitespace-nowrap px-4 py-2 text-muted-foreground"
onClick={() => router.push("/workspace/" + file._id)}
>
{authorData.map(
(author, index) =>
author.email === file.createdBy && (
<Avatar key={index} className="w-[40px] h-[40px]">
<AvatarImage src={""} />
<AvatarFallback className=" text-xs">
{author.name.charAt(0)}
</AvatarFallback>
</Avatar>
)
)}
<Avatar key={index} className="w-[40px] h-[40px]">
<AvatarImage src={""} />
<AvatarFallback className=" text-xs">
{file.createdBy.firstName.charAt(0)}
{file.createdBy.lastName.charAt(0)}
</AvatarFallback>
</Avatar>
</td>
<td>
<FileStatusModal
fileId={file._id}
email={user.email}
privateFIle={file.private}
privateFIle={file.filePrivate}
successTitle={
!file.private
!file.filePrivate
? "File accessible to members only"
: "File accessible to everyone"
}
dialogTitle={!file.private ? "Private File" : "Public File"}
dialogTitle={!file.filePrivate ? "Private File" : "Public File"}
dialogDescription={
!file.private
!file.filePrivate
? "Make file accessible to members only"
: "Make file accessible to everyone"
}
Expand Down Expand Up @@ -240,34 +240,9 @@ function FileList({
} | null>(null);
const [isSmallScreen, setIsSmallScreen] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const [authorData, setAuthorData] = useState<any[]>([]);
const safeFileList = Array.isArray(fileList) ? fileList : [];
const pathname = usePathname();

console.log(fileList)

useEffect(() => {
const getData = async () => {
let listOfCreators: string[] = [];
authorData.forEach((user: any) => {
listOfCreators.push(user.email);
});

fileList?.forEach(async (file) => {
if (!listOfCreators.includes(file.createdBy)) {
listOfCreators.push(file.createdBy);
const result = await convex.query(api.user.getUser, {
email: file.createdBy,
});
setAuthorData([...authorData, result[0]]);
}
});
};
if (fileList) {
getData();
}
}, [fileList]);

const sortedFiles = [...safeFileList];
if (sortConfig !== null) {
sortedFiles.sort((a, b) => {
Expand Down Expand Up @@ -383,7 +358,6 @@ function FileList({
onDelete={deleteFunc}
router={router}
index={index}
authorData={authorData}
/>
)
)}
Expand Down Expand Up @@ -459,23 +433,21 @@ function FileList({
</div>
</div>
<FileStatusModal
fileId={file._id}
email={user.email}
privateFIle={file.private}
successTitle={
!file.private
? "File accessible to members only"
: "File accessible to everyone"
}
dialogTitle={
!file.private ? "Private File" : "Public File"
}
dialogDescription={
!file.private
? "Make file accessible to members only"
: "Make file accessible to everyone"
}
/>
fileId={file._id}
email={user.email}
privateFIle={file.filePrivate}
successTitle={
!file.filePrivate
? "File accessible to members only"
: "File accessible to everyone"
}
dialogTitle={!file.filePrivate ? "Private File" : "Public File"}
dialogDescription={
!file.filePrivate
? "Make file accessible to members only"
: "Make file accessible to everyone"
}
/>
</div>

<div className="text-muted-foreground flex justify-end">
Expand Down
35 changes: 8 additions & 27 deletions src/app/dashboard/_components/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { setClose, setOpen } from "@/app/Redux/Menu/menuSlice";
import { useDispatch, useSelector } from "react-redux";
import { useTheme } from "next-themes";
import { RootState } from "@/config/store";
import createAxiosInstance from "@/config/AxiosProtectedRoute";
import { getFileUrl } from "@/lib/API-URLs";
// import { RootState } from "@/app/store";

function SideNav() {
Expand All @@ -22,8 +24,9 @@ function SideNav() {
const dispatch = useDispatch();
const { theme } = useTheme();
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
const email = useSelector((state:RootState)=>state.auth.user.email)
const user = useSelector((state:RootState)=>state.auth.user)
const dispatch_nav = useDispatch();
const axiosInstance = createAxiosInstance(user.accessToken)

useEffect(() => {
const handleResize = () => {
Expand All @@ -45,33 +48,11 @@ function SideNav() {
useEffect(() => {
activeTeam && getFiles();
}, [activeTeam]);
const onFileCreate = (fileName: string) => {
createFile({
fileName: fileName,
teamId: activeTeam?._id,
createdBy: email,
archive: false,
document: "",
whiteboard: "",
}).then(
(resp) => {
if (resp) {
getFiles();
toast.success("File created successfully!");
}
},
(e) => {
toast.error("Error while creating file");
}
);
};

const getFiles = async () => {
const result = await convex.query(api.files.getFiles, {
teamId: activeTeam?._id,
});
setFileList_(result);
setTotalFiles(result?.length);
const result = await axiosInstance.get(`${getFileUrl}/${activeTeam._id}`);
setFileList_(result.data);
setTotalFiles(result.data?.length);
};

return (
Expand Down Expand Up @@ -109,8 +90,8 @@ function SideNav() {

<div>
<SideNavBottomSection
getFiles={getFiles}
totalFiles={totalFiles}
onFileCreate={onFileCreate}
activeTeam={activeTeam}
/>
</div>
Expand Down
Loading

0 comments on commit e7ee318

Please sign in to comment.