Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the Editor to fetch file details if the parent doesn't pass… #1225

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions client/src/boot/registerQueries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Injector from 'lib/Injector';
import { fileInterface, file } from 'lib/fileFragments';
import readFilesQuery from 'state/files/readFilesQuery';
import readOneFileQuery from 'state/files/readOneFileQuery';
import readFilesQueryLegacy from 'state/files/_legacy/readFilesQuery';
import readOneFileQueryLegacy from 'state/files/_legacy/readOneFileQuery';
import readFileUsageQuery from 'state/files/readFileUsageQuery';

// Backward compatibility hack. Remove when GraphQL 4 is in core
Expand All @@ -11,6 +13,7 @@ const registerQueries = () => {
Injector.query.registerFragment('FileInterfaceFields', fileInterface);
Injector.query.registerFragment('FileFields', file);
Injector.query.register('ReadFilesQuery', isLegacy ? readFilesQueryLegacy : readFilesQuery);
Injector.query.register('ReadOneFileQuery', isLegacy ? readOneFileQueryLegacy : readOneFileQuery);
Injector.query.register('readFileUsageQuery', readFileUsageQuery);
};
export default registerQueries;
3 changes: 1 addition & 2 deletions client/src/containers/AssetAdmin/AssetAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,9 @@ class AssetAdmin extends Component {

const editorProps = {
dialog,
targetId,
fileId: targetId,
schemaUrl,
schemaUrlQueries,
file: this.findFile(targetId),
onClose: this.handleCloseFile,
onSubmit: this.handleSubmitEditor,
onUnpublish: this.handleUnpublish,
Expand Down
16 changes: 9 additions & 7 deletions client/src/containers/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import FormBuilderModal from 'components/FormBuilderModal/FormBuilderModal';
import * as UnsavedFormsActions from 'state/unsavedForms/UnsavedFormsActions';
import fileShape from 'lib/fileShape';
import PropTypes from 'prop-types';
import { inject } from 'lib/Injector';
import { inject, injectGraphql } from 'lib/Injector';
import * as confirmDeletionActions from 'state/confirmDeletion/ConfirmDeletionActions';
import * as modalActions from 'state/modal/ModalActions';
import EditorHeader, { buttonStates } from './EditorHeader';
Expand Down Expand Up @@ -48,7 +48,7 @@ class Editor extends Component {
* @returns {string}
*/
getFormSchemaUrl() {
const { schemaUrlQueries, schemaUrl, targetId } = this.props;
const { schemaUrlQueries, schemaUrl, fileId } = this.props;

const parsedURL = url.parse(schemaUrl);
const parsedQs =
Expand All @@ -59,7 +59,7 @@ class Editor extends Component {

return url.format({
...parsedURL,
pathname: `${parsedURL.path}/${targetId}`,
pathname: `${parsedURL.path}/${fileId}`,
search: qs.stringify(parsedQs)
});
}
Expand Down Expand Up @@ -206,6 +206,7 @@ class Editor extends Component {
const schemaUrl = this.getFormSchemaUrl();

let showButton = buttonStates.SWITCH;

if (dialog && file && file.type !== 'folder') {
// When editing the details of a file from inside the modal, we always show the back button
// Otherwise, we only show theb ack button in mobile view to allow deselection of file
Expand Down Expand Up @@ -259,7 +260,7 @@ class Editor extends Component {

render() {
const formSchemaUrl = this.getFormSchemaUrl();
const modalSchemaUrl = `${this.props.addToCampaignSchemaUrl}/${this.props.targetId}`;
const modalSchemaUrl = `${this.props.addToCampaignSchemaUrl}/${this.props.fileId}`;
const editorClasses = classnames(
'panel', 'form--no-dividers', 'editor', {
'editor--asset-dropzone--disable': !this.props.enableDropzone
Expand Down Expand Up @@ -323,12 +324,11 @@ class Editor extends Component {
Editor.propTypes = {
file: fileShape,
className: PropTypes.string,
targetId: PropTypes.number.isRequired,
fileId: PropTypes.number.isRequired,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed this to fileId. This avoids us having to do gymnastic to change the prop from targetId to fileID when we pass it to the graphQL query and it just makes more sense since the target will always be a file ID any way.

enableDropzone: PropTypes.bool,
dialog: PropTypes.bool,
onClose: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
// onUnpublish: PropTypes.func.isRequired,
schemaUrl: PropTypes.string.isRequired,
schemaUrlQueries: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
Expand Down Expand Up @@ -360,6 +360,7 @@ function mapStateToProps({ assetAdmin: { gallery, modal } }) {

export { Editor as Component };


export default compose(
inject(
['Loading'],
Expand All @@ -368,5 +369,6 @@ export default compose(
}),
() => 'AssetAdmin.Editor',
),
connect(mapStateToProps, mapDispatchToProps)
connect(mapStateToProps, mapDispatchToProps),
injectGraphql('ReadOneFileQuery')
)(Editor);
2 changes: 1 addition & 1 deletion client/src/containers/Editor/tests/Editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const render = (props) => {
const baseProps = {
schemaUrlQueries: [],
schemaUrl: 'edit/file',
targetId: 123,
fileId: 123,
onClose: jest.fn(),
onSubmit: jest.fn(),
actions: {
Expand Down
52 changes: 52 additions & 0 deletions client/src/state/files/_legacy/readOneFileQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { graphqlTemplates } from 'lib/Injector';

const apolloConfig = {
options({ fileId }) {
return {
variables: {
rootFilter: {
id: fileId
},
},
};
},
props({ data: { error, readFiles, loading } }) {
const file = (readFiles && readFiles.nodes[0]) ? readFiles.nodes[0] : null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only difference between v3.5 and 4. In 4, results are on the top level of readFiles

const errors = error && error.graphQLErrors &&
error.graphQLErrors.map((graphQLError) => graphQLError.message);
return {
loading,
file,
graphQLErrors: errors
};
},
};

const { READ } = graphqlTemplates;
const query = {
apolloConfig,
templateName: READ,
pluralName: 'Files',
pagination: false,
params: {
rootFilter: 'FileFilterInput'
},
args: {
root: {
filter: 'rootFilter'
}
},
fragments: [
'FileInterfaceFields',
'FileFields',
],
fields: [
'nodes', [
'...FileInterfaceFields',
'...FileFields'
],
],
};


export default query;
54 changes: 54 additions & 0 deletions client/src/state/files/readOneFileQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { graphqlTemplates } from 'lib/Injector';

/**
* Query to fetch a single file by its ID. The resulting file will be pass to your component
* under a `file` prop. `null` will be pass if the specified ID does not exist.
*/
const apolloConfig = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file mostly copies readFilesQuery, but removes all the fluff.

We're still using the same graphQL endpoint to retrieve our single file. Maybe we should create an actual readOneFile query server side. Not sure if that's advisable.

options({ fileId }) {
return {
variables: {
rootFilter: {
id: fileId
},
},
};
},
props({ data: { error, readFiles, loading: networkLoading } }) {
const file = readFiles ? readFiles[0] : null;
emteknetnz marked this conversation as resolved.
Show resolved Hide resolved
const errors = error && error.graphQLErrors &&
error.graphQLErrors.map((graphQLError) => graphQLError.message);
return {
loading: networkLoading,
file,
graphQLErrors: errors,
};
},
};

const { READ } = graphqlTemplates;
const query = {
apolloConfig,
templateName: READ,
pluralName: 'Files',
pagination: false,
params: {
rootFilter: 'FileFilterInput',
},
args: {
root: {
filter: 'rootFilter'
}
},
fragments: [
'FileInterfaceFields',
'FileFields',
],
fields: [
'...FileInterfaceFields',
'...FileFields',
],
maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved
};


export default query;