Skip to content

Commit

Permalink
Simpler list loading state management
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer committed Jan 31, 2023
1 parent 24b6386 commit d4e5dc3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useDeleteSlo } from '../../../hooks/slo/use_delete_slo';
export interface SloDeleteConfirmationModalProps {
slo: SLOWithSummaryResponse;
onCancel: () => void;
onDeleting: (isDeleting: boolean) => void;
onDeleting: () => void;
onDeleted: () => void;
}

Expand All @@ -34,18 +34,16 @@ export function SloDeleteConfirmationModal({
const { deleteSlo, success, loading, error } = useDeleteSlo();

if (loading) {
onDeleting(true);
onDeleting();
}

if (success) {
toasts.addSuccess(getDeleteSuccesfulMessage(name));
onDeleting(false);
onDeleted();
}

if (error) {
toasts.addDanger(getDeleteFailMessage(name));
onDeleting(false);
}

const handleConfirm = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export function SloList() {
const [sort, setSort] = useState<SortType>('name');
const [indicatorTypeFilter, setIndicatorTypeFilter] = useState<FilterType[]>([]);

const [isCloning, setIsCloning] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [isCloningOrDeleting, setIsCloningOrDeleting] = useState(false);
const [shouldReload, setShouldReload] = useState(false);

const {
Expand All @@ -43,17 +42,15 @@ export function SloList() {
useEffect(() => {
if (shouldReload) {
setShouldReload(false);
setIsCloning(false);
setIsDeleting(false);
}
}, [shouldReload]);

const handleCloning = (cloning: boolean) => {
setIsCloning(cloning);
};
if (!isLoadingSloList) {
setIsCloningOrDeleting(false);
}
}, [isLoadingSloList, shouldReload]);

const handleDeleting = (deleting: boolean) => {
setIsDeleting(deleting);
const handleCloningOrDeleting = () => {
setIsCloningOrDeleting(true);
};

const handleClonedOrDeleted = () => {
Expand Down Expand Up @@ -85,7 +82,7 @@ export function SloList() {
<EuiFlexGroup direction="column" gutterSize="m" data-test-subj="sloList">
<EuiFlexItem grow>
<SloListSearchFilterSortBar
loading={isLoadingSloList || isCloning || isDeleting}
loading={isLoadingSloList || isCloningOrDeleting}
onChangeQuery={handleChangeQuery}
onChangeSort={handleChangeSort}
onChangeIndicatorTypeFilter={handleChangeIndicatorTypeFilter}
Expand All @@ -98,8 +95,8 @@ export function SloList() {
loading={isLoadingSloList}
error={error}
onCloned={handleClonedOrDeleted}
onCloning={handleCloning}
onDeleting={handleDeleting}
onCloning={handleCloningOrDeleting}
onDeleting={handleCloningOrDeleting}
onDeleted={handleClonedOrDeleted}
/>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export interface SloListItemProps {
historicalSummary?: HistoricalSummaryResponse[];
historicalSummaryLoading: boolean;
onCloned: () => void;
onCloning: (isCloning: boolean) => void;
onCloning: () => void;
onDeleted: () => void;
onDeleting: (isDeleting: boolean) => void;
onDeleting: () => void;
}

export function SloListItem({
Expand Down Expand Up @@ -94,7 +94,9 @@ export function SloListItem({
};

useEffect(() => {
onCloning(isCloning);
if (isCloning) {
onCloning();
}

if (isCloned) {
onCloned();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface Props {
loading: boolean;
error: boolean;
onCloned: () => void;
onCloning: (isCloning: boolean) => void;
onCloning: () => void;
onDeleted: () => void;
onDeleting: (isDeleting: boolean) => void;
onDeleting: () => void;
}

export function SloListItems({
Expand Down

0 comments on commit d4e5dc3

Please sign in to comment.