Skip to content

Commit

Permalink
Issue #23 Resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
truffer11 committed Sep 30, 2024
1 parent e842db7 commit 8a687af
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 203 deletions.
49 changes: 30 additions & 19 deletions client-app/src/Components/DonorForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React, { useState, ChangeEvent, FormEvent } from 'react';
import axios from 'axios';
import '../css/DonorForm.css';
Expand Down Expand Up @@ -65,10 +64,6 @@ const DonorForm: React.FC = () => {
if (name === 'imageUpload' && files) {
const fileArray = Array.from(files);
const base64Images = await Promise.all(fileArray.map(file=>convertToBase64(file)));
if (files.length > 5) {
setErrorMessage('Please limit to 5 images only.');
return;
}
setFormData(prevState => ({
...prevState,
[name]: base64Images,
Expand Down Expand Up @@ -96,20 +91,36 @@ const DonorForm: React.FC = () => {
];

if (requiredFields.includes(name)) {
if (name === 'imageUpload') {
if (name === 'itemType') {
if (!value || value.length === 0) {
return "Please select an item type"
}
} else if (name === 'currentStatus') {
if (!value || value.length === 0) {
return "Please enter a status"
}
} else if (name === 'donorEmail') {
if (!value || value.length === 0) {
return 'Please upload at least one image.';
return "Please select the donor's email"
}
} else if (name === 'program') {
if (!value || value.length === 0) {
return "Please select a program"
}
} else if (name === 'imageUpload') {
if (!value || value.length === 0) {
return 'Please upload at least one image';
} else if (value.length > 5) {
return 'Please keep under 5 images'
}
} else if (name === 'dateDonated') {
if (!value || value.length === 0) {
return 'Please select a date'
}
} else if (typeof value === 'string' && !value.trim()) {
return `${name.replace(/([A-Z])/g, ' $1')} is required`;
}
}

if (name === 'donorEmail') {
if (!value) {
return 'Donor email is required.'
}
}
return '';
};

Expand All @@ -134,7 +145,7 @@ const DonorForm: React.FC = () => {
formData,
);
if (response.status === 201) {
setSuccessMessage('Donor added successfully!');
setSuccessMessage('Item added successfully!');
console.log('Item Type: ' + formData.itemType);
console.log('Current Status ' + formData.currentStatus);
console.log('Donor Email: ' + formData.donorEmail);
Expand All @@ -150,12 +161,12 @@ const DonorForm: React.FC = () => {
dateDonated: '',
});
} else {
setErrorMessage('Donor not added');
setErrorMessage('Item not added');
}
} catch (error: unknown) {
const message =
(error as any).response?.data?.message ||
'Error adding donor';
'Error adding item';
setErrorMessage(message);
}
} else {
Expand Down Expand Up @@ -238,7 +249,7 @@ const DonorForm: React.FC = () => {
return (
<div className="donor-form outer-container mx-auto p-10">
<h1 className="text-2xl font-bold heading-centered">
Add Donor Details
New Donated Item
</h1>
{errorMessage && <p className="error-message">{errorMessage}</p>}
{successMessage && (
Expand All @@ -255,7 +266,7 @@ const DonorForm: React.FC = () => {

<div className="form-field full-width button-container">
<button type="submit" className="submit-button">
Add Donor
Add Item
</button>
<button
type="button"
Expand All @@ -272,7 +283,7 @@ const DonorForm: React.FC = () => {

export default DonorForm;


// NOTE: I will be moving my code to NewDonationForm.tsx as soon as I can get a tab for it. This is so I could see what I was working on

/*import React, { useState, ChangeEvent, FormEvent } from 'react';
import axios from 'axios';
Expand Down
184 changes: 0 additions & 184 deletions client-app/src/Components/NewDonationForm.tsx

This file was deleted.

Loading

0 comments on commit 8a687af

Please sign in to comment.