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

Replace default alerts with Toast #543

Merged
3 commits merged into from
Aug 4, 2022
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
21 changes: 16 additions & 5 deletions frontend/src/components/QuickAdd.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef } from "react";
import { IdName, Issue, IssueActivityPair } from "../model";
import { IdName, Issue, IssueActivityPair, ToastMsg } from "../model";
import { getApiEndpoint, useDebounce } from "../utils";
import plus from "../icons/plus.svg";
import x from "../icons/x.svg";
Expand All @@ -10,8 +10,12 @@ import * as ReactDOM from "react-dom";

export const QuickAdd = ({
addIssueActivity,
toastList,
onToastListUpdate,
}: {
addIssueActivity: (pair: IssueActivityPair) => void;
toastList: ToastMsg[];
onToastListUpdate: (newToast: ToastMsg) => void;
}) => {
const [activities, setActivities] = useState<IdName[]>([]);
const [issue, setIssue] = useState<Issue>(null);
Expand Down Expand Up @@ -141,17 +145,24 @@ export const QuickAdd = ({
}
})
.catch((error) => {
alert(error);
onToastListUpdate({
type: "warning",
timeout: 5000,
message: error.message,
});
});
if (logout) context.setUser(null);
return foundIssues;
};

const handleAdd = (e) => {
if (issue === null) {
alert(
"We couldn't add anything. Make sure to type a valid issue number and choose an activity."
);
onToastListUpdate({
type: "warning",
timeout: 5000,
message:
"We couldn't add anything. Make sure to type a valid issue number and choose an activity.",
});
} else {
const pair: IssueActivityPair = {
issue: issue,
Expand Down
45 changes: 41 additions & 4 deletions frontend/src/pages/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ export const Report = () => {
}
})
.catch((error) => {
alert(error);
setToastList([
...toastList,
{
type: "warning",
timeout: 5000,
message: error.message,
},
]);
const favs = [...favorites];
setFavorites(favs);
return false;
Expand Down Expand Up @@ -400,7 +407,14 @@ export const Report = () => {
}
})
.catch((error) => {
alert(error);
setToastList([
...toastList,
{
type: "warning",
timeout: 5000,
message: error.message,
},
]);
return false;
});
if (logout) context.setUser(null);
Expand Down Expand Up @@ -462,7 +476,14 @@ export const Report = () => {
});
}
if (recentIssue) {
alert("This issue/activity pair is already added");
setToastList([
...toastList,
{
type: "warning",
timeout: 5000,
message: "This issue/activity pair is already added.",
},
]);
return;
}
const newRecentIssues = [...filteredRecents, pair];
Expand Down Expand Up @@ -607,6 +628,18 @@ export const Report = () => {
return pairs;
};

// Forwards the option to update the toast list to child components
const handleToastListUpdate = (newToast: ToastMsg) => {
setToastList([
...toastList,
{
type: newToast.type,
timeout: newToast.timeout,
message: newToast.message,
},
]);
};

if (context.user === null) return <></>;

// Main content
Expand Down Expand Up @@ -812,7 +845,11 @@ export const Report = () => {
<div className="footer">
<section className="footer-container">
<div className="col-8">
<QuickAdd addIssueActivity={addIssueActivityHandler}></QuickAdd>
<QuickAdd
addIssueActivity={addIssueActivityHandler}
toastList={toastList}
onToastListUpdate={handleToastListUpdate}
></QuickAdd>
</div>
{toastList.length > 0 && (
<Toast onCloseToast={handleCloseToast} toastList={toastList} />
Expand Down