Skip to content

Commit 72d4be6

Browse files
committed
apply PR feedback
1 parent 40cfa40 commit 72d4be6

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

frontend/javascripts/dashboard/dataset/dataset_settings_view.tsx

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ import {
1313
import { Alert, Button, Card, Form, Spin, Tabs, Tooltip } from "antd";
1414
import dayjs from "dayjs";
1515
import features from "features";
16-
import type { UnregisterCallback } from "history";
1716
import { handleGenericError } from "libs/error_handling";
1817
import { useWkSelector } from "libs/react_hooks";
1918
import Toast from "libs/toast";
2019
import { jsonStringify } from "libs/utils";
2120
import _ from "lodash";
2221
import messages from "messages";
23-
import { useCallback, useEffect, useRef, useState } from "react";
22+
import { useCallback, useEffect, useState } from "react";
2423
import { Link } from "react-router-dom";
2524
import type { APIDataSource, APIDataset, MutableAPIDataset } from "types/api_types";
2625
import { enforceValidatedDatasetViewConfiguration } from "types/schemas/dataset_view_configuration_defaults";
@@ -86,23 +85,6 @@ const DatasetSettingsView: React.FC<DatasetSettingsViewProps> = ({
8685
APIDataSource | null | undefined
8786
>(null);
8887

89-
const unblockRef = useRef<UnregisterCallback | null>(null);
90-
const blockTimeoutIdRef = useRef<number | null>(null);
91-
92-
const unblockHistory = useCallback(() => {
93-
window.onbeforeunload = null;
94-
95-
if (blockTimeoutIdRef.current != null) {
96-
clearTimeout(blockTimeoutIdRef.current);
97-
blockTimeoutIdRef.current = null;
98-
}
99-
100-
if (unblockRef.current != null) {
101-
unblockRef.current();
102-
unblockRef.current = null;
103-
}
104-
}, []);
105-
10688
const fetchData = useCallback(async (): Promise<void> => {
10789
try {
10890
setIsLoading(true);
@@ -384,11 +366,6 @@ const DatasetSettingsView: React.FC<DatasetSettingsViewProps> = ({
384366
setHasUnsavedChanges(true);
385367
}, []);
386368

387-
const handleCancel = useCallback(() => {
388-
unblockHistory();
389-
onCancel();
390-
}, [unblockHistory, onCancel]);
391-
392369
const handleDataSourceEditModeChange = useCallback(
393370
(activeEditMode: "simple" | "advanced") => {
394371
syncDataSourceFields(form, activeEditMode);
@@ -611,7 +588,7 @@ const DatasetSettingsView: React.FC<DatasetSettingsViewProps> = ({
611588
{confirmString}
612589
</Button>
613590
{Unicode.NonBreakingSpace}
614-
<Button onClick={handleCancel}>Cancel</Button>
591+
<Button onClick={onCancel}>Cancel</Button>
615592
</FormItem>
616593
</Spin>
617594
</Card>

frontend/javascripts/dashboard/dataset/team_selection_component.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getEditableTeams, getTeams } from "admin/rest_api";
22
import { Select } from "antd";
33
import { useEffectOnlyOnce } from "libs/react_hooks";
4+
import Toast from "libs/toast";
45
import _ from "lodash";
56
import { useCallback, useEffect, useState } from "react";
67
import type { APITeam } from "types/api_types";
@@ -43,12 +44,13 @@ function TeamSelectionComponent({
4344
try {
4445
const possibleTeams = allowNonEditableTeams ? await getTeams() : await getEditableTeams();
4546
setPossibleTeams(possibleTeams);
46-
setIsFetchingData(false);
4747
if (afterFetchedTeams) {
4848
afterFetchedTeams(possibleTeams);
4949
}
5050
} catch (_exception) {
51-
console.error("Could not load teams.");
51+
Toast.error("Could not load teams.");
52+
} finally {
53+
setIsFetchingData(false);
5254
}
5355
}
5456

@@ -61,11 +63,11 @@ function TeamSelectionComponent({
6163
? selectedTeamIdsOrId
6264
: [selectedTeamIdsOrId];
6365
const allTeams = getAllTeams();
64-
const selected = _.compact(selectedTeamIds.map((id) => allTeams.find((t) => t.id === id)));
66+
const selectedTeams = _.compact(selectedTeamIds.map((id) => allTeams.find((t) => t.id === id)));
6567
if (onChange) {
66-
onChange(Array.isArray(selectedTeamIdsOrId) ? selected : selected[0]);
68+
onChange(Array.isArray(selectedTeamIdsOrId) ? selectedTeams : selectedTeams[0]);
6769
}
68-
setSelectedTeams(selected);
70+
setSelectedTeams(selectedTeams);
6971
};
7072

7173
return (
@@ -78,7 +80,7 @@ function TeamSelectionComponent({
7880
onChange={onSelectTeams}
7981
value={selectedTeams.map((t) => t.id)}
8082
filterOption
81-
disabled={disabled ? disabled : false}
83+
disabled={disabled ?? false}
8284
loading={isFetchingData}
8385
>
8486
{getAllTeams().map((team) => (

frontend/javascripts/dashboard/dataset/useBeforeUnload_hook.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ const useBeforeUnload = (hasUnsavedChanges: boolean, message: string) => {
2828
newLocation: HistoryLocation<unknown>,
2929
action: HistoryAction,
3030
): string | false | void => {
31+
// Only show the prompt if this is a proper beforeUnload event from the browser
32+
// or the pathname changed
33+
// This check has to be done because history.block triggers this function even if only the url hash changed
3134
if (action === undefined || newLocation.pathname !== window.location.pathname) {
3235
if (hasUnsavedChanges) {
33-
window.onbeforeunload = null;
36+
window.onbeforeunload = null; // clear the event handler otherwise it would be called twice. Once from history.block once from the beforeunload event
3437
blockTimeoutIdRef.current = window.setTimeout(() => {
38+
// restore the event handler in case a user chose to stay on the page
3539
// @ts-ignore
3640
window.onbeforeunload = beforeUnload;
3741
}, 500);

0 commit comments

Comments
 (0)