Skip to content

Commit

Permalink
Merge branch 'main' into pdffix
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadeeproy3902 authored Jun 26, 2024
2 parents 9de07e6 + 56dfd90 commit 428f811
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
24 changes: 24 additions & 0 deletions convex/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,27 @@ export const changeToPublic = mutation({
return res;
},
});

export const updateWrite = mutation({
args: {
_id: v.id("files"),
writtenBy: v.array(v.string())
},
handler: async (ctx, args) => {
const { _id,writtenBy } = args;
const res = await ctx.db.patch(_id, { writtenBy, write:true, read:true });
return res;
},
});

export const updateRead = mutation({
args: {
_id: v.id("files"),
writtenBy: v.array(v.string())
},
handler: async (ctx, args) => {
const { _id,writtenBy } = args;
const res = await ctx.db.patch(_id, { writtenBy, write:false, read:true });
return res;
},
});
5 changes: 3 additions & 2 deletions convex/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { v } from "convex/values";

export default defineSchema({
teams: defineTable({

teamName: v.string(),
createdBy: v.string(),
teamMembers: v.array(v.string()),
Expand All @@ -21,6 +20,8 @@ export default defineSchema({
document: v.string(),
whiteboard: v.string(),
private: v.string(),
accessedBy: v.array(v.string())
writtenBy: v.array(v.string()),
write:v.boolean(),
read:v.boolean()
}),
});
33 changes: 33 additions & 0 deletions src/app/api/files/read/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { api } from "../../../../../convex/_generated/api";
import { ConvexHttpClient } from "convex/browser";

export const PUT = async (req: Request) => {
try {
const { teamId, email, memberEmail, writtenBy, fileId } = await req.json();

if (!teamId || !memberEmail || !email || !fileId)
return new Response("Parameters missing!!", { status: 401 });

const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);

const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId });

if (!teamInfo.teamMembers.includes(memberEmail)) {
return new Response("User is not member of the team", { status: 400 });
}

if (teamInfo.createdBy !== email) {
return new Response("Only owner can make changes!!", { status: 400 });
}

const updatedWrittenBy = Array.isArray(writtenBy)
? writtenBy.filter(writer => writer !== memberEmail)
: [];

await client.mutation(api.files.updateRead, { _id: fileId, writtenBy:updatedWrittenBy });

return new Response("Changed to Public!!", { status: 200 });
} catch (err) {
console.log(err);
}
};
29 changes: 29 additions & 0 deletions src/app/api/files/write/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { api } from "../../../../../convex/_generated/api";
import { ConvexHttpClient } from "convex/browser";

export const PUT = async (req: Request) => {
try {
const { teamId, email, memberEmail, writtenBy, fileId } = await req.json();

if (!teamId || !memberEmail || !email || !fileId)
return new Response("Parameters missing!!", { status: 401 });

const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);

const teamInfo = await client.query(api.teams.getTeamById, { _id: teamId });

if (!teamInfo.teamMembers.includes(memberEmail)) {
return new Response("User is not member of the team", { status: 400 });
}

if (teamInfo.createdBy !== email) {
return new Response("Only owner can make changes!!", { status: 400 });
}

await client.mutation(api.files.updateWrite, { _id: fileId, writtenBy: [...writtenBy,memberEmail] });

return new Response("Changed to Public!!", { status: 200 });
} catch (err) {
console.log(err);
}
};
4 changes: 2 additions & 2 deletions src/app/api/teams/get/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { NextResponse } from "next/server";
import { api } from "../../../../../convex/_generated/api";
import { ConvexHttpClient } from "convex/browser";

export const GET = async () => {
try {
console.log(process.env.NEXT_PUBLIC_CONVEX_URL!)

const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);

const teamInfo = await client.query(api.teams.getAllTeam);

return Response.json(teamInfo);
return NextResponse.json(teamInfo);
} catch (err) {
console.log(err)
}
Expand Down
83 changes: 82 additions & 1 deletion src/components/shared/FileStatusModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
} from "../ui/alert-dialog";
import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
import { useSelector } from "react-redux";
import { RootState } from "@/app/store";
import { changeToPrivateUrl, changeToPublicUrl } from "@/lib/API-URLs";
import axiosInstance from "@/config/AxiosInstance";
import { CheckCircle2 } from "lucide-react";

type Props = {
dialogTitle: string;
Expand All @@ -31,7 +36,45 @@ export default function FileStatusModal({
email,
}: Props) {
const [isSubmitted, setIsSubmitted] = useState(false);
const [isError, setError] = useState(false);
const [errorMsg, setErrorMsg] = useState("");
const teamId = useSelector((state: RootState) => state.team.teamId);

const FileHandler = async () => {
if (!privateFIle) {
try {
const res = await axiosInstance.put(changeToPrivateUrl, {
teamId,
email,
fileId,
});
if (res.status === 200) {
setIsSubmitted(true);
} else {
setError(true);
setErrorMsg("Error occured!! Please try again later!!");
}
} catch (err) {
console.log(err);
}
} else {
try {
const res = await axiosInstance.put(changeToPublicUrl, {
teamId,
email,
fileId,
});
if (res.status === 200) {
setIsSubmitted(true);
} else {
setError(true);
setErrorMsg("Error occured!! Please try again later!!");
}
} catch (err) {
console.log(err);
}
}
};
return (
<AlertDialog>
<AlertDialogTrigger>
Expand All @@ -50,7 +93,45 @@ export default function FileStatusModal({
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button onClick={() => {}}>Continue</Button>
<Button onClick={() => {FileHandler()}}>Continue</Button>
</AlertDialogFooter>
</>
)}

{isSubmitted && (
<>
<AlertDialogHeader>
<AlertDialogTitle className="flex gap-2">
<p>{successTitle}</p> <CheckCircle2 className="w-6 h-6" />
</AlertDialogTitle>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogAction
onClick={() => {
window.location.reload();
}}
>
Continue
</AlertDialogAction>
</AlertDialogFooter>
</>
)}

{isError && (
<>
<AlertDialogHeader>
<AlertDialogTitle className="flex gap-2">
<p>{errorMsg}</p> <CheckCircle2 className="w-6 h-6" />
</AlertDialogTitle>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogAction
onClick={() => {
window.location.reload();
}}
>
Continue
</AlertDialogAction>
</AlertDialogFooter>
</>
)}
Expand Down

0 comments on commit 428f811

Please sign in to comment.