Newer
Older
import yellowSuit from './assets/suitmeister-gul-kostym.jpg';
import axios from 'axios';
import noNotificationsImage from './assets/emoji2.png'; // Import your no notifications image
import './ProfilePage.css'; // Import the CSS file
import Button from '@mui/material/Button';
import Box from '@mui/material/Box';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import TextField from '@mui/material/TextField';
import MenuItem from '@mui/material/MenuItem';
import { addProduct, getLoggedInUser, getAllProducts } from './utils/api';
{ name: 'GÖRAN', email: 'pengarfinns@inte.nu', age: '48', profilePicture: yellowSuit }
];
const sizes = ['X-Small', 'Small', 'Medium', 'Large', 'X-Large'];
const ProfilePage = () => {
// Accessing the first element of the user array
//const [products, setProducts] = useState(initialProducts);
// State to manage modal visibility
const [open, setOpen] = useState(false);
// State to manage form data
const [formData, setFormData] = useState({
name: '',
picture: null,
price: '',
rentPrice: '',
size: '',
user_id: 1,
});
// State to manage notifications
const [notifications, setNotifications] = useState([
'Your order has been shipped!',
'You have a new message from support.',
'Your profile has been updated successfully.'
]);
// Function to remove a notification
const removeNotification = (index) => {
setNotifications(notifications.filter((_, i) => i !== index));
};
// Function to handle opening the modal
const handleClickOpen = () => {
setOpen(true);
};
// Function to handle closing the modal
const handleClose = () => {
setOpen(false);
};
// Function to handle form input changes
const handleChange = (e) => {
const { name, value, files } = e.target;
setFormData({
...formData,
[name]: files ? files[0] : value,
});
};
// Function to handle form submission
const handleSubmit = async (event) => {
event.preventDefault();
const newProduct = {
name: formData.name,
picture: formData.picture,
price: formData.price,
rentPrice: formData.rentPrice,
size: formData.size,
try {
const result = await addProduct(newProduct.picture, newProduct.price, newProduct.rentPrice, newProduct.name, newProduct.size, newProduct.userID);
if (result === 201) { // Assuming 201 is the status code for successful creation
setProducts([...products, result.data]); // Assuming the API returns the new product data
console.log('Product added successfully:', result.data);
window.location.reload();
handleClose(); // Close the modal or form
}
} catch (error) {
console.error('Error adding product:', error);
}
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [user, setUser] = useState(null);
const [loadingUser, setLoadingUser] = useState(true);
const [errorUser, setErrorUser] = useState(null);
const [products, setProducts] = useState([]);
const [loadingProducts, setLoadingProducts] = useState(true);
const [errorProducts, setErrorProducts] = useState(null);
//const [categories, setUser] = useState([]);
//const [loadingCategories, setLoadingUser] = useState(true);
//const [errorCategories, setErrorUser] = useState(null);
//const initialProducts = [
// { name: 'Nunna', picture: nun, price: '29.99' },
// { name: 'Sjuksköterska', picture: sjuksköterska, price: '39.99' },
// { name: 'Nunna', picture: nun, price: '29.99' },
// { name: 'Sjuksköterska', picture: sjuksköterska, price: '39.99' },
// { name: 'Nunna', picture: nun, price: '29.99' },
// { name: 'Sjuksköterska', picture: sjuksköterska, price: '39.99' },
//];
//useEffect(() => {
// axios.get('http://127.0.0.1:8000/themeApp/user/')
// .then(response => {
// setUser(response.data.user);
// console.log("SUcccesss : ", response.data.user);
// setLoadingUser(false);
// })
// .catch(error => {
// setErrorUser(error);
// setLoadingUser(false);
// });
//if (loading) return <p>Loading...</p>;
//if (error) return <p>Error loading products: {error.message}</p>;
// Fetch user when the component mounts
useEffect(() => {
const fetchUser = async () => {
try {
const result = await getLoggedInUser();
setUser(result.id);
// You can perform additional actions here, like updating the UI
} catch (error) {
console.error('Error:', error);
setErrorUser(error);
} finally {
setLoadingUser(false);
}
};
fetchUser();
}, []);
useEffect(() => {
const fetchProducts = async () => {
try {
const result = await getAllProducts();
if (user) {
const filteredProducts = result.data.filter(product => product.user_id === user);
setProducts(filteredProducts);
} else {
console.warn('User ID is null or undefined');
}
} catch (error) {
console.error('Error:', error);
setErrorProducts(error);
} finally {
setLoadingProducts(false);
}
};
fetchProducts();
}, [user]); // Include user in the dependency array
<div className="container">
<div className="header">
<h1 className="headerText">Välkommen, {currentUser.name}!</h1>
<div className="profile">
<img src={currentUser.profilePicture} alt="Profile" className="profilePicture" />
<div className="profileInfo">
<h2>Användarinformation:</h2>
<p>Namn: {currentUser.name}</p>
<p>Email: {currentUser.email}</p>
{/* Add more user information fields as needed */}
</div>
<Box sx={{display: 'flex', justifyContent:'center'}}>
<Button variant="contained" onClick={handleClickOpen}>Lägg till ny produkt</Button>
</Box>
<Dialog open={open} onClose={handleClose}>
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<DialogContent>
<TextField
autoFocus
margin="dense"
name="name"
label="Product Name"
type="text"
fullWidth
variant="standard"
value={formData.name}
onChange={handleChange}
/>
<TextField
margin="dense"
name="price"
label="Price"
type="number"
fullWidth
variant="standard"
value={formData.price}
onChange={handleChange}
/>
<TextField
margin="dense"
name="rentPrice"
label="Daily Rent Price"
type="number"
fullWidth
variant="standard"
value={formData.rentPrice}
onChange={handleChange}
/>
<TextField
margin="dense"
name="size"
label="Size"
select
fullWidth
variant="standard"
value={formData.size}
onChange={handleChange}
>
{sizes.map((size) => (
<MenuItem key={size} value={size}>
{size}
</MenuItem>
))}
</TextField>
<input
accept="image/*"
style={{ display: 'none' }}
id="upload-image"
type="file"
name="picture"
onChange={handleChange}
/>
<label htmlFor="upload-image">
<Button variant="contained" component="span">
Upload Image
</Button>
</label>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel</Button>
<Button onClick={handleSubmit} variant="contained">Lägg till produkt</Button>
</DialogActions>
</Dialog>
<div className="mainContent">
<div className="productListContainer">
<h2 style={{ textAlign: 'center' }}>Currently Listed Products:</h2>
<ul className="productList">
{products.map((product, index) => (
<li key={index} className="productItem" style={{ animationDelay: `${index * 0.4}s` }}>
<img src={product.image} alt={product.name} className="productImage"/>
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<div className="productInfo">
<h3 className="productName">{product.name}</h3>
<p className="productPrice">Price: ${product.price}</p>
</div>
</li>
))}
</ul>
</div>
<div className="notificationSection">
<h2 style={{ textAlign: 'center' }}>Notifications:</h2>
{notifications.length === 0 ? (
<div className="noNotifications">
<p className="noNotificationsText">No notifications at the moment</p>
<img src={noNotificationsImage} alt="No Notifications" className="noNotificationsImage" />
</div>
) : (
<ul className="notificationList">
{notifications.map((notification, index) => (
<li key={index} className="notificationItem">
{notification}
<button onClick={() => removeNotification(index)} className="removeButton">Mark as Read</button>
</li>
))}
</ul>
)}
</div>
</div>