forked from skupperproject/openshift-site-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFooter.tsx
More file actions
166 lines (138 loc) · 4.39 KB
/
Footer.tsx
File metadata and controls
166 lines (138 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { FC, useCallback, useEffect } from 'react';
import { RESTApi } from '@API/REST.api';
import { createAccessTokenRequest } from '@core/utils/createCRD';
import { AccessGrantCrdResponse } from '@interfaces/CRD_AccessGrant';
import { AccessTokenCrdParams } from '@interfaces/CRD_AccessToken';
import { HTTPError } from '@interfaces/REST.interfaces';
import {
Button,
WizardFooterWrapper,
Alert,
PageSectionVariants,
PageSection,
useWizardContext
} from '@patternfly/react-core';
import { useMutation } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { parse } from 'yaml';
import { useLinkForm } from './hooks/useLinkForm';
import { I18nNamespace } from '../../../../config/config';
const ButtonName: string[] = ['Next', 'Create', 'Done'];
interface FooterProps {
onSubmit: () => void;
onCancel: () => void;
}
export const Footer: FC<FooterProps> = function ({ onCancel, onSubmit }) {
const { t } = useTranslation(I18nNamespace);
const { activeStep, goToNextStep, goToPrevStep } = useWizardContext();
const {
state: { name, cost, file: fileContent },
isLoading,
validated,
setIsLoading,
setValidated,
dispatch
} = useLinkForm();
const mutationCreate = useMutation({
mutationFn: (data: AccessTokenCrdParams) => RESTApi.createAccessToken(data),
onError: (data: HTTPError) => {
dispatch({ type: 'SET_NAME', payload: '' });
dispatch({ type: 'SET_COST', payload: '' });
dispatch({ type: 'SET_FILE_NAME', payload: '' });
dispatch({ type: 'SET_FILE_CONTENT', payload: '' });
setValidated(data.descriptionMessage);
setIsLoading(false);
},
onSuccess: () => {
setValidated(undefined);
setIsLoading(true);
goToNextStep();
}
});
const handleSubmit = useCallback(() => {
if (!fileContent || !name) {
setValidated(t('Fill out all required fields before continuing'));
return;
}
try {
const JsonFile = parse(fileContent) as AccessGrantCrdResponse;
const { status } = JsonFile;
if (!status) {
setValidated(t('Invalid Grant format'));
return;
}
const data: AccessTokenCrdParams = createAccessTokenRequest({
metadata: {
name
},
spec: {
linkCost: Number(cost),
ca: status.ca,
code: status.code,
url: status.url
}
});
mutationCreate.mutate(data);
} catch {
setValidated(t('Invalid Grant format'));
}
}, [cost, fileContent, mutationCreate, name, setValidated, t]);
const handlePreviousStep = useCallback(() => {
setValidated(undefined);
goToPrevStep();
}, [goToPrevStep, setValidated]);
const handleNextStep = useCallback(() => {
setValidated(undefined);
if (activeStep?.index === 2) {
handleSubmit();
return;
}
if (activeStep?.index === 3) {
onSubmit();
}
goToNextStep();
}, [setValidated, activeStep?.index, goToNextStep, handleSubmit, onSubmit]);
const handleKeyPress = useCallback(
(event: KeyboardEvent) => {
if (event.key === 'Enter') {
event.preventDefault();
handleNextStep();
}
},
[handleNextStep]
);
useEffect(() => {
window.addEventListener('keydown', handleKeyPress);
return () => {
window.removeEventListener('keydown', handleKeyPress); // Cleanup listener on unmount
};
}, [handleKeyPress]);
return (
<>
{validated && activeStep?.index === 2 && (
<PageSection variant={PageSectionVariants.light}>
<Alert variant="danger" title={t('An error occurred')} aria-live="polite" isInline>
{validated}
</Alert>
</PageSection>
)}
<WizardFooterWrapper>
<Button
variant="secondary"
onClick={handlePreviousStep}
isDisabled={activeStep?.index === 1 || activeStep?.index === 3 || isLoading}
>
{t('Back')}
</Button>
<Button onClick={handleNextStep} isDisabled={isLoading || (activeStep?.index === 3 && !!validated)}>
{t(ButtonName[activeStep?.index - 1])}
</Button>
{!(activeStep.index === 3 && !isLoading && !validated) && (
<Button variant="link" onClick={onCancel}>
{activeStep?.index === 1 || activeStep?.index === 2 ? t('Cancel') : t('Dismiss')}
</Button>
)}
</WizardFooterWrapper>
</>
);
};