Skip to content

Commit

Permalink
Add loading spinner and useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
marniks7 committed Jan 15, 2023
1 parent 10ef750 commit 84f6c97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 34 deletions.
10 changes: 4 additions & 6 deletions src/api/pipelineRuns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ it('generate new pipeline run maximum', () => {
metadata: {
annotations: {
keya: 'valuea',
'kubectl.kubernetes.io/last-applied-configuration': {
keya: 'valuea'
}
'kubectl.kubernetes.io/last-applied-configuration':
'{"apiVersion": "tekton.dev/v1beta1", "keya": "valuea"}'
},
labels: {
keyl: 'valuel',
Expand Down Expand Up @@ -418,9 +417,8 @@ it('generate new pipeline run last applied configuration should be removed', ()
kind: 'PipelineRun',
metadata: {
annotations: {
'kubectl.kubernetes.io/last-applied-configuration': {
keya: 'valuea'
}
'kubectl.kubernetes.io/last-applied-configuration':
'{"apiVersion": "tekton.dev/v1beta1", "keya": "valuea"}'
},
name: 'test',
namespace: 'test2'
Expand Down
12 changes: 11 additions & 1 deletion src/containers/CreatePipelineRun/CreatePipelineRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,18 @@ function CreatePipelineRun() {
);
payloadYaml = yaml.dump(payload);
}
const loadingMessage = intl.formatMessage({
id: 'dashboard.loading.pipelineRun',
defaultMessage: 'Loading PipelineRun…'
});

return <CreateYAMLEditor code={payloadYaml || ''} loading={isLoading} />;
return (
<CreateYAMLEditor
code={payloadYaml || ''}
loading={isLoading}
loadingMessage={loadingMessage}
/>
);
}
const pipelineRun = getPipelineRunPayload({
labels: labels.reduce((acc, { key, value }) => {
Expand Down
65 changes: 38 additions & 27 deletions src/containers/CreatePipelineRun/YAMLEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,40 @@ import {
Button,
Form,
FormGroup,
InlineNotification
InlineNotification,
Loading
} from 'carbon-components-react';
import yaml from 'js-yaml';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { StreamLanguage } from '@codemirror/language';
import { yaml as codeMirrorYAML } from '@codemirror/legacy-modes/mode/yaml';
import { useNavigate } from 'react-router-dom-v5-compat';
import { createPipelineRunRaw, useSelectedNamespace } from '../../api';

export function CreateYAMLEditor({ code: initialCode, loading = false }) {
export function CreateYAMLEditor({
code: initialCode,
loading = false,
loadingMessage = ''
}) {
const intl = useIntl();
const navigate = useNavigate();
const { selectedNamespace } = useSelectedNamespace();

const [
{ isCreating, prevLoading, submitError, validationErrorMessage, code },
setState
] = useState({
isCreating: false,
prevLoading: loading,
submitError: '',
validationErrorMessage: '',
code: initialCode
});
const [{ isCreating, submitError, validationErrorMessage, code }, setState] =
useState({
isCreating: false,
submitError: '',
validationErrorMessage: '',
code: initialCode
});

if (prevLoading !== loading) {
useEffect(() => {
setState(state => ({
...state,
code: initialCode,
prevLoading: loading
code: initialCode
}));
}
}, [loading]);

function validateNamespace(obj) {
if (!obj?.metadata?.namespace) {
Expand Down Expand Up @@ -187,23 +188,33 @@ export function CreateYAMLEditor({ code: initialCode, loading = false }) {
lowContrast
/>
)}
<FormGroup legendText="">
<CodeMirror
value={code}
height="800px"
theme="dark"
extensions={[StreamLanguage.define(codeMirrorYAML)]}
onChange={onChange}
editable={!loading}
/>
<FormGroup legendText="" className="tkn--codemirror--form">
<>
{loading && (
<div className="tkn--data-loading-overlay">
<Loading description={loadingMessage} withOverlay={false} />
<span className="tkn--data-loading-text">{loadingMessage}</span>
</div>
)}
{!loading && (
<CodeMirror
value={code}
height="800px"
theme="dark"
extensions={[StreamLanguage.define(codeMirrorYAML)]}
onChange={onChange}
editable={!loading}
/>
)}
</>
</FormGroup>
<Button
iconDescription={intl.formatMessage({
id: 'dashboard.actions.createButton',
defaultMessage: 'Create'
})}
onClick={handleSubmit}
disabled={isCreating}
disabled={isCreating || loading}
>
{intl.formatMessage({
id: 'dashboard.actions.createButton',
Expand Down
14 changes: 14 additions & 0 deletions src/containers/CreatePipelineRun/YAMLEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { CreateYAMLEditor } from './YAMLEditor';
import * as PipelineRunsAPI from '../../api/pipelineRuns';

const submitButton = allByText => allByText('Create')[0];
const cancelButton = allByText => allByText('Cancel')[0];
const pipelineRun = `
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
Expand Down Expand Up @@ -211,4 +212,17 @@ describe('YAMLEditor', () => {
expect(getByText(/Error creating PipelineRun/)).toBeTruthy();
});
});

it('during loading loading message should be displayed', () => {
const { getAllByText } = renderWithRouter(
<CreateYAMLEditor loading loadingMessage="wait. test is in progress" />
);
expect(getAllByText(/wait. test is in progress/)).toBeTruthy();
});

it('during loading button create should be disabled and button cancel should not', () => {
const { queryAllByText } = renderWithRouter(<CreateYAMLEditor loading />);
expect(submitButton(queryAllByText).disabled).toBe(true);
expect(cancelButton(queryAllByText).disabled).toBe(false);
});
});

0 comments on commit 84f6c97

Please sign in to comment.