add userid sending

This commit is contained in:
Joseph J Helfenbein
2025-01-25 06:32:26 -05:00
parent f5735d85e3
commit fe228f211f
2 changed files with 29 additions and 29 deletions

View File

@@ -1,31 +1,32 @@
"use client"; "use client"
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import axios from 'axios'; import axios from 'axios';
import { Button } from '../../../components/ui/button'; import { useUser } from '@clerk/clerk-react';
import { Input } from '../../../components/ui/input'; import { Button, Input, Label, Card, CardHeader, CardBody, CardFooter } from 'components/ui';
import { Label } from '../../../components/ui/label';
import { Card, CardHeader, CardContent, CardFooter } from '../../../components/ui/card';
const AccountPage = () => { const AccountPage = () => {
const [user, setUser] = useState(null); const { user } = useUser();
const [userData, setUserData] = useState(null);
const [patients, setPatients] = useState([]); const [patients, setPatients] = useState([]);
const [selectedPatient, setSelectedPatient] = useState(null); const [selectedPatient, setSelectedPatient] = useState(null);
const [medications, setMedications] = useState([]); const [medications, setMedications] = useState([]);
const [diagnoses, setDiagnoses] = useState([]); const [diagnoses, setDiagnoses] = useState([]);
useEffect(() => { useEffect(() => {
axios.get('/api/user').then(response => { if (user) {
setUser(response.data); axios.get(`/api/user?userId=${user.id}`).then(response => {
if (response.data.role === 'caregiver') { setUserData(response.data);
axios.get('/api/patients').then(res => setPatients(res.data)); if (response.data.role === 'caregiver') {
} axios.get('/api/patients').then(res => setPatients(res.data));
}); }
}, []); });
}
}, [user]);
const handleRoleChange = async () => { const handleRoleChange = async () => {
const newRole = user.role === 'patient' ? 'caregiver' : 'patient'; const newRole = userData.role === 'patient' ? 'caregiver' : 'patient';
await axios.put('/api/user', { role: newRole }); await axios.put(`/api/user?userId=${user.id}`, { role: newRole });
setUser({ ...user, role: newRole }); setUserData({ ...userData, role: newRole });
if (newRole === 'caregiver') { if (newRole === 'caregiver') {
axios.get('/api/patients').then(res => setPatients(res.data)); axios.get('/api/patients').then(res => setPatients(res.data));
} else { } else {
@@ -58,7 +59,7 @@ const AccountPage = () => {
alert('Patient data updated successfully'); alert('Patient data updated successfully');
}; };
if (!user) return <div>Loading...</div>; if (!userData) return <div>Loading...</div>;
return ( return (
<div className="container mx-auto p-4"> <div className="container mx-auto p-4">
@@ -66,24 +67,24 @@ const AccountPage = () => {
<CardHeader> <CardHeader>
<h1 className="text-2xl font-bold">Account Page</h1> <h1 className="text-2xl font-bold">Account Page</h1>
</CardHeader> </CardHeader>
<CardContent> <CardBody>
<div className="mb-4"> <div className="mb-4">
<Label>Name:</Label> <Label>Name:</Label>
<p>{user.name}</p> <p>{userData.name}</p>
</div> </div>
<div className="mb-4"> <div className="mb-4">
<Label>Email:</Label> <Label>Email:</Label>
<p>{user.email}</p> <p>{userData.email}</p>
</div> </div>
<div className="mb-4"> <div className="mb-4">
<Label>Role:</Label> <Label>Role:</Label>
<p>{user.role}</p> <p>{userData.role}</p>
</div> </div>
<Button onClick={handleRoleChange} className="mb-4"> <Button onClick={handleRoleChange} className="mb-4">
Change role to {user.role === 'patient' ? 'caregiver' : 'patient'} Change role to {userData.role === 'patient' ? 'caregiver' : 'patient'}
</Button> </Button>
{user.role === 'caregiver' && ( {userData.role === 'caregiver' && (
<div> <div>
<h2 className="text-xl font-bold mb-4">Patients</h2> <h2 className="text-xl font-bold mb-4">Patients</h2>
<ul className="mb-4"> <ul className="mb-4">
@@ -103,7 +104,7 @@ const AccountPage = () => {
<CardHeader> <CardHeader>
<h3 className="text-xl font-bold">Edit Patient: {selectedPatient.name}</h3> <h3 className="text-xl font-bold">Edit Patient: {selectedPatient.name}</h3>
</CardHeader> </CardHeader>
<CardContent> <CardBody>
<div className="mb-4"> <div className="mb-4">
<Label>Medications:</Label> <Label>Medications:</Label>
{medications.map((medication, index) => ( {medications.map((medication, index) => (
@@ -140,7 +141,7 @@ const AccountPage = () => {
onChange={handleDiagnosesChange} onChange={handleDiagnosesChange}
/> />
</div> </div>
</CardContent> </CardBody>
<CardFooter> <CardFooter>
<Button onClick={handleSave}>Save</Button> <Button onClick={handleSave}>Save</Button>
</CardFooter> </CardFooter>
@@ -148,7 +149,7 @@ const AccountPage = () => {
)} )}
</div> </div>
)} )}
</CardContent> </CardBody>
</Card> </Card>
</div> </div>
); );

View File

@@ -1,16 +1,15 @@
import { getAuth } from '@clerk/nextjs/server';
import User from '../../models/User'; import User from '../../models/User';
import { connectDB } from '../../lib/utils'; import { connectDB } from '../../lib/utils';
export default async (req, res) => { export default async (req, res) => {
await connectDB(); await connectDB();
const { userId } = getAuth(req); const { userId } = req.query;
if (!userId) { if (!userId) {
return res.status(401).json({ message: 'Unauthorized' }); return res.status(401).json({ message: 'Unauthorized' });
} }
const user = await User.findOne({ id: userId }); const user = await User.findOne({ clerkId: userId });
if (!user) { if (!user) {
return res.status(404).json({ message: 'User not found' }); return res.status(404).json({ message: 'User not found' });