From fe228f211f451ef0b1ed42b99eee93956638f8c5 Mon Sep 17 00:00:00 2001 From: Joseph J Helfenbein Date: Sat, 25 Jan 2025 06:32:26 -0500 Subject: [PATCH] add userid sending --- src/app/(web)/account/page.jsx | 53 +++++++++++++++++----------------- src/app/api/user.js | 5 ++-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/app/(web)/account/page.jsx b/src/app/(web)/account/page.jsx index 7a6711f..2eff506 100644 --- a/src/app/(web)/account/page.jsx +++ b/src/app/(web)/account/page.jsx @@ -1,31 +1,32 @@ -"use client"; +"use client" import { useState, useEffect } from 'react'; import axios from 'axios'; -import { Button } from '../../../components/ui/button'; -import { Input } from '../../../components/ui/input'; -import { Label } from '../../../components/ui/label'; -import { Card, CardHeader, CardContent, CardFooter } from '../../../components/ui/card'; +import { useUser } from '@clerk/clerk-react'; +import { Button, Input, Label, Card, CardHeader, CardBody, CardFooter } from 'components/ui'; const AccountPage = () => { - const [user, setUser] = useState(null); + const { user } = useUser(); + const [userData, setUserData] = useState(null); const [patients, setPatients] = useState([]); const [selectedPatient, setSelectedPatient] = useState(null); const [medications, setMedications] = useState([]); const [diagnoses, setDiagnoses] = useState([]); useEffect(() => { - axios.get('/api/user').then(response => { - setUser(response.data); - if (response.data.role === 'caregiver') { - axios.get('/api/patients').then(res => setPatients(res.data)); - } - }); - }, []); + if (user) { + axios.get(`/api/user?userId=${user.id}`).then(response => { + setUserData(response.data); + if (response.data.role === 'caregiver') { + axios.get('/api/patients').then(res => setPatients(res.data)); + } + }); + } + }, [user]); const handleRoleChange = async () => { - const newRole = user.role === 'patient' ? 'caregiver' : 'patient'; - await axios.put('/api/user', { role: newRole }); - setUser({ ...user, role: newRole }); + const newRole = userData.role === 'patient' ? 'caregiver' : 'patient'; + await axios.put(`/api/user?userId=${user.id}`, { role: newRole }); + setUserData({ ...userData, role: newRole }); if (newRole === 'caregiver') { axios.get('/api/patients').then(res => setPatients(res.data)); } else { @@ -58,7 +59,7 @@ const AccountPage = () => { alert('Patient data updated successfully'); }; - if (!user) return
Loading...
; + if (!userData) return
Loading...
; return (
@@ -66,24 +67,24 @@ const AccountPage = () => {

Account Page

- +
-

{user.name}

+

{userData.name}

-

{user.email}

+

{userData.email}

-

{user.role}

+

{userData.role}

- {user.role === 'caregiver' && ( + {userData.role === 'caregiver' && (

Patients

    @@ -103,7 +104,7 @@ const AccountPage = () => {

    Edit Patient: {selectedPatient.name}

    - +
    {medications.map((medication, index) => ( @@ -140,7 +141,7 @@ const AccountPage = () => { onChange={handleDiagnosesChange} />
    -
    + @@ -148,7 +149,7 @@ const AccountPage = () => { )}
)} -
+
); diff --git a/src/app/api/user.js b/src/app/api/user.js index 03f6bb1..025c4d0 100644 --- a/src/app/api/user.js +++ b/src/app/api/user.js @@ -1,16 +1,15 @@ -import { getAuth } from '@clerk/nextjs/server'; import User from '../../models/User'; import { connectDB } from '../../lib/utils'; export default async (req, res) => { await connectDB(); - const { userId } = getAuth(req); + const { userId } = req.query; if (!userId) { return res.status(401).json({ message: 'Unauthorized' }); } - const user = await User.findOne({ id: userId }); + const user = await User.findOne({ clerkId: userId }); if (!user) { return res.status(404).json({ message: 'User not found' });