UserProfile.jsx 5.61 KB
import React, { useState } from 'react'
import { useAuth } from '../context/AuthContext'
import { userService } from '../services/api/userService'
import LoadingSpinner from '../components/common/LoadingSpinner'
import toast from 'react-hot-toast'

const UserProfile = () => {
  const { user, updateProfile } = useAuth()
  const [activeTab, setActiveTab] = useState('profile')
  const [loading, setLoading] = useState(false)
  const [formData, setFormData] = useState({
    name: user?.name || '',
    email: user?.email || '',
    phone: user?.phone || '',
    address: user?.address || ''
  })

  const handleChange = (e) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value
    })
  }

  const handleProfileSubmit = async (e) => {
    e.preventDefault()
    setLoading(true)
    
    try {
      const result = await updateProfile(formData)
      if (result.success) {
        toast.success('Profile updated successfully!')
      } else {
        toast.error(result.message)
      }
    } catch (error) {
      toast.error('Failed to update profile')
    } finally {
      setLoading(false)
    }
  }

  if (!user) {
    return <LoadingSpinner />
  }

  return (
    <div className="user-profile">
      <div className="container">
        <div className="profile-header">
          <h1>My Account</h1>
          <p>Welcome back, {user.name}!</p>
        </div>

        <div className="profile-content">
          <div className="profile-sidebar">
            <nav className="profile-nav">
              <button
                onClick={() => setActiveTab('profile')}
                className={`nav-item ${activeTab === 'profile' ? 'active' : ''}`}
              >
                Profile Information
              </button>
              <button
                onClick={() => setActiveTab('orders')}
                className={`nav-item ${activeTab === 'orders' ? 'active' : ''}`}
              >
                Order History
              </button>
              <button
                onClick={() => setActiveTab('addresses')}
                className={`nav-item ${activeTab === 'addresses' ? 'active' : ''}`}
              >
                Address Book
              </button>
              <button
                onClick={() => setActiveTab('security')}
                className={`nav-item ${activeTab === 'security' ? 'active' : ''}`}
              >
                Security
              </button>
            </nav>
          </div>

          <div className="profile-main">
            {activeTab === 'profile' && (
              <div className="profile-section">
                <h2>Profile Information</h2>
                <form onSubmit={handleProfileSubmit}>
                  <div className="form-row">
                    <div className="form-group">
                      <label htmlFor="name">Full Name</label>
                      <input
                        type="text"
                        id="name"
                        name="name"
                        value={formData.name}
                        onChange={handleChange}
                        required
                      />
                    </div>
                    <div className="form-group">
                      <label htmlFor="email">Email Address</label>
                      <input
                        type="email"
                        id="email"
                        name="email"
                        value={formData.email}
                        onChange={handleChange}
                        required
                      />
                    </div>
                  </div>

                  <div className="form-group">
                    <label htmlFor="phone">Phone Number</label>
                    <input
                      type="tel"
                      id="phone"
                      name="phone"
                      value={formData.phone}
                      onChange={handleChange}
                    />
                  </div>

                  <div className="form-group">
                    <label htmlFor="address">Address</label>
                    <textarea
                      id="address"
                      name="address"
                      value={formData.address}
                      onChange={handleChange}
                      rows="3"
                    />
                  </div>

                  <button 
                    type="submit" 
                    className="save-button"
                    disabled={loading}
                  >
                    {loading ? 'Saving...' : 'Save Changes'}
                  </button>
                </form>
              </div>
            )}

            {activeTab === 'orders' && (
              <div className="profile-section">
                <h2>Order History</h2>
                <p>View and manage your recent orders.</p>
                <a href="/orders" className="view-orders-button">
                  View All Orders
                </a>
              </div>
            )}

            {activeTab === 'addresses' && (
              <div className="profile-section">
                <h2>Address Book</h2>
                <p>Manage your shipping addresses.</p>
                {/* Address management component would go here */}
              </div>
            )}

            {activeTab === 'security' && (
              <div className="profile-section">
                <h2>Security Settings</h2>
                <p>Update your password and security preferences.</p>
                {/* Password change component would go here */}
              </div>
            )}
          </div>
        </div>
      </div>
    </div>
  )
}

export default UserProfile