Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pratyush error handling #3043

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
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
137 changes: 137 additions & 0 deletions src/components/EventRegistration/EventRegistration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import React, { useState, useEffect } from 'react';

Check failure on line 1 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

'React' is defined but never used

Check failure on line 1 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'React' is defined but never used
import { ApiEndpoint, ENDPOINTS } from '../../utils/URL';

Check failure on line 2 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

'ENDPOINTS' is defined but never used

Check failure on line 2 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'ENDPOINTS' is defined but never used

const EventRegistration = () => {

Check failure on line 4 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Function component is not a function declaration

Check failure on line 4 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Function component is not a function declaration
const [formValues, setFormValues] = useState({
eventName: '',
});
const [errors, setErrors] = useState({});
const [message, setMessage] = useState('');
const [events, setEvents] = useState([]);

Check failure on line 10 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

'events' is assigned a value but never used

Check failure on line 10 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'events' is assigned a value but never used

useEffect(() => {
fetchEvents();

Check failure on line 13 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

'fetchEvents' was used before it was defined

Check failure on line 13 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'fetchEvents' was used before it was defined
}, []);

const fetchEvents = async () => {
const token = localStorage.getItem('token'); // Replace with actual key
if (!token) {
console.error('No token found. Please log in.');

Check failure on line 19 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 19 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
return;
}
const APIEndpoint =
process.env.REACT_APP_APIENDPOINT || 'https://hgn-rest-beta.azurewebsites.net/api';
console.log('api endpoint: ', ApiEndpoint);

Check failure on line 24 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 24 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
try {
const response = await fetch(`${APIEndpoint}/EventRegistration`, {
headers: {
Authorization: `Bearer ${token}`, // Add Authorization header
},
});

if (response.ok) {
const data = await response.json();
setEvents(data);
} else {
console.error('Failed to fetch events');

Check failure on line 36 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 36 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
}
} catch (error) {
console.error('Error fetching events:', error);

Check failure on line 39 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 39 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
}
};

const handleChange = e => {
const { name, value } = e.target;
setFormValues(prev => ({
...prev,
[name]: value,
}));
setErrors(prev => ({
...prev,
[name]: '',
}));
};

const handleSubmit = async e => {
e.preventDefault();
const newErrors = {};
if (!formValues.eventName.trim()) {
newErrors.eventName = 'Event Name is required.';
}

if (Object.keys(newErrors).length > 0) {
setErrors(newErrors);
} else {
const APIEndpoint =
process.env.REACT_APP_APIENDPOINT || 'https://hgn-rest-beta.azurewebsites.net/api';
console.log('api endpoint: ', ApiEndpoint);

Check failure on line 67 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 67 in src/components/EventRegistration/EventRegistration.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected console statement
try {
const response = await fetch(`${APIEndpoint}/EventRegistration`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`, // Add Authorization header
},
body: JSON.stringify(formValues), // Convert formValues to JSON
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();
console.log('Event registered successfully:', data);
setMessage('Event registered successfully!');
setFormValues({ eventName: '' });
} catch (error) {
console.error('Error registering event:', error);
}
}
};

const handleCancel = () => {
setFormValues({ eventName: '' });
setErrors({});
setMessage('');
};

return (
<div className="max-w-md mx-auto mt-10 p-6 bg-white rounded-lg shadow-xl">
<h2 className="text-2xl font-bold mb-6 text-center">Event Registration</h2>
<form onSubmit={handleSubmit}>
<div className="mb-4">
<label htmlFor="eventName" className="block text-gray-700 text-sm font-bold mb-2">
Event Name: <span className="text-red-500">*</span>
</label>
<input
type="text"
id="eventName"
name="eventName"
value={formValues.eventName}
onChange={handleChange}
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
/>
{errors.eventName && <p className="text-red-500 text-xs italic">{errors.eventName}</p>}
</div>
<div className="flex items-center justify-between">
<button
type="submit"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
>
Submit
</button>
<button
type="button"
onClick={handleCancel}
className="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
>
Cancel
</button>
</div>
</form>
{message && <div className="mt-4 p-2 bg-green-100 text-green-700 rounded">{message}</div>}
</div>
);
};

export default EventRegistration;
Loading
Loading