Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion web_app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
"@types/node": "^22.13.5",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/swagger-ui-react": "^4.18.3",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/ui": "^4.0.14",
"autoprefixer": "^10.4.20",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"jsdom": "^27.2.0",
"postcss": "^8.5.1",
"tailwindcss": "^3.4.17",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
"vite": "^7.2.4",
"vitest": "^4.0.14"
}
}
62 changes: 52 additions & 10 deletions web_app/src/Librarian/LibrarianAddBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ const LibrarianAddBook: React.FC = () => {
if (!authorOptions.includes(author)) {
try {
const token = localStorage.getItem('access_token');
if (!token) {
return;
}
if (!token) return;

const response = await fetch(`${API_BASE_URL}/api/authors`, {
method: 'POST',
Expand All @@ -113,16 +111,26 @@ const LibrarianAddBook: React.FC = () => {
});

if (!response.ok) {
if (response.status === 409) {
throw new Error("Ten autor już istnieje.");
}

const errorText = await response.text();
throw new Error(`Błąd ${response.status}: ${errorText}`);
const finalMessage = errorText || response.statusText;
throw new Error(`Błąd ${response.status}: ${finalMessage}`);
}

await fetchAuthors('');
await fetchPublishers('');
toast.success(`Dodano nowego autora: ${author}`);

} catch (err) {
console.error("Error adding author:", err);
setError('Wystąpił błąd podczas dodawania autora.');
const msg = (err as Error).message || "Błąd dodawania autora";
setError(msg);
toast.error(msg);

setAuthors(prev => prev.filter(a => a !== author));
}
}
}
Expand Down Expand Up @@ -166,14 +174,26 @@ const LibrarianAddBook: React.FC = () => {
body: JSON.stringify({ name: publisherName }),
});

if (!response.ok) throw new Error(await response.text());
if (!response.ok) {
if (response.status === 409) {
throw new Error("To wydawnictwo już istnieje.");
}

const errorText = await response.text();
const finalMessage = errorText || response.statusText;
throw new Error(`Błąd ${response.status}: ${finalMessage}`);
}

await fetchPublishers('');
toast.success(`Dodano nowe wydawnictwo: ${publisherName}`);
} catch (err) {
console.error("Error adding publisher:", err);
const msg = (err as Error).message || "Błąd dodawania wydawnictwa";

toast.error(msg);
setPublisher('');
}
}
//setPublisherInput('');
setShowPublisherDropdown(false);
};

Expand All @@ -187,7 +207,7 @@ const LibrarianAddBook: React.FC = () => {
};

const handleAddBook = async () => {
if (!title.trim() || !categoryName.trim() || authors.length === 0 || !publisher.trim() || !isbn.trim() || !language.trim() || releaseYear === null) {
if (!title.trim() || !categoryName.trim() || authors.length === 0 || !publisher.trim() || !isbn.trim() || !language.trim() || releaseYear === '') {
setError('Wszystkie pola są wymagane.');
return;
}
Expand Down Expand Up @@ -228,12 +248,34 @@ const LibrarianAddBook: React.FC = () => {

if (!response.ok) {
const errorText = await response.text();
throw new Error(`Błąd ${response.status}: ${errorText}`);

if (response.status === 409) {
throw new Error("Książka o podanym numerze ISBN już istnieje.");
}

if (response.status === 400) {
throw new Error(errorText || "Nieprawidłowe dane formularza.");
}

if (response.status === 401) {
throw new Error("Sesja wygasła. Proszę zalogować się ponownie.");
}

if (response.status === 413) {
throw new Error("Wybrane zdjęcie jest zbyt duże.");
}

// Code 500 and other unknown
const finalMessage = errorText || response.statusText || 'Unknown Server Error';
throw new Error(`Błąd ${response.status}: ${finalMessage}`);
}

toast.success("Książka została dodana!");
navigate('/librarian-dashboard');
} catch (error) {
setError((error as Error).message || 'Wystąpił błąd podczas dodawania książki.');
const msg = (error as Error).message || 'Wystąpił błąd.';
setError(msg);
toast.error(msg);
} finally {
setLoading(false);
}
Expand Down
10 changes: 10 additions & 0 deletions web_app/src/LibraryAdmin/LibraryAdminAddLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const LibraryAdminAddLibrary: React.FC = () => {
emailAddress: '',
});

const [errorMessage, setErrorMessage] = useState<string>('');

const handleLogout = () => {
localStorage.removeItem('access_token');
localStorage.removeItem('role');
Expand All @@ -38,6 +40,7 @@ const LibraryAdminAddLibrary: React.FC = () => {

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setErrorMessage('');

const requestBody = {
street: formData.addressLine,
Expand All @@ -64,9 +67,11 @@ const LibraryAdminAddLibrary: React.FC = () => {
navigate('/processing-info');
} else {
console.error('Error submitting request', response.statusText);
setErrorMessage('Wystąpił błąd podczas wysyłania formularza.');
}
} catch (error) {
console.error('Error:', error);
setErrorMessage('Nie udało się połączyć z serwerem. Sprawdź swoje połączenie internetowe.');
}
};

Expand Down Expand Up @@ -129,6 +134,11 @@ const LibraryAdminAddLibrary: React.FC = () => {
Złóż podanie
</button>
</div>
{errorMessage && (
<p className="mt-6 text-left text-red-600 font-semibold">
{errorMessage}
</p>
)}
</form>
</section>
</main>
Expand Down
Loading