📋 Manage Assignments
diff --git a/src/app/assignments/page.js b/src/app/pages/assignments/page.js
similarity index 66%
rename from src/app/assignments/page.js
rename to src/app/pages/assignments/page.js
index d95aebc..ea05c9a 100644
--- a/src/app/assignments/page.js
+++ b/src/app/pages/assignments/page.js
@@ -1,22 +1,22 @@
-// app/assignments/page.js
"use client";
import { useEffect, useState } from "react";
-import { databases } from "../lib/appwrite"; // Appwrite SDK
+import { databases, account } from "../../lib/appwrite";
import { ID } from "appwrite";
export default function AssignmentsPage() {
const [assignments, setAssignments] = useState([]);
const [submissionLinks, setSubmissionLinks] = useState({});
- const [userId, setUserId] = useState(""); // You might fetch this from your auth context/session
+ const [userId, setUserId] = useState(null);
+ const [loadingUser, setLoadingUser] = useState(true); // 🟡 Loading state
+ // Fetch assignments and user on mount
useEffect(() => {
- // Fetch assignments from Appwrite
const fetchAssignments = async () => {
try {
const response = await databases.listDocuments(
- "your_database_id",
- "assignments_collection_id"
+ "67e1452b00016444b37f",
+ "67f0f285001a1614b4e8"
);
setAssignments(response.documents);
} catch (error) {
@@ -24,28 +24,21 @@ export default function AssignmentsPage() {
}
};
+ const fetchUser = async () => {
+ try {
+ const user = await account.get();
+ setUserId(user.$id);
+ } catch (error) {
+ console.error("User not logged in:", error);
+ } finally {
+ setLoadingUser(false); // ✅ Done loading
+ }
+ };
+
fetchAssignments();
+ fetchUser();
}, []);
- const handleSubmit = async (assignmentId) => {
- try {
- await databases.createDocument(
- "67e1452b00016444b37f",
- "67f0f285001a1614b4e8",
- ID.unique(),
- {
- assignmentId,
- userId,
- submissionLink: submissionLinks[assignmentId],
- }
- );
-
- alert("Submitted!");
- } catch (error) {
- console.error("Submission failed:", error);
- }
- };
-
const handleInputChange = (assignmentId, value) => {
setSubmissionLinks({
...submissionLinks,
@@ -53,6 +46,40 @@ export default function AssignmentsPage() {
});
};
+ const handleSubmit = async (assignmentId) => {
+ if (!userId) {
+ alert("User not logged in. Cannot submit.");
+ return;
+ }
+
+ const submissionLink = submissionLinks[assignmentId];
+ if (!submissionLink) {
+ alert("Submission link is empty.");
+ return;
+ }
+
+ try {
+ await databases.createDocument(
+ "67e1452b00016444b37f",
+ "67f5ea0a000393d31806", // ✅ Replace with correct ID
+ ID.unique(),
+ {
+ assignmentId,
+ userId,
+ submissionLink,
+ }
+ );
+ alert("Submitted!");
+ } catch (error) {
+ console.error("Submission failed:", error);
+ alert("Submission failed: " + error.message);
+ }
+ };
+
+ if (loadingUser) {
+ return
Loading...
Assignments
@@ -81,6 +108,7 @@ export default function AssignmentsPage() {
diff --git a/src/app/pages/dashboard/page.js b/src/app/pages/dashboard/page.js
index fa1129b..9b7e9af 100644
--- a/src/app/pages/dashboard/page.js
+++ b/src/app/pages/dashboard/page.js
@@ -2,6 +2,8 @@
import { useEffect, useState } from 'react';
import { account } from"../../lib/appwrite";
import { useRouter } from 'next/navigation';
+import Sidebar from '@/app/components/Sidebar';
+import Navbar from '@/app/components/Navbar';
export default function DashboardPage() {
const router = useRouter();
@@ -22,9 +24,11 @@ export default function DashboardPage() {
if (!user) return
Checking auth...
;
return (
-
+
+
Welcome, {user.name || user.email} 👋
This is your dashboard.
-
+
+
);
}
diff --git a/src/app/user/page.js b/src/app/pages/user/page.js
similarity index 88%
rename from src/app/user/page.js
rename to src/app/pages/user/page.js
index f07bc80..9b74da8 100644
--- a/src/app/user/page.js
+++ b/src/app/pages/user/page.js
@@ -1,7 +1,7 @@
'use client';
import { useEffect, useState } from "react";
-import { getCurrentUserWithRole } from "../utils/auth";
+import { getCurrentUserWithRole } from "../../utils/auth";
export default function UserDashboard() {
const [user, setUser] = useState(null);
diff --git a/src/app/pages/user/profile/page.js b/src/app/pages/user/profile/page.js
new file mode 100644
index 0000000..c63840f
--- /dev/null
+++ b/src/app/pages/user/profile/page.js
@@ -0,0 +1,136 @@
+'use client';
+
+import { useEffect, useState } from "react";
+import { getCurrentUserWithRole } from "../../../utils/auth";
+import { databases, databaseId, membersCollectionId, Query } from "../../../lib/appwrite";
+import { useRouter } from "next/navigation";
+
+export default function UserProfilePage() {
+ const router = useRouter();
+ const [profile, setProfile] = useState(null);
+ const [form, setForm] = useState({ name: '', phone: '' });
+ const [docId, setDocId] = useState(null);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState(null);
+
+ useEffect(() => {
+ async function fetchProfile() {
+ try {
+ setLoading(true);
+ setError(null);
+
+ const user = await getCurrentUserWithRole();
+ console.log("User data:", user);
+
+ if (!user) {
+ throw new Error("Please login to access this page");
+ }
+
+ // Use the same DB ID as in your auth.js
+ const dbId = "67e1452b00016444b37f";
+ const collectionId = "67f0f1200006897dc192";
+
+ const res = await databases.listDocuments(
+ dbId,
+ collectionId,
+ [Query.equal('userId', user.$id)]
+ );
+
+ console.log("Profile data:", res);
+
+ if (res.documents.length === 0) {
+ throw new Error("No profile found for this user");
+ }
+
+ const data = res.documents[0];
+ setDocId(data.$id);
+ setProfile(data);
+ setForm({
+ name: data.name || user.name || '',
+ phone: data.phone || ''
+ });
+
+ } catch (err) {
+ console.error("Profile fetch error:", err);
+ setError(err.message);
+ if (err.message.includes("authenticated") || err.message.includes("login")) {
+ router.push('/login');
+ }
+ } finally {
+ setLoading(false);
+ }
+ }
+
+ fetchProfile();
+ }, [router]);
+
+ const handleChange = (e) => {
+ setForm({
+ ...form,
+ [e.target.name]: e.target.value
+ });
+ };
+
+ const handleSubmit = async (e) => {
+ e.preventDefault();
+ try {
+ if (!docId) throw new Error("No profile document found");
+
+ // Use the same DB ID as above
+ const dbId = "67e1452b00016444b37f";
+ const collectionId = "67f0f1200006897dc192";
+
+ await databases.updateDocument(
+ dbId,
+ collectionId,
+ docId,
+ form
+ );
+ alert("Profile updated successfully!");
+ } catch (err) {
+ console.error("Update error:", err);
+ alert(err.message || "Failed to update profile");
+ }
+ };
+
+ if (loading) return
Loading profile...
;
+ if (error) return
{error}
;
+
+ return (
+
+ );
+}
\ No newline at end of file
diff --git a/src/app/user/layout.js b/src/app/user/layout.js
deleted file mode 100644
index d912ff3..0000000
--- a/src/app/user/layout.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// app/user/layout.js
-import React from 'react';
-import Navbar from "../components/Navbar"; // Adjust path if needed
-import Sidebar from "../components/Sidebar"; // Adjust path if needed
-
-export default function UserLayout({ children }) {
- return (
-
- );
-}
diff --git a/src/app/user/profile/page.js b/src/app/user/profile/page.js
deleted file mode 100644
index bde2cf0..0000000
--- a/src/app/user/profile/page.js
+++ /dev/null
@@ -1,67 +0,0 @@
-'use client';
-
-import { useEffect, useState } from "react";
-import { getCurrentUser } from "../../utils/auth";
-import { databases } from "../../lib/appwrite";
-
-export default function UserProfilePage() {
- const [profile, setProfile] = useState(null);
- const [form, setForm] = useState(null);
- const [docId, setDocId] = useState(null);
- const [loading, setLoading] = useState(true);
-
- useEffect(() => {
- async function fetchProfile() {
- const user = await getCurrentUser();
- const res = await databases.listDocuments(
- process.env.NEXT_PUBLIC_DB_ID,
- process.env.NEXT_PUBLIC_MEMBERS_COLLECTION_ID,
- [`query.equal("userId", ["${user.$id}"])`]
- );
-
- const data = res.documents[0];
- setDocId(data.$id);
- setProfile(data);
- setForm({ name: data.name, phone: data.phone });
- setLoading(false);
- }
-
- fetchProfile();
- }, []);
-
- const handleChange = (e) => setForm({ ...form, [e.target.name]: e.target.value });
-
- const handleSubmit = async (e) => {
- e.preventDefault();
- await databases.updateDocument(
- process.env.NEXT_PUBLIC_DB_ID,
- process.env.NEXT_PUBLIC_MEMBERS_COLLECTION_ID,
- docId,
- form
- );
- alert("Profile updated!");
- };
-
- if (loading) return
Loading...
;
-
- return (
-
- );
-}
diff --git a/src/app/utils/auth.js b/src/app/utils/auth.js
index dd98d41..7aeb8f2 100644
--- a/src/app/utils/auth.js
+++ b/src/app/utils/auth.js
@@ -1,35 +1,39 @@
import { Client, Account, Databases, Query } from "appwrite";
const client = new Client()
- .setEndpoint("https://cloud.appwrite.io/v1") // Update with your Appwrite endpoint
+ .setEndpoint("https://cloud.appwrite.io/v1")
.setProject("67e1445400053dca1d9b");
const account = new Account(client);
const databases = new Databases(client);
export async function getCurrentUserWithRole() {
- try {
- const user = await account.get();
-
- const dbId = "67e1452b00016444b37f";
- const collectionId = "67f0f1200006897dc192";
-
- const response = await databases.listDocuments(dbId, collectionId, [
- Query.equal("userId", user.$id),
- ]);
-
- console.log("Query result:", response); // 🔍 Check this in console
-
- const userDoc = response.documents[0];
-
- return {
- name: user.name,
- email: user.email,
- role: userDoc?.role || "unknown",
- };
- } catch (error) {
- console.error("Error fetching user with role:", error);
- return null;
+ try {
+ const user = await account.get();
+
+ if (!user || !user.$id) {
+ throw new Error("User not authenticated");
}
+
+ const dbId = "67e1452b00016444b37f";
+ const collectionId = "67f0f1200006897dc192";
+
+ const response = await databases.listDocuments(dbId, collectionId, [
+ Query.equal("userId", user.$id),
+ ]);
+
+ const userDoc = response.documents[0];
+
+ return {
+ $id: user.$id, // Make sure to include this
+ name: user.name,
+ email: user.email,
+ role: userDoc?.role || "unknown",
+ // Include the document ID if needed
+ docId: userDoc?.$id
+ };
+ } catch (error) {
+ console.error("Error fetching user with role:", error);
+ throw error; // Throw instead of returning null
}
-
+}
\ No newline at end of file