-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No blank backend role before adding a new one in Create User page #1384
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,35 +22,43 @@ import { | |
EuiFormRow, | ||
} from '@elastic/eui'; | ||
import { isEmpty } from 'lodash'; | ||
import React, { Dispatch, Fragment, SetStateAction } from 'react'; | ||
import React, { Dispatch, Fragment, SetStateAction, useState } from 'react'; | ||
import { | ||
appendElementToArray, | ||
removeElementFromArray, | ||
updateElementInArrayHandler, | ||
} from '../../utils/array-state-utils'; | ||
import { PanelWithHeader } from '../../utils/panel-with-header'; | ||
import { FormRow } from '../../utils/form-row'; | ||
import { DocLinks, LIMIT_WIDTH_INPUT_CLASS } from '../../constants'; | ||
|
||
function generateBackendRolesPanels( | ||
backendRoles: string[], | ||
setBackendRoles: Dispatch<SetStateAction<string[]>> | ||
setBackendRoles: Dispatch<SetStateAction<string[]>>, | ||
emptyRoleIndex: number, | ||
roleEmptyErrorMessage: string, | ||
setRoleEmptyErrorMessage: Dispatch<SetStateAction<string>> | ||
) { | ||
const panels = backendRoles.map((backendRole, arrayIndex) => { | ||
return ( | ||
<Fragment key={`backend-role-${arrayIndex}`}> | ||
<EuiFlexGroup> | ||
<EuiFlexItem className={LIMIT_WIDTH_INPUT_CLASS}> | ||
<FormRow headerText={arrayIndex === 0 ? 'Backend role' : ''}> | ||
<EuiFormRow | ||
label={arrayIndex === 0 ? 'Backend role' : ''} | ||
error={roleEmptyErrorMessage} | ||
isInvalid={arrayIndex === emptyRoleIndex && !isEmpty(roleEmptyErrorMessage)} | ||
> | ||
<EuiFieldText | ||
isInvalid={arrayIndex === emptyRoleIndex && !isEmpty(roleEmptyErrorMessage)} | ||
id={`backend-role-${arrayIndex}`} | ||
value={backendRole} | ||
onChange={(e) => | ||
updateElementInArrayHandler(setBackendRoles, [arrayIndex])(e.target.value) | ||
} | ||
onChange={(e) => { | ||
updateElementInArrayHandler(setBackendRoles, [arrayIndex])(e.target.value); | ||
setRoleEmptyErrorMessage(''); | ||
}} | ||
placeholder="Type in backend role" | ||
/> | ||
</FormRow> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<EuiFormRow hasEmptyLabelSpace={arrayIndex === 0 ? true : false}> | ||
|
@@ -67,6 +75,7 @@ function generateBackendRolesPanels( | |
</Fragment> | ||
); | ||
}); | ||
|
||
return <>{panels}</>; | ||
} | ||
|
||
|
@@ -75,6 +84,8 @@ export function BackendRolePanel(props: { | |
setState: Dispatch<SetStateAction<string[]>>; | ||
}) { | ||
const { state, setState } = props; | ||
const [roleEmptyErrorMessage, setRoleEmptyErrorMessage] = useState(''); | ||
const [emptyRoleIndex, setEmptyRoleIndex] = useState(-1); | ||
// Show one empty row if there is no data. | ||
if (isEmpty(state)) { | ||
setState(['']); | ||
|
@@ -86,12 +97,24 @@ export function BackendRolePanel(props: { | |
helpLink={DocLinks.AccessControlDoc} | ||
optional | ||
> | ||
{generateBackendRolesPanels(state, setState)} | ||
{generateBackendRolesPanels( | ||
state, | ||
setState, | ||
emptyRoleIndex, | ||
roleEmptyErrorMessage, | ||
setRoleEmptyErrorMessage | ||
)} | ||
<EuiSpacer /> | ||
<EuiButton | ||
id="backend-role-add-row" | ||
onClick={() => { | ||
appendElementToArray(setState, [], ''); | ||
if (state.indexOf('') !== -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙌 - thank you for adding the input validation here :) |
||
setRoleEmptyErrorMessage('Type a backend role before adding a new one'); | ||
setEmptyRoleIndex(state.indexOf('')); | ||
} else { | ||
setRoleEmptyErrorMessage(''); | ||
appendElementToArray(setState, [], ''); | ||
} | ||
}} | ||
> | ||
Add another backend role | ||
|
125 changes: 125 additions & 0 deletions
125
...nfiguration/panels/internal-user-edit/test/__snapshots__/backend-role-panel.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`User editing - backend role panel BackendRolePanel add backend role when one of the previous roles is blank 1`] = ` | ||
<PanelWithHeader | ||
headerSubText="Backend roles are used to map users from external authentication systems, such as LDAP or SAML to OpenSearch security roles." | ||
headerText="Backend roles" | ||
helpLink="https://opensearch.org/docs/latest/security-plugin/access-control/index/" | ||
optional={true} | ||
> | ||
<EuiFlexGroup> | ||
<EuiFlexItem | ||
className="limit-width-input" | ||
> | ||
<EuiFormRow | ||
describedByIds={Array []} | ||
display="row" | ||
error="" | ||
fullWidth={false} | ||
hasChildLabel={true} | ||
hasEmptyLabelSpace={false} | ||
isInvalid={false} | ||
label="Backend role" | ||
labelType="label" | ||
> | ||
<EuiFieldText | ||
id="backend-role-0" | ||
isInvalid={false} | ||
onChange={[Function]} | ||
placeholder="Type in backend role" | ||
value="admin" | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
<EuiFlexItem | ||
grow={false} | ||
> | ||
<EuiFormRow | ||
describedByIds={Array []} | ||
display="row" | ||
fullWidth={false} | ||
hasChildLabel={true} | ||
hasEmptyLabelSpace={true} | ||
labelType="label" | ||
> | ||
<EuiButton | ||
color="danger" | ||
id="backend-role-delete-0" | ||
onClick={[Function]} | ||
> | ||
Remove | ||
</EuiButton> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiFlexGroup> | ||
<EuiFlexItem | ||
className="limit-width-input" | ||
> | ||
<EuiFormRow | ||
describedByIds={Array []} | ||
display="row" | ||
error="" | ||
fullWidth={false} | ||
hasChildLabel={true} | ||
hasEmptyLabelSpace={false} | ||
isInvalid={false} | ||
label="" | ||
labelType="label" | ||
> | ||
<EuiFieldText | ||
id="backend-role-1" | ||
isInvalid={false} | ||
onChange={[Function]} | ||
placeholder="Type in backend role" | ||
value="HR" | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
<EuiFlexItem | ||
grow={false} | ||
> | ||
<EuiFormRow | ||
describedByIds={Array []} | ||
display="row" | ||
fullWidth={false} | ||
hasChildLabel={true} | ||
hasEmptyLabelSpace={false} | ||
labelType="label" | ||
> | ||
<EuiButton | ||
color="danger" | ||
id="backend-role-delete-1" | ||
onClick={[Function]} | ||
> | ||
Remove | ||
</EuiButton> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiSpacer /> | ||
<EuiButton | ||
id="backend-role-add-row" | ||
onClick={[Function]} | ||
> | ||
Add another backend role | ||
</EuiButton> | ||
</PanelWithHeader> | ||
`; | ||
|
||
exports[`User editing - backend role panel BackendRolePanel add backend role when the previous role is blank 1`] = ` | ||
<PanelWithHeader | ||
headerSubText="Backend roles are used to map users from external authentication systems, such as LDAP or SAML to OpenSearch security roles." | ||
headerText="Backend roles" | ||
helpLink="https://opensearch.org/docs/latest/security-plugin/access-control/index/" | ||
optional={true} | ||
> | ||
<EuiSpacer /> | ||
<EuiButton | ||
id="backend-role-add-row" | ||
onClick={[Function]} | ||
> | ||
Add another backend role | ||
</EuiButton> | ||
</PanelWithHeader> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change, please revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be a ./gradlew spotlessApply thing. If so, it is probably fine.