poprawy obsługi blędów

This commit is contained in:
Wiktor Szynaka 2025-01-03 00:41:13 +01:00
parent 3643fd1377
commit f53526db82
4 changed files with 258 additions and 27 deletions

View File

@ -8,7 +8,7 @@ const ListaProduktow = ({ onAdd }) => {
const [products, setProducts] = useState([]);
const [deleteProductId, setDeleteProductId] = useState(null);
const [showModal, setShowModal] = useState(false);
const [deleteError, setDeleteError] = useState(false);
const [deleteError, setDeleteError] = useState(null);
const navigate = useNavigate();
const fetchProducts = async () => {
@ -19,7 +19,6 @@ const ListaProduktow = ({ onAdd }) => {
});
setProducts(response.data);
} catch (error) {
setDeleteError(true);
console.error('Błąd podczas pobierania produktów:', error);
}
};
@ -54,7 +53,7 @@ const ListaProduktow = ({ onAdd }) => {
setDeleteProductId(null);
} catch (error) {
setShowModal(false);
setDeleteError(true);
setDeleteError(error.response?.data || 'Nieznany błąd');
console.error('Błąd podczas usuwania produktu:', error);
}
};
@ -94,27 +93,26 @@ const ListaProduktow = ({ onAdd }) => {
{product.type === 0 ? "" : product.availability}
</td>
<td className="p-3 flex justify-center space-x-2">
<div className="flex justify-center items-center opacity-0 group-hover:opacity-100 transition-opacity duration-200">
<div className="flex justify-center items-center opacity-0 group-hover:opacity-100 transition-opacity duration-200">
<button
onClick={() => handleEditProduct(product.id)}
className="text-blue-500 hover:bg-blue-200 active:bg-blue-300 focus:outline-none p-2 rounded-full transition-colors"
>
<EditIcon className = "w-5 h-5"/>
<EditIcon className="w-5 h-5" />
</button>
<button
onClick={() => openDeleteConfirmation(product.id)}
className="text-red-500 hover:bg-red-200 active:bg-red-300 focus:outline-none p-2 rounded-full transition-colors"
>
<KoszIcon className = "w-5 h-5"/>
<KoszIcon className="w-5 h-5" />
</button>
</div>
</td>
</td>
</tr>
))}
</tbody>
</table>
</div>
))}
</tbody>
</table>
</div>
{showModal && (
<div className="fixed inset-0 bg-gray-500 bg-opacity-50 flex justify-center items-center z-50">
@ -137,19 +135,19 @@ const ListaProduktow = ({ onAdd }) => {
</div>
</div>
)}
{deleteError && (
{deleteError && (
<div className="fixed inset-0 bg-gray-500 bg-opacity-50 flex justify-center items-center z-50">
<div className="bg-white p-6 rounded-md shadow-lg w-96">
<h2 className="text-lg font-bold mb-4">Usuwanie produktu nie powiodło się.</h2>
{/*<p>Tu będzie komunikat z api</p>*/}
<button
onClick={() => setDeleteError(false)}
className="bg-gray-500 text-white py-2 px-4 rounded"
>
Wróć
</button>
</div>
<p className="text-red-500">{deleteError}</p>
<button
onClick={() => setDeleteError(null)}
className="bg-gray-500 text-white py-2 px-4 rounded"
>
Wróć
</button>
</div>
</div>
)}
</div>
);

View File

@ -9,6 +9,7 @@ const ListaTransakcji = ({ onAdd}) => {
const [transactions, setTransactions] = useState([]);
const [deleteTransactionId, setDeleteTransactionId] = useState(null);
const [showModal, setShowModal] = useState(false);
const [deleteError, setDeleteError] = useState(null);
const navigate = useNavigate();
const fetchTransactions = async () => {
@ -72,8 +73,9 @@ const ListaTransakcji = ({ onAdd}) => {
setShowModal(false);
setDeleteTransactionId(null);
} catch (error) {
console.error('Błąd podczas usuwania transakcji:', error);
}
setShowModal(false);
setDeleteError(error.response?.data || 'Nieznany błąd');
console.error('Błąd podczas usuwania produktu:', error);}
};
const openDeleteConfirmation = (transactionId) => {
@ -150,7 +152,10 @@ const ListaTransakcji = ({ onAdd}) => {
<h2 className="text-lg font-bold mb-4">Czy na pewno chcesz usunąć transakcję?</h2>
<div className="flex justify-between">
<button
onClick={handleDeleteTransaction}
onClick={() => {
handleDeleteTransaction();
setShowModal(false);
}}
className="bg-red-500 text-white py-2 px-4 rounded"
>
Tak
@ -165,6 +170,20 @@ const ListaTransakcji = ({ onAdd}) => {
</div>
</div>
)}
{deleteError && (
<div className="fixed inset-0 bg-gray-500 bg-opacity-50 flex justify-center items-center z-50">
<div className="bg-white p-6 rounded-md shadow-lg w-96">
<h2 className="text-lg font-bold mb-4">Usuwanie transakcji nie powiodło się.</h2>
<p className="text-red-500">{deleteError}</p>
<button
onClick={() => setDeleteError(null)}
className="bg-gray-500 text-white py-2 px-4 rounded"
>
Wróć
</button>
</div>
</div>
)}
</div>
);
};

View File

@ -12,6 +12,51 @@ const PanelAdministratora = () => {
const [selectedEmail, setSelectedEmail] = useState('');
const [workdays, setWorkdays] = useState([]);
const [absenceType, setAbsenceType] = useState('');
const [userEmail, setUserEmail] = useState('');
const [userPassword, setUserPassword] = useState('');
const [userRole, setUserRole] = useState('');
const [changePasswordEmail, setChangePasswordEmail] = useState('');
const [changePasswordValue, setChangePasswordValue] = useState('');
const [errors, setErrors] = useState({});
const validateInputs = () => {
const newErrors = {};
if (!userEmail) newErrors.email = "Pole email jest wymagane.";
if (!userPassword) newErrors.password = "Pole hasło jest wymagane.";
if (!userRole) newErrors.role = "Wybór roli jest wymagany.";
return newErrors;
};
const addUser = async () => {
const validationErrors = validateInputs();
if (Object.keys(validationErrors).length > 0) {
setErrors(validationErrors);
return;
}
setErrors({});
try {
await axios.post(
"https://localhost:7039/api/user/create",
{
login: userEmail,
email: userEmail,
password: userPassword,
role: userRole,
newEncryption: true,
},
{
headers: { Authorization: `Bearer ${localStorage.getItem("token")}` },
}
);
setUserEmail("");
setUserPassword("");
setUserRole("");
} catch (error) {
console.error("Błąd podczas tworzenia konta:", error);
}
};
const fetchEmails = async () => {
try {
@ -24,6 +69,32 @@ const PanelAdministratora = () => {
}
};
const changePassword = async () => {
if (!changePasswordEmail || !changePasswordValue) {
alert("Wszystkie pola muszą być wypełnione!");
return;
}
try {
await axios.post(
'https://localhost:7039/api/user/ChangeUserPassword',
{
email: changePasswordEmail,
password: changePasswordValue,
},
{
headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
}
);
setChangePasswordEmail('');
setChangePasswordValue('');
} catch (error) {
console.error('Błąd podczas zmiany hasła:', error);
}
};
const addAbsence = async () => {
if (!selectedEmail || !absenceType || !startDate || !endDate) {
alert("Wszystkie pola muszą być wypełnione!");
@ -138,6 +209,26 @@ const PanelAdministratora = () => {
Raporty
</button>
</div>
<div className='px-5'>
<button
onClick={() => setSelectedOption('konta')}
className={`
${selectedOption === 'konta' ? 'text-white font-bold' : 'text-gray-200'}
hover:text-white hover:bg-indigo-600 hover:rounded-lg transition duration-300 ease-in-out
`}>
Konta
</button>
</div>
<div className='px-5'>
<button
onClick={() => setSelectedOption('hasła')}
className={`
${selectedOption === 'hasła' ? 'text-white font-bold' : 'text-gray-200'}
hover:text-white hover:bg-indigo-600 hover:rounded-lg transition duration-300 ease-in-out
`}>
Hasła
</button>
</div>
</div>
</div>
@ -297,6 +388,131 @@ const PanelAdministratora = () => {
</div>
)}
{selectedOption === 'konta' && (
<div className="flex justify-center items-start pt-10">
<div className="bg-white p-8 rounded-xl shadow-lg w-full max-w-md">
<div className="mb-6">
<h2 className="text-2xl font-semibold text-center mb-4">Dodaj konto</h2>
<div className="mb-4">
<label htmlFor="email" className="block text-lg font-medium text-gray-700 mb-2">
Email:
</label>
<input
type="email"
id="email"
value={userEmail}
onChange={(e) => setUserEmail(e.target.value)}
className={`w-full p-3 border ${
errors.email ? "border-red-500" : "border-gray-300"
} rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500`}
/>
{errors.email && <p className="text-red-500 text-sm">{errors.email}</p>}
</div>
<div className="mb-4">
<label htmlFor="password" className="block text-lg font-medium text-gray-700 mb-2">
Hasło:
</label>
<input
type="password"
id="password"
value={userPassword}
onChange={(e) => setUserPassword(e.target.value)}
className={`w-full p-3 border ${
errors.password ? "border-red-500" : "border-gray-300"
} rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500`}
/>
{errors.password && <p className="text-red-500 text-sm">{errors.password}</p>}
</div>
<div className="mb-4">
<label htmlFor="role" className="block text-lg font-medium text-gray-700 mb-2">
Rola:
</label>
<div className="flex items-center space-x-4">
<label className="flex items-center space-x-2">
<input
type="radio"
name="role"
value="user"
checked={userRole === "user"}
onChange={(e) => setUserRole(e.target.value)}
className="form-radio text-blue-500 focus:ring-blue-500"
/>
<span className="text-gray-700">User</span>
</label>
<label className="flex items-center space-x-2">
<input
type="radio"
name="role"
value="admin"
checked={userRole === "admin"}
onChange={(e) => setUserRole(e.target.value)}
className="form-radio text-blue-500 focus:ring-blue-500"
/>
<span className="text-gray-700">Admin</span>
</label>
</div>
{errors.role && <p className="text-red-500 text-sm">{errors.role}</p>}
</div>
<button
onClick={addUser}
className="w-full bg-blue-500 text-white py-3 px-4 rounded-lg mt-4 hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-300"
>
Dodaj konto
</button>
</div>
</div>
</div>
)}
{selectedOption === 'hasła' && (
<div className="flex justify-center items-start pt-10">
<div className="bg-white p-8 rounded-xl shadow-lg w-full max-w-md">
<div className="mb-6">
<h2 className="text-2xl font-semibold text-center mb-4">Zmień hasło</h2>
<div className="mb-4">
<label htmlFor="email" className="block text-lg font-medium text-gray-700 mb-2">Wybierz email:</label>
<select
id="email"
value={changePasswordEmail}
onChange={(e) => setChangePasswordEmail(e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="">Wybierz...</option>
{emails.map((email) => (
<option key={email} value={email}>{email}</option>
))}
</select>
</div>
<div className="mb-4">
<label htmlFor="changePasswordValue" className="block text-lg font-medium text-gray-700 mb-2">
Nowe hasło:
</label>
<input
type="password"
id="changePasswordValue"
value={changePasswordValue}
onChange={(e) => setChangePasswordValue(e.target.value)}
className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<button
onClick={changePassword}
className="w-full bg-blue-500 text-white py-3 px-4 rounded-lg mt-4 hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 transition duration-300"
>
Zmień hasło
</button>
</div>
</div>
</div>
)}
</div>
);
};

View File

@ -2,17 +2,15 @@ import React, { useState, useEffect } from 'react';
import axios from 'axios';
const WidokHarmonogramu = ({ workdays }) => {
const [currentDate, setCurrentDate] = useState(new Date());
const [displayDate, setDisplayDate] = useState(new Date());
const [daysInMonth, setDaysInMonth] = useState([]);
const [manualDateChange, setManualDateChange] = useState(false);
const [isWorking, setIsWorking] = useState(false);
const [selectedDay, setSelectedDay] = useState(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
generateDaysInMonth();
}, [displayDate, workdays]);
}, [workdays]);
const generateDaysInMonth = () => {
const firstDayOfMonth = new Date(displayDate.getFullYear(), displayDate.getMonth(), 1);