This commit is contained in:
zadit
2024-12-28 07:39:48 +07:00
parent 47bb8b40c8
commit 9a89e3e996
5 changed files with 205 additions and 68 deletions

View File

@@ -1,4 +1,3 @@
// src/components/AccountUpdatePage.js
import React, { useState } from 'react';
import styles from './AccountUpdatePage.module.css'; // Adjust CSS if needed
import { updateUser } from '../helpers/userHelpers';
@@ -8,21 +7,36 @@ const AccountUpdatePage = ({ user, showEmail, onSubmit }) => {
username: user.username.startsWith('guest') ? '' : user.username || '',
email: user.email || '',
password: user.password === 'unsetunsetunset' ? '' : user.password || '',
// Add other fields as needed
});
const [errorMessage, setErrorMessage] = useState('');
const [successMessage, setSuccessMessage] = useState(''); // New state for success messages
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
const handleSubmit = async (e) => {
e.preventDefault();
// Validate password length on the frontend
if (formData.password && formData.password.length < 6) {
setErrorMessage('Password is too short');
return;
}
try {
const response = await updateUser(formData);
console.log('User updated successfully:', response);
onSubmit(formData);
const response = await updateUser(formData); // Call the updated helper function
// If there's a success message, display it
if (response.message) {
setSuccessMessage(response.message); // Set the success message from the response
setErrorMessage(''); // Clear any previous error messages
}
} catch (error) {
console.error('Failed to update user:', error);
// Handle the error by displaying the error message
setErrorMessage(error.message || 'Failed to update user. Please try again.');
setSuccessMessage(''); // Clear success message if there's an error
}
};
@@ -65,7 +79,11 @@ const AccountUpdatePage = ({ user, showEmail, onSubmit }) => {
/>
</label>
{/* Add other fields as needed */}
{/* Display success message */}
{successMessage && <div className={styles.successMessage}>{successMessage}</div>}
{/* Display error message */}
{errorMessage && <div className={styles.errorMessage}>{errorMessage}</div>}
<button type="submit" className={styles.submitButton}>
Submit