diff --git a/src/app/(web)/account/page.jsx b/src/app/(web)/account/page.jsx index 4dd586e..7475de3 100644 --- a/src/app/(web)/account/page.jsx +++ b/src/app/(web)/account/page.jsx @@ -1,162 +1,166 @@ +"use client" import { useState, useEffect } from 'react'; import axios from 'axios'; -import { useUser } from '@clerk/clerk-react'; -import { Button, Input, Label, Card, CardHeader, CardContent, CardFooter } from 'components/ui'; +import { useUser } from '@clerk/nextjs'; +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'; const AccountPage = () => { - 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(() => { - 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 = 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 { - setPatients([]); - setSelectedPatient(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(() => { + 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 = 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 { + setPatients([]); + setSelectedPatient(null); + } + }; + + const handlePatientSelect = (patient) => { + setSelectedPatient(patient); + setMedications(patient.medications); + setDiagnoses(patient.medicalConditions); + }; + + const handleMedicationsChange = (index, field, value) => { + const updatedMedications = [...medications]; + updatedMedications[index][field] = value; + setMedications(updatedMedications); + }; + + const handleDiagnosesChange = (e) => { + setDiagnoses(e.target.value.split(',')); + }; + + const handleAddMedication = () => { + setMedications([...medications, { name: '', dosage: '', frequency: '' }]); + }; + + const handleSave = async () => { + await axios.put(`/api/patients/${selectedPatient.email}`, { + medications, + medicalConditions: diagnoses, + }); + alert('Patient data updated successfully'); + }; + + if (!userData) return
{userData.name}
+{userData.email}
+{userData.role}
+{userData.name}
-{userData.email}
-{userData.role}
-