Merge pull request #5553 from keithjgrant/inventory-add-save

Inventory Add form fixes

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-01-06 14:29:19 +00:00
committed by GitHub
5 changed files with 33 additions and 4 deletions

View File

@@ -16,7 +16,7 @@ import ErrorDetail from '@components/ErrorDetail';
const EmptyState = styled(PFEmptyState)` const EmptyState = styled(PFEmptyState)`
width: var(--pf-c-empty-state--m-lg--MaxWidth); width: var(--pf-c-empty-state--m-lg--MaxWidth);
max-width: 100%; margin: 0 auto;
`; `;
async function logout() { async function logout() {

View File

@@ -48,7 +48,9 @@ function InventoryAdd({ history, i18n }) {
data: { id: inventoryId }, data: { id: inventoryId },
} = await InventoriesAPI.create({ } = await InventoriesAPI.create({
organization: organization.id, organization: organization.id,
insights_credential: insights_credential.id, insights_credential: insights_credential
? insights_credential.id
: null,
...remainingValues, ...remainingValues,
}); });
if (instanceGroups) { if (instanceGroups) {
@@ -68,7 +70,15 @@ function InventoryAdd({ history, i18n }) {
}; };
if (error) { if (error) {
return <ContentError />; return (
<PageSection>
<Card>
<CardBody>
<ContentError error={error} />
</CardBody>
</Card>
</PageSection>
);
} }
if (isLoading) { if (isLoading) {
return <ContentLoading />; return <ContentLoading />;

View File

@@ -77,6 +77,8 @@ function InventoryForm({
form.setFieldValue('organization', value); form.setFieldValue('organization', value);
}} }}
value={field.value} value={field.value}
touched={form.touched.organization}
error={form.errors.organization}
required required
/> />
)} )}

View File

@@ -1,9 +1,16 @@
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
export function required(message, i18n) { export function required(message, i18n) {
const errorMessage = message || i18n._(t`This field must not be blank`);
return value => { return value => {
if (typeof value === 'string' && !value.trim()) { if (typeof value === 'string' && !value.trim()) {
return message || i18n._(t`This field must not be blank`); return errorMessage;
}
if (typeof value === 'number' && !Number.isNaN(value)) {
return undefined;
}
if (!value) {
return errorMessage;
} }
return undefined; return undefined;
}; };

View File

@@ -27,6 +27,16 @@ describe('validators', () => {
}); });
}); });
test('required interprets undefined as empty value', () => {
expect(required(null, i18n)(undefined)).toEqual({
id: 'This field must not be blank',
});
});
test('required interprets 0 as non-empty value', () => {
expect(required(null, i18n)(0)).toBeUndefined();
});
test('maxLength accepts value below max', () => { test('maxLength accepts value below max', () => {
expect(maxLength(10, i18n)('snazzy')).toBeUndefined(); expect(maxLength(10, i18n)('snazzy')).toBeUndefined();
}); });