Skip to content

Commit

Permalink
Merge pull request #162 from creative-commoners/pulls/4/loading
Browse files Browse the repository at this point in the history
ENH Show loading component during ajax read requests
  • Loading branch information
sabina-talipova authored Jan 10, 2024
2 parents bfc5eb3 + 09ca349 commit 5db919e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { injectGraphql } from 'lib/Injector';
import fieldHolder from 'components/FieldHolder/FieldHolder';
import LinkPicker from 'components/LinkPicker/LinkPicker';
import LinkPickerTitle from 'components/LinkPicker/LinkPickerTitle';
import Loading from 'components/Loading/Loading';
import LinkType from 'types/LinkType';
import LinkModalContainer from 'containers/LinkModalContainer';
import * as toastsActions from 'state/toasts/ToastsActions';
Expand Down Expand Up @@ -45,6 +46,7 @@ const LinkField = ({
}) => {
const [data, setData] = useState({});
const [editingID, setEditingID] = useState(0);
const [loading, setLoading] = useState(false);

// Ensure we have a valid array
let linkIDs = value;
Expand All @@ -61,6 +63,7 @@ const LinkField = ({
// This happens any time a link is added or removed and triggers a re-render
useEffect(() => {
if (!editingID && linkIDs.length > 0) {
setLoading(true);
const query = [];
for (const linkID of linkIDs) {
query.push(`itemIDs[]=${linkID}`);
Expand All @@ -70,9 +73,11 @@ const LinkField = ({
.then(response => response.json())
.then(responseJson => {
setData(responseJson);
setLoading(false);
})
.catch(() => {
actions.toasts.error(i18n._t('LinkField.FAILED_TO_LOAD_LINKS', 'Failed to load links'))
setLoading(false);
});
}
}, [editingID, value && value.length]);
Expand Down Expand Up @@ -172,6 +177,10 @@ const LinkField = ({
const renderModal = !saveRecordFirst && Boolean(editingID);
const saveRecordFirstText = i18n._t('LinkField.SAVE_RECORD_FIRST', 'Cannot add links until the record has been saved');

if (loading && !saveRecordFirst) {
return <div className="link-field__loading"><Loading/></div>;
}

return <LinkFieldContext.Provider value={{ ownerID, ownerClass, ownerRelation, actions }}>
{ saveRecordFirst && <div className="link-field__save-record-first">{saveRecordFirstText}</div>}
{ renderPicker && <LinkPicker
Expand Down
9 changes: 9 additions & 0 deletions client/src/components/LinkField/LinkField.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.link-field__save-record-first {
padding-top: 7px;
}

.link-field__loading {
.cms-content-loading-spinner {
position: initial;
}
.ui-widget-overlay-light {
opacity: 0;
}
}
32 changes: 28 additions & 4 deletions client/src/components/LinkField/tests/LinkField-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* global jest, test */
import React from 'react';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { Component as LinkField } from '../LinkField';

let doResolve;

jest.mock('lib/Backend', () => ({
get: () => new Promise(() => {})
get: () => new Promise((resolve) => {
doResolve = resolve;
})
}));

window.ss.config = {
Expand All @@ -25,7 +29,12 @@ function makeProps(obj = {}) {
value: 123,
onChange: () => {},
types: [],
actions: {},
actions: {
toasts: {
success: () => {},
error: () => {}
}
},
isMulti: false,
canCreate: true,
ownerID: 123,
Expand All @@ -41,14 +50,29 @@ test('LinkField will render save-record-first div if ownerID is 0', async () =>
})}
/>);
expect(container.querySelectorAll('.link-field__save-record-first')).toHaveLength(1);
expect(container.querySelectorAll('.link-field__loading')).toHaveLength(0);
expect(container.querySelectorAll('.link-picker')).toHaveLength(0);
});

test('LinkField will render loading indicator if ownerID is not 0', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1

})}
/>);
expect(container.querySelectorAll('.link-field__save-record-first')).toHaveLength(0);
expect(container.querySelectorAll('.link-field__loading')).toHaveLength(1);
expect(container.querySelectorAll('.link-picker')).toHaveLength(0);
});

test('LinkField will render link-picker if ownerID is not 0', async () => {
test('LinkField will render link-picker if ownerID is not 0 and has finished loading', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1
})}
/>);
doResolve();
await screen.findByText('Add Link');
expect(container.querySelectorAll('.link-field__save-record-first')).toHaveLength(0);
expect(container.querySelectorAll('.link-field__loading')).toHaveLength(0);
expect(container.querySelectorAll('.link-picker')).toHaveLength(1);
});

0 comments on commit 5db919e

Please sign in to comment.