To set a maximum limit of 4 buttons in your SpecialiteForm component, you'll need to implement the following changes:
-
Limit Button Addition:
- Prevent users from adding more than 4 buttons by disabling the "Add Button" when the limit is reached.
- Optionally, display an error message when users attempt to exceed the limit.
-
Handle Edge Cases:
- Ensure that when loading existing data, if there are already 4 buttons, the "Add Button" remains disabled.
- Maintain consistent behavior across all interactions.
Here's how you can implement these changes:
1. Update the handleButtonAdd Function
Modify the handleButtonAdd function to check the current number of buttons before adding a new one. If the limit is reached, you can set an error message.
const handleButtonAdd = () => {
if (formData.buttons.length >= 4) {
setError('You can only add up to 4 buttons.');
return;
}
setFormData((prev) => ({
...prev,
buttons: [...prev.buttons, ''],
}));
};
2. Disable the "Add Button" When Limit is Reached
In the Buttons Section of your form, disable the "Add Button" when there are already 4 buttons. This provides immediate feedback to the user and prevents unnecessary clicks.
<Button
startIcon={<AddIcon />}
onClick={handleButtonAdd}
variant="outlined"
sx={{ mb: 2 }}
disabled={formData.buttons.length >= 4}
>
Add Button
</Button>
3. (Optional) Provide User Feedback
If you want to give users additional feedback when they reach the maximum number of buttons, you can conditionally render a message. However, since we're already disabling the button and setting an error message, this might be redundant. Use this based on your UX preference.
{formData.buttons.length >= 4 && (
<Typography variant="body2" color="textSecondary">
Maximum of 4 buttons reached.
</Typography>
)}
4. Complete Modified Code
Below is your complete SpecialiteForm component incorporating the aforementioned changes:
import React, { useState, useEffect } from 'react';
import axios from 'axios'; // Import axios
import {
Box,
TextField,
Button,
Typography,
Grid,
IconButton,
Stack,
Alert,
} from '@mui/material';
import DeleteIcon from '@mui/icons-material/Delete';
import AddIcon from '@mui/icons-material/Add';
const SpecialiteForm = () => {
const [formData, setFormData] = useState({
title: '',
buttons: [''],
description: '',
image: null,
image_path: '',
section_title: '',
section_description: '',
cta_button: '',
});
const [preview, setPreview] = useState('');
const [error, setError] = useState('');
const [success, setSuccess] = useState('');
useEffect(() => {
const timer = setTimeout(() => {
setError('');
setSuccess('');
}, 5000);
return () => clearTimeout(timer);
}, [error, success]);
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get('http://127.0.0.1:8000/specialite-page');
const result = response.data;
if (result.status === 'success') {
const data = result.data;
setFormData({
...data,
buttons: JSON.parse(data.buttons),
image: null,
});
if (data.image_path) {
setPreview(`http://127.0.0.1:8000/storage/${data.image_path}`);
}
} else {
setError('Failed to load existing data');
}
} catch (err) {
console.error(err);
setError('Failed to load existing data');
}
};
fetchData();
}, []);
const handleInputChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({
...prev,
[name]: value,
}));
};
const handleImageChange = (e) => {
const file = e.target.files[0];
if (file) {
if (file.size > 2048000) { // 2MB
setError('Image size should be less than 2MB');
return;
}
if (
![
'image/jpeg',
'image/png',
'image/jpg',
'image/gif',
'image/svg+xml',
].includes(file.type)
) {
setError('Invalid file type. Please upload an image (JPEG, PNG, JPG, GIF, SVG)');
return;
}
setFormData((prev) => ({
...prev,
image: file,
}));
setPreview(URL.createObjectURL(file));
}
};
const handleButtonAdd = () => {
if (formData.buttons.length >= 4) {
setError('You can only add up to 4 buttons.');
return;
}
setFormData((prev) => ({
...prev,
buttons: [...prev.buttons, ''],
}));
};
const handleButtonRemove = (index) => {
setFormData((prev) => ({
...prev,
buttons: prev.buttons.filter((_, i) => i !== index),
}));
};
const handleButtonChange = (index, value) => {
setFormData((prev) => ({
...prev,
buttons: prev.buttons.map((button, i) => (i === index ? value : button)),
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
const formDataToSend = new FormData();
Object.keys(formData).forEach((key) => {
if (key === 'buttons') {
// Append each button individually
formData[key].forEach((button) => {
formDataToSend.append(`${key}[]`, button);
});
} else if (key === 'image' && formData[key]) {
formDataToSend.append(key, formData[key]);
} else if (key !== 'image_path' && key !== 'image') {
formDataToSend.append(key, formData[key]);
}
});
try {
const response = await axios.post('http://127.0.0.1:8000/specialite-page', formDataToSend, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
const result = response.data;
if (result.status === 'success') {
setSuccess('Specialite page updated successfully');
if (result.data.image_path) {
setPreview(`http://127.0.0.1:8000/storage/${result.data.image_path}`);
setFormData((prev) => ({
...prev,
image_path: result.data.image_path,
image: null,
}));
}
} else {
setError('Failed to update specialite page');
}
} catch (err) {
console.error(err);
setError('An error occurred while submitting the form');
}
};
return (
<Box>
<Typography variant="h4" gutterBottom>
Specialite Page Settings
</Typography>
{error && <Alert severity="error" sx={{ mb: 2 }}>{error}</Alert>}
{success && <Alert severity="success" sx={{ mb: 2 }}>{success}</Alert>}
<form onSubmit={handleSubmit}>
<Grid container spacing={3}>
{/* Title Field */}
<Grid item xs={12}>
<TextField
fullWidth
label="Title"
name="title"
value={formData.title}
onChange={handleInputChange}
inputProps={{
maxLength:24
}}
required
/>
</Grid>
{/* Description Field */}
<Grid item xs={12}>
<TextField
fullWidth
multiline
rows={4}
label="Description"
name="description"
value={formData.description}
inputProps={{
maxLength:250
}}
onChange={handleInputChange}
required
/>
</Grid>
{/* Buttons Section */}
<Grid item xs={12}>
<Typography variant="subtitle1" gutterBottom>
Buttons
</Typography>
{formData.buttons.map((button, index) => (
<Stack
key={index}
direction="row"
spacing={2}
sx={{ mb: 2 }}
alignItems="center"
>
<TextField
fullWidth
label={`Button ${index + 1}`}
value={button}
inputProps={{
maxLength:20
}}
onChange={(e) => handleButtonChange(index, e.target.value)}
required
/>
<IconButton
onClick={() => handleButtonRemove(index)}
disabled={formData.buttons.length === 1}
>
<DeleteIcon />
</IconButton>
</Stack>
))}
<Button
startIcon={<AddIcon />}
onClick={handleButtonAdd}
variant="outlined"
sx={{ mb: 2 }}
disabled={formData.buttons.length >= 4}
>
Add Button
</Button>
{/* Optional: Display a message when maximum buttons are reached */}
{formData.buttons.length >= 4 && (
<Typography variant="body2" color="textSecondary">
Maximum of 4 buttons reached.
</Typography>
)}
</Grid>
{/* Image Upload */}
<Grid item xs={12}>
<Button
variant="contained"
component="label"
fullWidth
sx={{ mb: 2 }}
>
Upload Image
<input
type="file"
hidden
accept="image/*"
onChange={handleImageChange}
/>
</Button>
{preview && (
<Box sx={{ mt: 2, mb: 2 }}>
<Typography variant="subtitle2" gutterBottom>
{formData.image ? 'New Image Preview:' : 'Current Image:'}
</Typography>
<img
src={preview}
alt="Preview"
style={{
maxWidth: '100%',
maxHeight: '200px',
border: '1px solid #ddd',
borderRadius: '4px',
padding: '4px',
}}
/>
</Box>
)}
</Grid>
{/* Section Title */}
<Grid item xs={12}>
<TextField
fullWidth
label="Section Title"
name="section_title"
value={formData.section_title}
inputProps={{
maxLength:24
}}
onChange={handleInputChange}
required
/>
</Grid>
{/* Section Description */}
<Grid item xs={12}>
<TextField
fullWidth
multiline
rows={4}
label="Section Description"
name="section_description"
inputProps={{
maxLength:250
}}
value={formData.section_description}
onChange={handleInputChange}
required
/>
</Grid>
{/* CTA Button */}
<Grid item xs={12}>
<TextField
fullWidth
label="CTA Button"
name="cta_button"
inputProps={{
maxLength:15
}}
value={formData.cta_button}
onChange={handleInputChange}
required
/>
</Grid>
{/* Submit Button */}
<Grid item xs={12}>
<Button
type="submit"
variant="contained"
color="primary"
size="large"
fullWidth
>
Save Changes
</Button>
</Grid>
</Grid>
</form>
</Box>
);
};
export default SpecialiteForm;
5. Explanation of Changes
6. Testing the Implementation
After implementing the changes, ensure to test the following scenarios:
-
Adding Buttons:
- Add buttons up to the 4th one and confirm that the "Add Button" gets disabled thereafter.
- Attempting to add a 5th button should display an error message.
-
Removing Buttons:
- Remove a button and check if the "Add Button" becomes enabled again.
-
Loading Existing Data:
- If existing data has 4 buttons, ensure that the form loads correctly and the "Add Button" is disabled.
-
Form Submission:
- Ensure that the form submits correctly with the maximum number of buttons.
7. Additional Recommendations
-
Validation:
- Ensure that each button label is unique or meets any additional validation criteria you might have.
-
User Experience:
- Consider providing inline validation or real-time feedback as users interact with the form to enhance the user experience.
-
Accessibility:
- Ensure that the form is accessible, especially when disabling buttons, by providing appropriate ARIA labels or statuses.
Implementing these changes will help enforce the maximum limit of 4 buttons, providing a better user experience and maintaining data integrity.