Lookup tweaks/bug fixes

This commit is contained in:
Keith Grant
2019-12-04 08:48:49 -08:00
parent 569b5bc533
commit 75b7d74f91
5 changed files with 33 additions and 11 deletions

View File

@@ -71,7 +71,7 @@ function CredentialLookup({
optionCount={count} optionCount={count}
header={label} header={label}
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
readOnly={canDelete} readOnly={!canDelete}
selectItem={item => dispatch({ type: 'SELECT_ITEM', item })} selectItem={item => dispatch({ type: 'SELECT_ITEM', item })}
deselectItem={item => dispatch({ type: 'DESELECT_ITEM', item })} deselectItem={item => dispatch({ type: 'DESELECT_ITEM', item })}
/> />

View File

@@ -43,9 +43,7 @@ function MultiCredentialsLookup(props) {
try { try {
const types = await loadCredentialTypes(); const types = await loadCredentialTypes();
setCredentialTypes(types); setCredentialTypes(types);
setSelectedType( setSelectedType(types.find(type => type.kind === 'ssh') || types[0]);
types.find(type => type.name === 'Machine') || types[0]
);
} catch (err) { } catch (err) {
onError(err); onError(err);
} }
@@ -71,7 +69,7 @@ function MultiCredentialsLookup(props) {
})(); })();
}, [selectedType, history.location.search, onError]); }, [selectedType, history.location.search, onError]);
const isMultiple = selectedType && selectedType.name === 'Vault'; const isMultiple = selectedType && selectedType.kind === 'vault';
const renderChip = ({ item, removeItem, canDelete }) => ( const renderChip = ({ item, removeItem, canDelete }) => (
<CredentialChip <CredentialChip
key={item.id} key={item.id}

View File

@@ -1,15 +1,17 @@
import React from 'react'; import React from 'react';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
function LookupErrorMessage({ error }) { function LookupErrorMessage({ error, i18n }) {
if (!error) { if (!error) {
return null; return null;
} }
return ( return (
<div className="pf-c-form__helper-text pf-m-error" aria-live="polite"> <div className="pf-c-form__helper-text pf-m-error" aria-live="polite">
{error.message || 'An error occured'} {error.message || i18n._(t`An error occured`)}
</div> </div>
); );
} }
export default LookupErrorMessage; export default withI18n()(LookupErrorMessage);

View File

@@ -1,5 +1,3 @@
// import { useReducer, useEffect } from 'react';
export default function reducer(state, action) { export default function reducer(state, action) {
switch (action.type) { switch (action.type) {
case 'SELECT_ITEM': case 'SELECT_ITEM':
@@ -51,10 +49,16 @@ function toggleModal(state) {
if (isModalOpen) { if (isModalOpen) {
return closeModal(state); return closeModal(state);
} }
let selectedItems = [];
if (multiple) {
selectedItems = [...value];
} else if (value) {
selectedItems.push(value);
}
return { return {
...state, ...state,
isModalOpen: !isModalOpen, isModalOpen: !isModalOpen,
selectedItems: multiple ? [...value] : [value], selectedItems,
}; };
} }

View File

@@ -129,6 +129,24 @@ describe('Lookup reducer', () => {
}); });
}); });
it('should set null value to empty array', () => {
const state = {
isModalOpen: false,
selectedItems: [{ id: 1 }],
value: null,
multiple: false,
};
const result = reducer(state, {
type: 'TOGGLE_MODAL',
});
expect(result).toEqual({
isModalOpen: true,
selectedItems: [],
value: null,
multiple: false,
});
});
it('should open the modal (multiple)', () => { it('should open the modal (multiple)', () => {
const state = { const state = {
isModalOpen: false, isModalOpen: false,