-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCustomerAccountDetails.tsx
128 lines (117 loc) · 4.99 KB
/
CustomerAccountDetails.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useEffect, useState } from 'react';
import { databases } from '../lib/appwrite.config';
import * as sdk from 'node-appwrite';
import ChangePasswordForm from './UpdatePassword';
import { ReviewCardProps } from '../types/appwrite.type';
import UpdateProfileForm from './UpdateProfileForm';
import ReviewCardConsumerSelf from './ReviewCardConsumerSelf';
import { fetchReviewsForConsumer } from './DataReviewConsumerSelfView';
const CustomerAccountDetails: React.FC = () => {
const [profile, setProfile] = useState<any>(null);
const [message, setMessage] = useState('');
const [expandedSection, setExpandedSection] = useState<string | null>(null);
const [activeSetting, setActiveSetting] = useState<string | null>(null);
const [reviews, setReviews] = useState<ReviewCardProps[]>([]);
useEffect(() => {
const fetchProfile = async () => {
try {
const session = JSON.parse(localStorage.getItem('appwriteSession') || '{}');
if (!session || !session.userId) {
setMessage('No active session found. Please log in.');
return;
}
const response = await databases.listDocuments(
process.env.DATABASE_ID!,
process.env.CONSUMER_COLLECTION_ID!,
[sdk.Query.equal('userId', session.userId)]
);
if (response.documents.length > 0) {
const userProfile = response.documents[0];
setProfile(userProfile);
// Fetch reviews using the document ID
const fetchedReviews = await fetchReviewsForConsumer(userProfile.$id);
setReviews(fetchedReviews);
} else {
setMessage('Profile not found.');
}
} catch (error) {
console.error('Error fetching profile:', error);
setMessage('Error fetching profile.');
}
};
fetchProfile();
}, []);
const toggleSection = (section: string) => {
setExpandedSection(expandedSection === section ? null : section);
setActiveSetting(null);
};
const handleSettingClick = (setting: string) => {
setActiveSetting(setting);
};
return (
<div className="bg-gray-100 rounded-lg p-6">
<div className="flex justify-between items-start mb-4 border-b border-gray-300 pb-4">
<div className="flex items-center">
<div className="w-40 h-40 flex items-center justify-center rounded-full">
<img className="w-32 h-32 rounded-full" src={profile?.profileImg} alt={profile?.name} />
</div>
<div className="mx-6">
<h2 className="font-bold text-lg">{profile?.name}</h2>
<p>{profile?.city}, {profile?.state}</p>
<p>ID: {profile?.userId}</p>
<p>Joined On: {profile?.createon?.substring(0, 10)}</p>
<p>User Type: {profile?.userType}</p>
</div>
</div>
<div className="w-5/12 items-center justify-center mt-10">
<h3 className=""><span className="label mt-1">Phone:</span>{profile?.phone}</h3>
<h3 className=""><span className="label">e-mail:</span>{profile?.email}</h3>
<h3 className=""><span className="label">Address:</span>{profile?.address}</h3>
</div>
</div>
<div className="flex">
<div className="w-full p-4">
<h3 className="my-4 cursor-pointer" onClick={() => toggleSection('accountSettings')}>
Account Settings
</h3>
{expandedSection === 'accountSettings' && (
<div className="p-4 bg-white rounded shadow">
<p className="cursor-pointer my-2 font-bold" onClick={() => handleSettingClick('changePassword')}>Change Password</p>
{activeSetting === 'changePassword' && profile && (
<div className="mt-4">
<ChangePasswordForm userId={profile.userId} />
</div>
)}
<p className="cursor-pointer my-2 font-bold" onClick={() => handleSettingClick('updateAccountInfo')}>Update Account Info</p>
{activeSetting === 'updateAccountInfo' && profile && (
<div className="mt-4">
<UpdateProfileForm profile={profile} />
</div>
)}
</div>
)}
<h3 className="my-4 cursor-pointer" onClick={() => toggleSection('currentBookings')}>
Current Bookings
</h3>
{expandedSection === 'currentBookings' && (
<div className="p-4 bg-white rounded shadow">
<p>Current bookings content...</p>
</div>
)}
<h3 className="my-4 cursor-pointer" onClick={() => toggleSection('myReviews')}>
Reviews I Wrote
</h3>
{expandedSection === 'myReviews' && (
<div className="mt-4">
<h3 className="text-2xl font-bold mb-4">Reviews</h3>
{reviews.map((review, index) => (
<ReviewCardConsumerSelf key={index} {...review} />
))}
</div>
)}
</div>
</div>
</div>
);
};
export default CustomerAccountDetails;