Skip to content

Commit eead511

Browse files
committed
add useRequiredActionAcknowledgements
1 parent fb45a9f commit eead511

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

packages/wizard/src/demo/RequiredActions.tsx

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React from 'react';
22

33
import { Checkbox } from '@leafygreen-ui/checkbox';
44
import { css } from '@leafygreen-ui/emotion';
55
import { spacing } from '@leafygreen-ui/tokens';
66
import { Body, Description, H3, Link } from '@leafygreen-ui/typography';
77

8-
import { useWizardStepContext } from '../';
9-
8+
import { useRequiredActionAcknowledgements } from './hooks/useRequiredActionAcknowledgements';
109
import {
1110
ApplicationsCard,
1211
ClustersCard,
@@ -40,17 +39,8 @@ const sectionDividerStyles = css`
4039
`;
4140

4241
export const RequiredActions = () => {
43-
const { setAcknowledged } = useWizardStepContext();
44-
45-
const [checkbox1, setCheckbox1] = useState(false);
46-
const [checkbox2, setCheckbox2] = useState(false);
47-
const [checkbox3, setCheckbox3] = useState(false);
48-
49-
// TODO: reducer. Update the wizard step's acknowledged state when all checkboxes are checked
50-
useEffect(() => {
51-
const allChecked = checkbox1 && checkbox2 && checkbox3;
52-
setAcknowledged(allChecked);
53-
}, [checkbox1, checkbox2, checkbox3, setAcknowledged]);
42+
const { acknowledgementsState, setAcknowledgementState } =
43+
useRequiredActionAcknowledgements();
5444

5545
return (
5646
<>
@@ -81,24 +71,24 @@ export const RequiredActions = () => {
8171
label="Permanently deletes all project data, including clusters, configurations, services, app services and triggers, and data across Atlas, which cannot be recovered."
8272
description="All project data including clusters, configurations, services, backup snapshots, app data, search indexes, federated database access, and Stream Processors will be permanently deleted. Deleted data cannot be recovered."
8373
bold
84-
checked={checkbox1}
85-
onChange={e => setCheckbox1(e.target.checked)}
74+
checked={acknowledgementsState[0]}
75+
onChange={e => setAcknowledgementState(0, e.target.checked)}
8676
/>
8777

8878
<Checkbox
8979
label="Overrides Termination Protection, deletes archives, backup snapshots, Stream Processors, and Connections, stops all data processing."
9080
description="Termination Protection will be disabled for all clusters. While archive data in object storage will remain recoverable for up to 5 days, online archives and backup snapshots will be deleted immediately. Stream Processors and Connections will be permanently deleted. Data processing will stop immediately."
9181
bold
92-
checked={checkbox2}
93-
onChange={e => setCheckbox2(e.target.checked)}
82+
checked={acknowledgementsState[1]}
83+
onChange={e => setAcknowledgementState(1, e.target.checked)}
9484
/>
9585

9686
<Checkbox
9787
label="Revokes access to federated databases, disables communication with Atlas clusters, and may disrupt application functionality."
9888
description="Access to federated database instances for dependent applications or services will be revoked, and communication between my application and the Atlas cluster or services will be disabled, which may cause functionality disruptions. I confirm that I have taken necessary measures to ensure uninterrupted access and mitigate impact."
9989
bold
100-
checked={checkbox3}
101-
onChange={e => setCheckbox3(e.target.checked)}
90+
checked={acknowledgementsState[2]}
91+
onChange={e => setAcknowledgementState(2, e.target.checked)}
10292
/>
10393
</div>
10494
</>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Reducer, useReducer } from 'react';
2+
3+
import { useWizardStepContext } from '../../WizardStep';
4+
5+
export type RequiredActionsAcknowledgementsState = [boolean, boolean, boolean];
6+
7+
export interface RequiredActionsAcknowledgementsAction {
8+
index: 0 | 1 | 2;
9+
value: boolean;
10+
}
11+
12+
export const useRequiredActionAcknowledgements = () => {
13+
const { isAcknowledged, setAcknowledged } = useWizardStepContext();
14+
15+
const [acknowledgementsState, dispatch] = useReducer<
16+
Reducer<
17+
RequiredActionsAcknowledgementsState,
18+
RequiredActionsAcknowledgementsAction
19+
>
20+
>(
21+
(state, action) => {
22+
const newState: RequiredActionsAcknowledgementsState = [...state];
23+
newState[action.index] = action.value;
24+
25+
if (newState.every(Boolean)) {
26+
setAcknowledged(true);
27+
} else {
28+
setAcknowledged(false);
29+
}
30+
31+
return newState;
32+
},
33+
[false, false, false],
34+
);
35+
36+
const setAcknowledgementState = (index: 0 | 1 | 2, value: boolean) =>
37+
dispatch({ index, value });
38+
39+
return {
40+
acknowledgementsState,
41+
setAcknowledgementState,
42+
isAcknowledged,
43+
};
44+
};

0 commit comments

Comments
 (0)