Skip to content

Commit

Permalink
[Redesign add page] Add form status callout message (#5634)
Browse files Browse the repository at this point in the history
* Add form status manager and unit tests

* Add empty and invalid fields messages

* Hide commands code block when exists warning messages

* Fix fields names in warning messages

* Updated CHANGELOG
  • Loading branch information
Machi3mfl authored Jul 7, 2023
1 parent bade293 commit d0b89f4
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ All notable changes to the Wazuh app project will be documented in this file.
- Added new CLI to generate API data from specification file [#5519](https://github.com/wazuh/wazuh-kibana-app/pull/5519)
- Added specific RBAC permissions to Security section [#5551](https://github.com/wazuh/wazuh-kibana-app/pull/5551)
- Added development so that the images of the new agent deployment page also have dark mode. [#5620](https://github.com/wazuh/wazuh-kibana-app/pull/5620)
- Added register agent form status callout message [#5634](https://github.com/wazuh/wazuh-kibana-app/pull/5634)


### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
getOptionalParameterStepStatus,
showCommandsSections,
getPasswordStepStatus,
getIncompleteSteps,
getInvalidFields,
tFormFieldsLabel,
tFormStepsLabel,
} from '../../services/register-agent-steps-status-services';
import { webDocumentationLink } from '../../../../../common/services/web_documentation';

Expand Down Expand Up @@ -61,9 +65,17 @@ export const Steps = ({
protocol: connection.isUDP ? 'UDP' : '',
},
} as IParseRegisterFormValues;
const [missingStepsName, setMissingStepsName] = useState<tFormStepsLabel[]>(
[],
);
const [invalidFieldsName, setInvalidFieldsName] = useState<
tFormFieldsLabel[]
>([]);
const [registerAgentFormValues, setRegisterAgentFormValues] =
useState<IParseRegisterFormValues>(initialParsedFormValues);

const FORM_MESSAGE_CONJUNTION = ' and ';

useEffect(() => {
// get form values and parse them divided in OS and optional params
const registerAgentFormValuesParsed = parseRegisterAgentFormValues(
Expand All @@ -78,6 +90,8 @@ export const Steps = ({
setStartCommandStepStatus(
getAgentCommandsStepStatus(form.fields, startCommandWasCopied),
);
setMissingStepsName(getIncompleteSteps(form.fields) || []);
setInvalidFieldsName(getInvalidFields(form.fields) || []);
}, [form.fields]);

const { installCommand, startCommand, selectOS, setOptionalParams } =
Expand Down Expand Up @@ -142,7 +156,8 @@ export const Steps = ({
color='warning'
title={
<span>
The Wazuh password is required but wasn't defined. Please check our{' '}
The Wazuh password is required but wasn't defined. Please
check our{' '}
<EuiLink
target='_blank'
href={webDocumentationLink(
Expand Down Expand Up @@ -177,25 +192,67 @@ export const Steps = ({
</span>
),
children: (
<CommandOutput
commandText={installCommand}
showCommand={showCommandsSections(form.fields)}
os={registerAgentFormValues.operatingSystem.name}
onCopy={() => setInstallCommandWasCopied(true)}
password={registerAgentFormValues.optionalParams.wazuhPassword}
/>
<>
{missingStepsName?.length ? (
<EuiCallOut
color='warning'
title={`Please select the ${missingStepsName?.join(FORM_MESSAGE_CONJUNTION)}.`}
iconType='iInCircle'
/>
) : null}
{invalidFieldsName?.length ? (
<EuiCallOut
color='danger'
title={`There are fields with errors. Please verify them: ${invalidFieldsName?.join(
FORM_MESSAGE_CONJUNTION,
)}.`}
iconType='iInCircle'
style={{ marginTop: '1rem' }}
/>
) : null}
{!missingStepsName?.length && !invalidFieldsName?.length ? (
<CommandOutput
commandText={installCommand}
showCommand={showCommandsSections(form.fields)}
os={registerAgentFormValues.operatingSystem.name}
onCopy={() => setInstallCommandWasCopied(true)}
password={registerAgentFormValues.optionalParams.wazuhPassword}
/>
) : null}
</>
),
status: installCommandStepStatus,
},
{
title: <span className='stepTitle'>Start the Wazuh agent:</span>,
children: (
<CommandOutput
commandText={startCommand}
showCommand={showCommandsSections(form.fields)}
os={registerAgentFormValues.operatingSystem.name}
onCopy={() => setStartCommandWasCopied(true)}
/>
<>
{missingStepsName?.length ? (
<EuiCallOut
color='warning'
title={`Please select the ${missingStepsName?.join(FORM_MESSAGE_CONJUNTION)}.`}
iconType='iInCircle'
/>
) : null}
{invalidFieldsName?.length ? (
<EuiCallOut
color='danger'
title={`There are fields with errors. Please verify them: ${invalidFieldsName?.join(
FORM_MESSAGE_CONJUNTION,
)}.`}
iconType='iInCircle'
style={{ marginTop: '1rem' }}
/>
) : null}
{!missingStepsName?.length && !invalidFieldsName?.length ? (
<CommandOutput
commandText={startCommand}
showCommand={showCommandsSections(form.fields)}
os={registerAgentFormValues.operatingSystem.name}
onCopy={() => setStartCommandWasCopied(true)}
/>
) : null}
</>
),
status: startCommandStepStatus,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {
EnhancedFieldConfiguration,
UseFormReturn,
} from '../../../components/common/form/types';
import {
FormStepsDependencies,
RegisterAgentFormStatusManager,
} from './form-status-manager';

const defaultFormFieldData: EnhancedFieldConfiguration = {
changed: true,
value: 'value1',
error: '',
currentValue: '',
initialValue: '',
type: 'text',
onChange: () => {
console.log('onChange');
},
setInputRef: () => {
console.log('setInputRef');
},
inputRef: null,
};

const formFieldsDefault: UseFormReturn['fields'] = {
field1: {
...defaultFormFieldData,
value: '',
error: null,
},
field2: {
...defaultFormFieldData,
value: '',
error: 'error message',
},
field3: {
...defaultFormFieldData,
value: 'value valid',
error: null,
},
};

describe('RegisterAgentFormStatusManager', () => {
it('should create a instance', () => {
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
);
expect(registerAgentFormStatusManager).toBeDefined();
});

it('should return the form status', () => {
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
);
const formStatus = registerAgentFormStatusManager.getFormStatus();
expect(formStatus).toEqual({
field1: 'empty',
field2: 'invalid',
field3: 'complete',
});
});

it('should return the field status', () => {
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
);
const fieldStatus = registerAgentFormStatusManager.getFieldStatus('field1');
expect(fieldStatus).toEqual('empty');
});

it('should return error if fieldname not found', () => {
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
);
expect(() =>
registerAgentFormStatusManager.getFieldStatus('field4'),
).toThrowError('Fieldname not found');
});

it('should return a INVALID when the step have an error', () => {
const formSteps: FormStepsDependencies = {
step1: ['field1', 'field2'],
step2: ['field3'],
};
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
formSteps,
);
expect(registerAgentFormStatusManager).toBeDefined();
expect(registerAgentFormStatusManager.getStepStatus('step1')).toEqual(
'invalid',
);
});

it('should return COMPLETE when the step have no errors and is not empty', () => {
const formSteps: FormStepsDependencies = {
step1: ['field1', 'field2'],
step2: ['field3'],
};
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
formSteps,
);
expect(registerAgentFormStatusManager).toBeDefined();
expect(registerAgentFormStatusManager.getStepStatus('step2')).toEqual(
'complete',
);
});

it('should return EMPTY when the step all fields empty', () => {
const formSteps: FormStepsDependencies = {
step1: ['field1'],
step2: [ 'field2',
'field3' ],
};
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
formSteps,
);
expect(registerAgentFormStatusManager).toBeDefined();
expect(registerAgentFormStatusManager.getStepStatus('step1')).toEqual(
'empty',
);
});

it('should return all the steps status', () => {
const formSteps: FormStepsDependencies = {
step1: ['field1'],
step2: [ 'field2',
'field3' ],
step3: ['field3']
};
const registerAgentFormStatusManager = new RegisterAgentFormStatusManager(
formFieldsDefault,
formSteps,
);
expect(registerAgentFormStatusManager).toBeDefined();
expect(registerAgentFormStatusManager.getFormStepsStatus()).toEqual({
step1: 'empty',
step2: 'invalid',
step3: 'complete'
});
});
});
Loading

0 comments on commit d0b89f4

Please sign in to comment.