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

@@ -276,7 +276,7 @@ function App() {
}; };
}, [socket, shopId]); }, [socket, shopId]);
async function checkIfStillViewingOtherTransaction(){ async function checkIfStillViewingOtherTransaction() {
console.log("transaction notification"); console.log("transaction notification");
console.log(modalContent); console.log(modalContent);
@@ -323,7 +323,7 @@ function App() {
const isViewingOtherTransaction = await checkIfStillViewingOtherTransaction(); const isViewingOtherTransaction = await checkIfStillViewingOtherTransaction();
// If transaction_info is an empty string, set the modal // If transaction_info is an empty string, set the modal
if (!isViewingOtherTransaction) { if (!isViewingOtherTransaction) {
setModal("new_transaction", data); setModal("new_transaction", data);
} }
}); });
@@ -651,6 +651,7 @@ function App() {
</Routes> </Routes>
</header> </header>
<Modal <Modal
user={user}
shop={shop} shop={shop}
isOpen={isModalOpen} isOpen={isModalOpen}
modalContent={modalContent} modalContent={modalContent}

View File

@@ -1,4 +1,3 @@
// src/components/AccountUpdatePage.js
import React, { useState } from 'react'; import React, { useState } from 'react';
import styles from './AccountUpdatePage.module.css'; // Adjust CSS if needed import styles from './AccountUpdatePage.module.css'; // Adjust CSS if needed
import { updateUser } from '../helpers/userHelpers'; import { updateUser } from '../helpers/userHelpers';
@@ -8,21 +7,36 @@ const AccountUpdatePage = ({ user, showEmail, onSubmit }) => {
username: user.username.startsWith('guest') ? '' : user.username || '', username: user.username.startsWith('guest') ? '' : user.username || '',
email: user.email || '', email: user.email || '',
password: user.password === 'unsetunsetunset' ? '' : user.password || '', 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) => { const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value }); setFormData({ ...formData, [e.target.name]: e.target.value });
}; };
const handleSubmit = async (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
// Validate password length on the frontend
if (formData.password && formData.password.length < 6) {
setErrorMessage('Password is too short');
return;
}
try { try {
const response = await updateUser(formData); const response = await updateUser(formData); // Call the updated helper function
console.log('User updated successfully:', response);
onSubmit(formData); // 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) { } 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> </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}> <button type="submit" className={styles.submitButton}>
Submit Submit

View File

@@ -1,27 +1,131 @@
/* src/components/AccountUpdateModal.module.css */ /* AccountUpdatePage.module.css */
.modalOverlay { /* General container styles */
position: fixed; .container {
top: 0; max-width: 600px;
left: 0; margin: 0 auto;
width: 100%; padding: 20px;
height: 100%; background-color: #f9f9f9;
background: rgba(0, 0, 0, 0.5); border-radius: 8px;
display: flex; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
align-items: center; font-family: Arial, sans-serif;
justify-content: center; }
z-index: 101;
/* Heading styles */
h2 {
text-align: center;
font-size: 1.8rem;
color: #333;
margin-bottom: 20px;
}
/* Form styles */
.form {
display: flex;
flex-direction: column;
gap: 20px;
}
/* Label and input styles */
.formLabel {
font-size: 1rem;
color: #555;
font-weight: 600;
}
.formInput {
padding: 10px;
margin-top: 8px;
font-size: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
width: 100%;
box-sizing: border-box;
transition: border 0.3s ease;
}
.formInput:focus {
border: 1px solid #007BFF;
outline: none;
}
/* Submit button styles */
.submitButton {
padding: 12px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s ease;
}
.submitButton:hover {
background-color: #0056b3;
}
/* Mobile responsive design */
@media (max-width: 768px) {
.container {
padding: 15px;
width: 90%;
} }
.modalContent { h2 {
background: white; font-size: 1.6rem;
padding: 20px;
border-radius: 10px;
max-width: 500px;
width: 100%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
position: relative;
z-index: 11;
color: black;
} }
.formInput {
font-size: 0.9rem;
}
.submitButton {
padding: 10px;
font-size: 0.95rem;
}
}
/* Tablet and desktop view adjustments */
@media (min-width: 769px) {
.container {
padding: 20px;
width: 50%;
}
h2 {
font-size: 2rem;
}
.formInput {
font-size: 1rem;
}
.submitButton {
padding: 12px;
font-size: 1.1rem;
}
}
/* Additional Accessibility Enhancements */
.formInput:focus {
border-color: #007BFF;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
/* AccountUpdatePage.module.css */
/* Success message styles */
.successMessage {
color: green;
font-size: 1rem;
margin-top: 10px;
text-align: center;
}
/* Error message styles */
.errorMessage {
color: red;
font-size: 1rem;
margin-top: 10px;
text-align: center;
}

View File

@@ -27,7 +27,7 @@ import Login from "../pages/Login";
import ResetPassword from "../pages/ResetPassword"; import ResetPassword from "../pages/ResetPassword";
import { getImageUrl } from "../helpers/itemHelper.js"; import { getImageUrl } from "../helpers/itemHelper.js";
const Modal = ({ shop, isOpen, onClose, modalContent, setModal, handleMoveToTransaction,welcomePageConfig }) => { const Modal = ({ user, shop, isOpen, onClose, modalContent, setModal, handleMoveToTransaction,welcomePageConfig }) => {
const [shopImg, setShopImg] = useState(''); const [shopImg, setShopImg] = useState('');
@@ -55,7 +55,7 @@ const Modal = ({ shop, isOpen, onClose, modalContent, setModal, handleMoveToTran
<div onClick={handleOverlayClick} className={styles.modalOverlay}> <div onClick={handleOverlayClick} className={styles.modalOverlay}>
<div className={styles.modalContent} onClick={handleContentClick}> <div className={styles.modalContent} onClick={handleContentClick}>
{modalContent === "edit_account" && <AccountUpdatePage shop={shop} />} {modalContent === "edit_account" && <AccountUpdatePage user={user} />}
{modalContent === "join" && <Join />} {modalContent === "join" && <Join />}
{modalContent === "reset-password" && <ResetPassword />} {modalContent === "reset-password" && <ResetPassword />}
{modalContent === "req_notification" && <NotificationRequest setModal={setModal} />} {modalContent === "req_notification" && <NotificationRequest setModal={setModal} />}

View File

@@ -146,30 +146,44 @@ export const loginUser = async (username, password) => {
return { success: false, token: null }; return { success: false, token: null };
} }
}; };
export const updateUser = async (formData) => { export const updateUser = async (formData) => {
const token = getLocalStorage("auth"); const token = getLocalStorage("auth"); // Retrieve token from local storage
if (token) {
try { if (!token) {
const response = await fetch(API_BASE_URL + "/user/update-user", { // Handle missing token scenario
method: "POST", throw new Error("User not authenticated. No token found.");
headers: { }
"Content-Type": "application/json",
Authorization: `Bearer ${token}`, try {
}, const response = await fetch(API_BASE_URL + "/user/update-user", {
body: JSON.stringify(formData), method: "POST",
}); headers: {
if (!response.ok) { "Content-Type": "application/json",
throw new Error(`HTTP error! status: ${response.status}`); Authorization: `Bearer ${token}`,
} },
body: JSON.stringify(formData),
});
// Check if response status is not ok (e.g., 400 or 500 errors)
if (!response.ok) {
const data = await response.json(); const data = await response.json();
return data;
} catch (error) { // If the response body has an error, throw it to propagate to the frontend
console.error("Error updating user:", error); throw new Error(data.error || `Error: ${response.statusText}`);
} }
// If the response is OK, return the data
const data = await response.json();
return data;
} catch (error) {
// Log and rethrow error to be handled in the calling function
console.error("Error updating user:", error);
throw error; // Re-throw the error so the calling function can handle it
} }
}; };
//for super //for super
export const getAnalytics = async (formData) => { export const getAnalytics = async (formData) => {
const token = getLocalStorage("auth"); const token = getLocalStorage("auth");