Skip to content

Commit

Permalink
🎨 Update Format
Browse files Browse the repository at this point in the history
  • Loading branch information
nmdra committed Oct 14, 2024
1 parent 3f1d7bb commit 8b6fcc7
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 128 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,4 @@ export const deleteUserAccount = async (req, res, next) => {
} catch (error) {
return next(error)
}
}
}
12 changes: 6 additions & 6 deletions backend/routes/Blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const router = Router()
// Function to validate required fields with enhanced rules
const validateBlogFields = (title, content, author) => {
const errors = {}

if (!title) {
errors.title = 'Title is required.'
} else if (title.length < 5) {
Expand All @@ -18,7 +18,7 @@ const validateBlogFields = (title, content, author) => {
} else if (content.length < 10) {
errors.content = 'Content must be at least 10 characters long.'
}

if (!author) {
errors.author = 'Author is required.'
} else if (author.trim().length === 0) {
Expand All @@ -37,9 +37,9 @@ router.post('/add', async (req, res) => {
const errors = validateBlogFields(title, content, author)
if (Object.keys(errors).length) {
// Return a structured response showing all validation errors
return res.status(400).json({
return res.status(400).json({
message: 'Validation failed. Please fix the errors below.',
errors
errors,
})
}

Expand Down Expand Up @@ -96,9 +96,9 @@ router.put('/update/:id', async (req, res) => {
const errors = validateBlogFields(title, content, author)
if (Object.keys(errors).length) {
// Return a structured response showing all validation errors
return res.status(400).json({
return res.status(400).json({
message: 'Validation failed. Please fix the errors below.',
errors
errors,
})
}

Expand Down
37 changes: 27 additions & 10 deletions frontend/src/Components/ConfirmationModalDelete.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import React from 'react';
import React from 'react'

Check failure on line 1 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L1

[no-unused-vars] 'React' is defined but never used.

const ConfirmationModalDelete = ({ isOpen, onClose, onConfirm, confirmationText, setConfirmationText }) => {
if (!isOpen) return null;
const ConfirmationModalDelete = ({
isOpen,

Check failure on line 4 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L4

[react/prop-types] 'isOpen' is missing in props validation
onClose,

Check failure on line 5 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L5

[react/prop-types] 'onClose' is missing in props validation
onConfirm,

Check failure on line 6 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L6

[react/prop-types] 'onConfirm' is missing in props validation
confirmationText,

Check failure on line 7 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L7

[react/prop-types] 'confirmationText' is missing in props validation
setConfirmationText,

Check failure on line 8 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L8

[react/prop-types] 'setConfirmationText' is missing in props validation
}) => {
if (!isOpen) return null

return (
<div className="fixed inset-0 bg-gray-800 bg-opacity-50 flex justify-center items-center z-50">
<div className="bg-white p-6 rounded shadow-md w-1/3">
<h2 className="text-xl font-semibold mb-4">Confirm Account Deletion</h2>
<p>Type <strong>"I want to delete my account"</strong> to confirm:</p>
<h2 className="text-xl font-semibold mb-4">
Confirm Account Deletion
</h2>
<p>
Type <strong>"I want to delete my account"</strong> to

Check failure on line 19 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L19

[react/no-unescaped-entities] `"` can be escaped with `&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`.

Check failure on line 19 in frontend/src/Components/ConfirmationModalDelete.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/ConfirmationModalDelete.jsx#L19

[react/no-unescaped-entities] `"` can be escaped with `&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`.
confirm:
</p>
<input
type="text"
value={confirmationText}
onChange={(e) => setConfirmationText(e.target.value)}
className="border border-gray-300 p-2 rounded mb-4 w-full"
/>
<div className="flex justify-end">
<button onClick={onClose} className="mr-2 bg-gray-300 text-gray-700 font-bold py-1 px-3 rounded">
<button
onClick={onClose}
className="mr-2 bg-gray-300 text-gray-700 font-bold py-1 px-3 rounded"
>
Cancel
</button>
<button onClick={onConfirm} className="bg-red-600 text-white font-bold py-1 px-3 rounded">
<button
onClick={onConfirm}
className="bg-red-600 text-white font-bold py-1 px-3 rounded"
>
Confirm
</button>
</div>
</div>
</div>
);
};
)
}

export default ConfirmationModalDelete;
export default ConfirmationModalDelete
40 changes: 21 additions & 19 deletions frontend/src/Components/DeleteAccountButton.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import React, { useState } from 'react';
import { toast } from 'react-hot-toast';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
import ConfirmationModal from './ConfirmationModalDelete'; // Import the modal component
import React, { useState } from 'react'

Check failure on line 1 in frontend/src/Components/DeleteAccountButton.jsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

frontend/src/Components/DeleteAccountButton.jsx#L1

[no-unused-vars] 'React' is defined but never used.
import { toast } from 'react-hot-toast'
import axios from 'axios'
import { useNavigate } from 'react-router-dom'
import ConfirmationModal from './ConfirmationModalDelete' // Import the modal component

const DeleteAccountButton = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [confirmationText, setConfirmationText] = useState('');
const navigate = useNavigate();
const [isModalOpen, setIsModalOpen] = useState(false)
const [confirmationText, setConfirmationText] = useState('')
const navigate = useNavigate()

const handleDeleteAccount = async () => {
if (confirmationText !== 'I want to delete my account') {
toast.error('Please type "I want to delete my account" to confirm.');
return;
toast.error('Please type "I want to delete my account" to confirm.')
return
}

try {
const response = await axios.delete('/api/users'); // Adjust the endpoint as necessary
toast.success(response.data.message);
const response = await axios.delete('/api/users') // Adjust the endpoint as necessary
toast.success(response.data.message)

// Redirect to the registration page after deletion
navigate('/register'); // Adjust the path as necessary
navigate('/register') // Adjust the path as necessary
} catch (error) {
toast.error(error.response?.data?.message || 'Failed to delete account');
toast.error(
error.response?.data?.message || 'Failed to delete account'
)
}
};
}

return (
<div>
Expand All @@ -43,7 +45,7 @@ const DeleteAccountButton = () => {
setConfirmationText={setConfirmationText}
/>
</div>
);
};
)
}

export default DeleteAccountButton;
export default DeleteAccountButton
4 changes: 1 addition & 3 deletions frontend/src/Components/Home/ShopList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const ShopList = () => {
const fetchShops = async () => {
try {
// Use the VITE_API_URL environment variable from .env file
const { data } = await axios.get(
`/api/shops/all-shops`
)
const { data } = await axios.get(`/api/shops/all-shops`)
setShops(data) // Set shop data in state
setLoading(false) // Turn off loading
} catch (error) {
Expand Down
70 changes: 38 additions & 32 deletions frontend/src/Pages/Admin/AaddCoupons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ import Swal from 'sweetalert2'
import Sidebar from '../../Components/Admin/AsideBar'

const AddCoupon = () => {
const navigate = useNavigate();
const navigate = useNavigate()
const [formData, setFormData] = useState({
couponCode: '',
discount: '',
expiryDate: '',
});
const [loading, setLoading] = useState(false);
})
const [loading, setLoading] = useState(false)
const [errors, setErrors] = useState({
couponCode: '',
discount: '',
expiryDate: '',
});
})

// Handle input change with validation
const handleChange = (e) => {
const { name, value } = e.target;
let error = '';
const { name, value } = e.target
let error = ''

// Coupon Code validation: Ensure it's not empty
if (name === 'couponCode' && value === '') {
error = 'Coupon code cannot be empty';
error = 'Coupon code cannot be empty'
}

// Discount validation: Must be a number and <= 100
if (name === 'discount') {
const discountValue = parseInt(value, 10);
const discountValue = parseInt(value, 10)
if (isNaN(discountValue) || discountValue < 0) {
error = 'Discount must be a positive number';
error = 'Discount must be a positive number'
} else if (discountValue > 100) {
error = 'Discount cannot be more than 100';
error = 'Discount cannot be more than 100'
}
}
// Expiry date validation: Must be today or a future date
Expand All @@ -48,45 +48,45 @@ const AddCoupon = () => {
}

// Update form data and errors
setFormData({ ...formData, [name]: value });
setErrors({ ...errors, [name]: error });
};
setFormData({ ...formData, [name]: value })
setErrors({ ...errors, [name]: error })
}

// Handle form submission
const handleSubmit = async (e) => {
e.preventDefault();
e.preventDefault()
// Check if any validation errors exist
if (Object.values(errors).some((err) => err)) {
Swal.fire('Validation Error', 'Please fix the errors', 'error');
return;
Swal.fire('Validation Error', 'Please fix the errors', 'error')
return
}

try {
setLoading(true);
setLoading(true)

await axios.post('/coupon', formData, {
headers: {
Authorization: `Bearer ${localStorage.getItem('token')}`,
},
});
})

Swal.fire('Success', 'Coupon created successfully', 'success');
navigate('/coupons');
Swal.fire('Success', 'Coupon created successfully', 'success')
navigate('/coupons')
} catch (err) {
console.error('Error creating coupon:', err);
console.error('Error creating coupon:', err)
Swal.fire(
'Error',
'Error creating coupon, please try again',
'error'
);
)
} finally {
setLoading(false);
setLoading(false)
}
};
}

const handleCancel = () => {
navigate('/coupons');
};
navigate('/coupons')
}

return (
<div className="flex min-h-screen bg-gray-50">
Expand Down Expand Up @@ -137,8 +137,14 @@ const AddCoupon = () => {
value={formData.discount}
onChange={handleChange}
onBlur={() => {
if (parseInt(formData.discount, 10) > 100) {
setFormData({ ...formData, discount: '100' });
if (
parseInt(formData.discount, 10) >
100
) {
setFormData({
...formData,
discount: '100',
})
}
}}
required
Expand All @@ -161,7 +167,7 @@ const AddCoupon = () => {
onChange={handleChange}
required
min="2024-10-15"
max="2030-10-15"
max="2030-10-15"
/>
{errors.expiryDate && (
<p className="text-red-500 text-xs">
Expand Down Expand Up @@ -190,7 +196,7 @@ const AddCoupon = () => {
</form>
</div>
</div>
);
};
)
}

export default AddCoupon;
export default AddCoupon
Loading

0 comments on commit 8b6fcc7

Please sign in to comment.