Skip to content

Commit

Permalink
Fix the problem with instant redirect (provectus#390)
Browse files Browse the repository at this point in the history
* Fix the problem with instant redirect

* Rewrite updateSchema thunk
  • Loading branch information
GneyHabub committed May 1, 2021
1 parent 6a3c61d commit 4451b66
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const LatestVersionItem: React.FC<LatestVersionProps> = ({
<div className="tile is-parent">
<div className="tile is-child box">
<JSONEditor
isFixedHeight
name="schema"
value={JSON.stringify(JSON.parse(schema), null, '\t')}
showGutter={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const SchemaVersion: React.FC<SchemaVersionProps> = ({
<td>{id}</td>
<td>
<JSONEditor
isFixedHeight
name="schema"
value={JSON.stringify(JSON.parse(schema), null, '\t')}
showGutter={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ exports[`LatestVersionItem matches snapshot 1`] = `
className="tile is-child box"
>
<JSONEditor
isFixedHeight={true}
name="schema"
readOnly={true}
showGutter={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`SchemaVersion matches snapshot 1`] = `
</td>
<td>
<JSONEditor
isFixedHeight={true}
name="schema"
readOnly={true}
showGutter={false}
Expand Down
38 changes: 26 additions & 12 deletions kafka-ui-react-app/src/components/Schemas/Edit/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Edit = ({
const {
register,
handleSubmit,
formState: { isSubmitting },
formState: { isSubmitting, isDirty },
control,
} = useForm<NewSchemaSubjectRaw>({ mode: 'onChange' });

Expand All @@ -62,15 +62,19 @@ const Edit = ({
compatibilityLevel: CompatibilityLevelCompatibilityEnum;
newSchema: string;
}) => {
await updateSchema(
schema,
newSchema,
schemaType,
compatibilityLevel,
clusterName,
subject
);
history.push(clusterSchemaPath(clusterName, subject));
try {
await updateSchema(
schema,
newSchema,
schemaType,
compatibilityLevel,
clusterName,
subject
);
history.push(clusterSchemaPath(clusterName, subject));
} catch (e) {
// do not redirect
}
},
[schema, register, control, clusterName, subject, updateSchema, history]
);
Expand All @@ -96,7 +100,7 @@ const Edit = ({
</div>
</div>

{schemasAreFetched && !isSubmitting ? (
{schemasAreFetched ? (
<div className="box">
<form
onSubmit={handleSubmit(onSubmit)}
Expand All @@ -111,6 +115,7 @@ const Edit = ({
required: 'Schema Type is required.',
})}
defaultValue={schema.schemaType}
disabled={isSubmitting}
>
{Object.keys(SchemaType).map((type: string) => (
<option key={type} value={type}>
Expand All @@ -128,6 +133,7 @@ const Edit = ({
name="compatibilityLevel"
ref={register()}
defaultValue={schema.compatibilityLevel}
disabled={isSubmitting}
>
{Object.keys(CompatibilityLevelCompatibilityEnum).map(
(level: string) => (
Expand All @@ -143,7 +149,9 @@ const Edit = ({
<div className="column is-one-half">
<h4 className="title is-5 mb-2">Latest Schema</h4>
<JSONEditor
isFixedHeight
readOnly
height="500px"
value={getFormattedSchema()}
name="latestSchema"
highlightActiveLine={false}
Expand All @@ -154,8 +162,10 @@ const Edit = ({
<Controller
control={control}
name="newSchema"
disabled={isSubmitting}
render={({ name, onChange }) => (
<JSONEditor
readOnly={isSubmitting}
defaultValue={getFormattedSchema()}
name={name}
onChange={onChange}
Expand All @@ -164,7 +174,11 @@ const Edit = ({
/>
</div>
</div>
<button type="submit" className="button is-primary">
<button
type="submit"
className="button is-primary"
disabled={!isDirty || isSubmitting}
>
Submit
</button>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`Edit Component when schemas are fetched matches the snapshot 1`] = `
>
<select
defaultValue="AVRO"
disabled={false}
name="schemaType"
>
<option
Expand Down Expand Up @@ -84,6 +85,7 @@ exports[`Edit Component when schemas are fetched matches the snapshot 1`] = `
>
<select
defaultValue="BACKWARD"
disabled={false}
name="compatibilityLevel"
>
<option
Expand Down Expand Up @@ -143,7 +145,9 @@ exports[`Edit Component when schemas are fetched matches the snapshot 1`] = `
Latest Schema
</h4>
<JSONEditor
height="500px"
highlightActiveLine={false}
isFixedHeight={true}
name="latestSchema"
readOnly={true}
value="{
Expand Down Expand Up @@ -261,13 +265,15 @@ exports[`Edit Component when schemas are fetched matches the snapshot 1`] = `
"watchInternal": [Function],
}
}
disabled={false}
name="newSchema"
render={[Function]}
/>
</div>
</div>
<button
className="button is-primary"
disabled={true}
type="submit"
>
Submit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { connect } from 'react-redux';
import { RootState } from 'redux/interfaces';
import { createSchema } from 'redux/actions';
import { getSchemaCreated } from 'redux/reducers/schemas/selectors';

import New from './New';

const mapStateToProps = (state: RootState) => ({
isSchemaCreated: getSchemaCreated(state),
});

const mapDispatchToProps = {
createSchema,
};

export default connect(mapStateToProps, mapDispatchToProps)(New);
export default connect(null, mapDispatchToProps)(New);
30 changes: 20 additions & 10 deletions kafka-ui-react-app/src/components/common/JSONEditor/JSONEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import 'ace-builds/src-noconflict/mode-json5';
import 'ace-builds/src-noconflict/theme-textmate';
import React from 'react';

const JSONEditor: React.FC<IAceEditorProps> = (props) => (
<AceEditor
mode="json5"
theme="textmate"
tabSize={2}
width="100%"
wrapEnabled
{...props}
/>
);
interface JSONEditorProps extends IAceEditorProps {
isFixedHeight?: boolean;
}

const JSONEditor: React.FC<JSONEditorProps> = (props) => {
const { isFixedHeight, value } = props;
return (
<AceEditor
mode="json5"
theme="textmate"
tabSize={2}
width="100%"
height={
isFixedHeight ? `${(value?.split('\n').length || 32) * 16}px` : '500px'
}
wrapEnabled
{...props}
/>
);
};

export default JSONEditor;
Original file line number Diff line number Diff line change
Expand Up @@ -122,53 +122,22 @@ describe('Thunks', () => {
expect(error.status).toEqual(404);
expect(store.getActions()).toEqual([
actions.createSchemaAction.request(),
actions.createSchemaAction.failure({}),
actions.createSchemaAction.failure({
alert: {
response: {
body: undefined,
status: 404,
statusText: 'Not Found',
},
subject: 'schema-NewSchema',
title: 'Schema NewSchema',
},
}),
]);
}
});
});

describe('updateSchemaCompatibilityLevel', () => {
it('creates UPDATE_SCHEMA__SUCCESS when patching a schema', async () => {
fetchMock.putOnce(
`/api/clusters/${clusterName}/schemas/${subject}/compatibility`,
200
);
await store.dispatch(
thunks.updateSchemaCompatibilityLevel(
clusterName,
subject,
CompatibilityLevelCompatibilityEnum.BACKWARD
)
);
expect(store.getActions()).toEqual([
actions.updateSchemaCompatibilityLevelAction.request(),
actions.updateSchemaCompatibilityLevelAction.success(),
]);
});

it('creates UPDATE_SCHEMA__SUCCESS when failing to patch a schema', async () => {
fetchMock.putOnce(
`/api/clusters/${clusterName}/schemas/${subject}/compatibility`,
404
);
try {
await store.dispatch(
thunks.updateSchemaCompatibilityLevel(
clusterName,
subject,
CompatibilityLevelCompatibilityEnum.BACKWARD
)
);
} catch (error) {
expect(error.status).toEqual(404);
expect(store.getActions()).toEqual([
actions.updateSchemaCompatibilityLevelAction.request(),
actions.updateSchemaCompatibilityLevelAction.failure({}),
]);
}
});
});
describe('deleteSchema', () => {
it('fires DELETE_SCHEMA__SUCCESS on success', async () => {
fetchMock.deleteOnce(
Expand Down Expand Up @@ -203,7 +172,7 @@ describe('Thunks', () => {
});

describe('updateSchema', () => {
it('calls createSchema', () => {
it('calls PATCH_SCHEMA__REQUEST', () => {
store.dispatch(
thunks.updateSchema(
fixtures.schema,
Expand All @@ -215,23 +184,7 @@ describe('Thunks', () => {
)
);
expect(store.getActions()).toEqual([
actions.createSchemaAction.request(),
]);
});

it('calls updateSchema and does not call createSchema when schema does not change', () => {
store.dispatch(
thunks.updateSchema(
fixtures.schema,
fixtures.schema.schema,
SchemaType.JSON,
CompatibilityLevelCompatibilityEnum.FORWARD,
clusterName,
subject
)
);
expect(store.getActions()).toEqual([
actions.updateSchemaCompatibilityLevelAction.request(),
actions.updateSchemaAction.request(),
]);
});
});
Expand Down
10 changes: 5 additions & 5 deletions kafka-ui-react-app/src/redux/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ export const createSchemaAction = createAsyncAction(
'POST_SCHEMA__FAILURE'
)<undefined, SchemaSubject, { alert?: FailurePayload }>();

export const updateSchemaCompatibilityLevelAction = createAsyncAction(
'PATCH_SCHEMA_COMPATIBILITY__REQUEST',
'PATCH_SCHEMA_COMPATIBILITY__SUCCESS',
'PATCH_SCHEMA_COMPATIBILITY__FAILURE'
)<undefined, undefined, { alert?: FailurePayload }>();
export const updateSchemaAction = createAsyncAction(
'PATCH_SCHEMA__REQUEST',
'PATCH_SCHEMA__SUCCESS',
'PATCH_SCHEMA__FAILURE'
)<undefined, SchemaSubject, { alert?: FailurePayload }>();

export const deleteSchemaAction = createAsyncAction(
'DELETE_SCHEMA__REQUEST',
Expand Down
Loading

0 comments on commit 4451b66

Please sign in to comment.