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

Remove unnecessary success toast #541

Merged
merged 5 commits into from
Aug 4, 2022
Merged
Changes from 2 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
66 changes: 35 additions & 31 deletions frontend/src/pages/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const Report = () => {
const [showUnsavedWarning, setShowUnsavedWarning] = useState(false);
const [showHidden, setShowHidden] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [isFavNameUpdated, setIsFavNameUpdated] = useState(false);
const context = React.useContext(AuthContext);
const urlparams = useParams();

Expand Down Expand Up @@ -157,7 +158,7 @@ export const Report = () => {
);
const issues = [...recentIssues];
if (!!priorityIssues) {
let nonPrioIssues = [];
let nonPrioIssues: IssueActivityPair[] = [];
issues.forEach((issue) => {
let match = priorityIssues.find(
(fav) =>
Expand Down Expand Up @@ -338,6 +339,7 @@ export const Report = () => {
let newFav = { ...existingFav, custom_name };
favs.splice(favs.indexOf(existingFav), 1, newFav);
setFavorites(favs);
setIsFavNameUpdated(true);
}
};

Expand Down Expand Up @@ -407,42 +409,34 @@ export const Report = () => {
return saved;
};

// Make sure that the custom issue name is saved in the database.
const handleCustomNamesSave = async () => {
const saved = await saveFavorites([...favorites, ...hidden]);
if (!saved) {
alert("Favourite rows could not be saved. Please try again later.");
return;
}
setToastList([
...toastList,
{
type: "success",
timeout: 3000,
message: "Custom names were saved!",
},
]);
};

// Check for ...
const handleSave = async () => {
setShowUnsavedWarning(false);
handleCustomNamesSave();
if (newTimeEntries.length === 0) {
if (newTimeEntries.length === 0 && !isFavNameUpdated) {
return;
}
toggleLoadingPage(true);
const unsavedEntries = [];
for await (let entry of newTimeEntries) {
const saved = await reportTime(entry);
if (!saved) {
unsavedEntries.push(entry);
let saveFailed = false;
if (newTimeEntries.length !== 0) {
const unsavedEntries = [];
for await (let entry of newTimeEntries) {
const saved = await reportTime(entry);
if (!saved) {
unsavedEntries.push(entry);
saveFailed = true;
}
}
await getAllEntries([...favorites, ...hidden, ...filteredRecents]);
setNewTimeEntries(unsavedEntries);
}
if (isFavNameUpdated) {
const namesSaved = await saveFavorites([...favorites, ...hidden]);
if (!namesSaved) {
saveFailed = true;
}
}
await getAllEntries([...favorites, ...hidden, ...filteredRecents]);
setNewTimeEntries(unsavedEntries);
toggleLoadingPage(false);
if (unsavedEntries.length === 0) {
if (!saveFailed) {
setToastList([
...toastList,
{
Expand All @@ -451,8 +445,16 @@ export const Report = () => {
message: "All changes saved!",
},
]);
} else if (unsavedEntries.length > 0) {
} else if (saveFailed) {
setShowUnsavedWarning(true);
setToastList([
...toastList,
{
type: "warning",
timeout: 3000,
message: "Not all changes could be saved. Please try again later.",
},
]);
}
};

Expand Down Expand Up @@ -526,7 +528,7 @@ export const Report = () => {
and, if there are none, for entries from the database for the respective cell.
*/
const findRowHours = (rowTopic: IssueActivityPair) => {
let rowHours = [];
let rowHours: number[] = [];
currentWeekArray.map((day) => {
let hours: number = null;
let entry: TimeEntry | FetchedTimeEntry = newTimeEntries?.find(
Expand Down Expand Up @@ -558,7 +560,7 @@ export const Report = () => {
If there is no entry in the database, id is 0.
*/
const findRowEntries = (rowTopic: IssueActivityPair, days: Date[]) => {
let entries = [];
let entries: FetchedTimeEntry[] = [];
days.map((day) => {
let entry = timeEntries?.find(
(entry) =>
Expand Down Expand Up @@ -722,6 +724,7 @@ export const Report = () => {
topic={recentIssue}
onCellUpdate={handleCellUpdate}
onToggleFav={handleToggleFav}
onFavNameUpdate={handleFavNameUpdate}
onToggleHide={toggleHide}
days={currentWeekArray}
rowHours={findRowHours(recentIssue)}
Expand Down Expand Up @@ -752,6 +755,7 @@ export const Report = () => {
topic={hiddenIssue}
onCellUpdate={handleCellUpdate}
onToggleFav={handleToggleFav}
onFavNameUpdate={handleFavNameUpdate}
onToggleHide={toggleHide}
days={currentWeekArray}
rowHours={findRowHours(hiddenIssue)}
Expand Down