Commit 8118fa3
Add API toast service for ARO and MCC (#599)
# Purpose
Closes #570.
# New Changes
- A standardized notification service which responds based on the
success or failure of an API call. The service is very flexible and can
be called in a variety of situations. A toast will emerge from the
top-left, and will last for five seconds. If you hover over it, the
toast will not dissapear.
- Uses `react-toastify`, so be sure to install that.
- In ARO's main.tsx, I added two buttons that you can play around with
to see how the toasts appear. Please remove them for future updates.
- Added .DS_Store to .gitignore.
# How to Use the Service
## Usage
#### Import the Service
```TypeScript
import toastService from '../services/Toast.service.ts';
```
#### Call the Service
The service should be used within logic that handles asynchronous
operations, such as in a try...catch...finally block after an API call.
On Success, call ```toastService.success()``` with a confirmation
message.
On Error, call ```toastService.error()``` with an informative error
message.
### Example Implementation for a Form
Place a single <ToastContainer /> at the application root and trigger
toasts from the API service layer.
```TypeScript
const handleFormSubmit = async (formData) => {
try {
await apiClient.saveData(formData);
toastService.success("Data saved successfully!");
} catch (err: any) {
const message = err.response?.data?.message || "An unexpected error occurred.";
toastService.error(`Failed to save: ${message}`);
}
};
```
## Example in App.tsx
Try running the following code added to App.tsx. The dots ```...```
represents the rest of the code.
```TypeScript
...
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import './styles/toasts.css';
import toastService from './services/Toast.service.ts'
...
function App() {
...
const showSuccess = () => {
toastService.success("This is a success message for testing!");
};
const showError = () => {
toastService.error("This is an error message for testing.");
};
return (
...
{/* --- TEST BUTTONS --- */}
<div className="w-1/4 flex flex-row mt-[10%] ms-[34.5%]">
<h3>Toast Test Controls</h3>
<button onClick={showSuccess} className="bg-lime-400 w-1/2 cursor-pointer">Success</button>
<button onClick={showError} className="bg-red-400 btn w-1/2 cursor-pointer">Error</button>
</div>
<ToastContainer />
{/* --- END --- */}
...
)
}
```
Clicking on the buttons should display the error messages respectively.
# Testing
Explain tests that you ran to verify code functionality.
- [x] I have tested this PR by running the ARO website by adding buttons
which call the toasts, and they worked as expected.
- [x] I have tested this PR by running the MCC website by adding buttons
which call the toasts, and they worked as expected.
- [x] I have included a video of the tests performed below.
https://github.com/user-attachments/assets/9fc79a4e-6791-4258-ae7b-3085ff06bf75
# Outstanding Changes
Because this is meant to be a standardized service, I tried making the
service with generality in mind. You can use this for any API call, from
backend to frontend responses (although it was designed for frontend).
---------
Co-authored-by: Qinkai Li <q95li@uwaterloo.ca>1 parent 2056454 commit 8118fa3
File tree
8 files changed
+157
-4
lines changed- gs/frontend
- aro
- src
- services
- mcc
- src
- services
8 files changed
+157
-4
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | | - | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
0 commit comments