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

Account Page

-
- -
- -

{userData.name}

-
-
- -

{userData.email}

-
-
- -

{userData.role}

-
- + const handleSave = async () => { + await axios.put(`/api/patients/${selectedPatient.email}`, { + medications, + medicalConditions: diagnoses, + }); + alert('Patient data updated successfully'); + }; - {userData.role === 'caregiver' && ( -
-

Patients

-
    - {patients.map(patient => ( -
  • handlePatientSelect(patient)} - className="cursor-pointer hover:bg-gray-200 p-2" - > - {patient.name} -
  • - ))} -
+ if (!userData) return
Loading...
; - {selectedPatient && ( - - -

Edit Patient: {selectedPatient.name}

-
- -
- - {medications.map((medication, index) => ( -
- handleMedicationsChange(index, 'name', e.target.value)} - className="mb-2" - /> - handleMedicationsChange(index, 'dosage', e.target.value)} - className="mb-2" - /> - handleMedicationsChange(index, 'frequency', e.target.value)} - className="mb-2" - /> -
- ))} -
-
- - -
-
- - - -
- )} -
- )} -
-
-
- ); + return ( +
+ + +

Account Page

+
+ +
+ +

{userData.name}

+
+
+ +

{userData.email}

+
+
+ +

{userData.role}

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

Patients

+
    + {patients.map(patient => ( +
  • handlePatientSelect(patient)} + className="cursor-pointer hover:bg-gray-200 p-2" + > + {patient.name} +
  • + ))} +
+ + {selectedPatient && ( + + +

Edit Patient: {selectedPatient.name}

+
+ +
+ + {medications.map((medication, index) => ( +
+ handleMedicationsChange(index, 'name', e.target.value)} + className="mb-2" + /> + handleMedicationsChange(index, 'dosage', e.target.value)} + className="mb-2" + /> + handleMedicationsChange(index, 'frequency', e.target.value)} + className="mb-2" + /> +
+ ))} + +
+
+ + +
+
+ + + +
+ )} +
+ )} +
+
+
+ ); }; export default AccountPage; \ No newline at end of file