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

Support upload and display of new images #800

Merged
merged 4 commits into from
Feb 27, 2019
Merged

Conversation

benthorner
Copy link
Contributor

@benthorner benthorner commented Feb 19, 2019

https://trello.com/c/QDEuNPgp/653-allow-users-to-upload-an-inline-image-in-the-context-of-a-modal

This exposes the upload form in the modal context of the images index
page, and overrides the 'Upload' button to post the form data i.e. upload
the image. Depending on the form data, we may need to render the
response HTML to display requirements issues, or follow a redirect.

The API of the FormData class means we can support posting a form
generically, which is handled using a new 'postModalForm' function in
the existing 'glue' module called 'inline-image-modal'. We also follow
any redirects automatically, to avoid unnecessary boilerplate '.fetch's.

In future work we will implement the rest of the upload workflow, so
that the modal loads the crop page after the image is uploaded.

@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 19, 2019 17:03 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 20, 2019 10:36 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 20, 2019 15:19 Inactive
@benthorner benthorner changed the title [WIP] Support upload and display of new images Support upload and display of new images Feb 20, 2019
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 20, 2019 15:37 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 20, 2019 15:49 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 11:19 Inactive
@benthorner benthorner force-pushed the inline-image-modal branch 3 times, most recently from 623d692 to f45ce9d Compare February 21, 2019 11:39
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 12:00 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 12:01 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 12:05 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 14:19 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 21, 2019 17:35 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 26, 2019 13:34 Inactive
@benthorner
Copy link
Contributor Author

@kevindew this is now ready, when you get a moment :-).

@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 26, 2019 14:50 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 26, 2019 14:57 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 26, 2019 15:17 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 11:29 Inactive
Copy link
Member

@kevindew kevindew left a comment

Choose a reason for hiding this comment

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

This is looking good, bit of a mixed bag of comments from me here as I've veered from super syntax issues to questioning whether the whole modal navigation strategy is a sufficient approach.

app/assets/javascripts/modules/inline-image-modal.js Outdated Show resolved Hide resolved
@@ -50,6 +75,17 @@ InlineImageModal.prototype.performAction = function (item) {
var editor = this.$module.closest('[data-module="markdown-editor"]')
this.$modal.close()
editor.selectionReplace(item.dataset.modalData)
},
'upload': function () {
this.postModalForm(item.dataset.modalActionForm)
Copy link
Member

Choose a reason for hiding this comment

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

Are we vulnerable at this point to dual submissions? I assume that nothing changes in the UI once they submit the form until we receive a response which could be a while for an upload.

Could well be that we have the same problem with the non modal form of this too but feedback seems to be clearer in a browser when you're changing a whole page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we are actually changing the modal to the loading screen - this happens before any action is invoked, as otherwise it kind of defeats the point of the loading screen ;-). I think there's a separate question, which you also asked, about the behaviour of short-lived loading screens.

I guess they can potentially click the button twice, really quickly, although I can't do this myself - in a very slow browser, perhaps.

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok - I think I might just not be understanding how this works. Is this correct?

  • When I click the button to submit the form it prevents default on that event
  • It focuses the modal again
  • It hides the form and shows the loading view
  • it pulls the data from the hidden view and submits it
  • that form is only cleared once showDynamic section is called again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right - is there still something you're concerned about here?

Copy link
Member

Choose a reason for hiding this comment

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

Nope - just taking my old brain a while to understand

app/assets/javascripts/modules/inline-image-modal.js Outdated Show resolved Hide resolved
create_image_path(@document),
id: "image-upload",
multipart: true,
data: { gtm: "image-upload-submit" },
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if GTM will still receive this event? I assume this listens to form submit events but it doesn't look like we listen to any of those.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alex-ju is currently working on rewriting our local modules/ to support re-initialisation, just like components. I think you're referring to GTMFormListener here, so it should be fixed by that :-).

Copy link
Member

Choose a reason for hiding this comment

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

I was referring to lack of a submit event existing. If GTM is listening for form submit events then this one will never be called because we preventDefault on the button click event.

GTM likely has all sorts of voodoo though so this may not be remotely a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - it wouldn't have worked. I've just added a commit that, with the work @alex-ju is doing, will preserve the current behaviour.

app/views/images/index.html.erb Outdated Show resolved Hide resolved
This exposes the upload form in the modal context of the images index
page, and overrides the 'Upload' button to post the form data i.e. upload
the image. Depending on the form data, we may need to render the
response HTML to display requirements issues, or follow a redirect.

The API of the FormData class means we can support posting a form
generically, which is handled using a new 'postModalForm' function in
the existing 'glue' module called 'inline-image-modal'. We also follow
any redirects automatically, to avoid unnecessary boilerplate '.fetch's.

In future work we will implement the rest of the upload workflow, so
that the modal loads the crop page after the image is uploaded.
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 11:46 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 12:04 Inactive
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 12:06 Inactive
This manually focusses any error-summary component to simulate its
normal behaviour. Ideally, we should change this and other components in
govuk-frontend so that they can be re-initialized, and in this case,
stop relying on window events to auto-focus.

We also have components from govuk_publishing_components, which can be
idempotently re-initialized with 'GOVUK.modules.start()', but this is
not necessary yet. And we have our local components, which don't support
re-initialization at all, which we will address in future work.
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 12:07 Inactive
@kevindew
Copy link
Member

Glad the submit thing wasn't much of a pain. Thinking about it it would be pretty trivial to support form submissions too if we had

item.addEventListener('click', function (event) {
listen on submit events as well and set data on form rather than buttons.

Copy link
Member

@kevindew kevindew left a comment

Choose a reason for hiding this comment

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

Thanks for resolving the comments. This seems good to merge now with maybe some further digging needed for GTM.

end

def and_i_upload_an_invalid_image
find('form input[type="file"]').set(Rails.root.join(file_fixture("text-file.txt")))
Copy link
Member

Choose a reason for hiding this comment

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

I wrote a comment on this before but it doesn't seem to be here so apologies if it's come through twice - I'm assuming I just lost it in the review

I notice we didn't need the Rails.root.join on

find('form input[type="file"]').set(file_fixture("text-file.txt"))
so wondering if this is needed or special.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was strange - I encountered this and forgot to come back to it. I've added another commit to explain, but it seems like absolute paths are required for JS feature specs.

Copy link
Member

Choose a reason for hiding this comment

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

Oh that's a pain - thanks for the commit explanation

Previously we overrode the behaviour of the 'Upload' button to support
the inline image workflow, but this interferes with our GTM 'submit'
listener. This changes the way we override the 'Upload' action, so that
it happens on the form instead of the button - with some changes to
our GTMFormListener module to support re-initialisation, this works.
@benthorner benthorner temporarily deployed to content-publisher-revie-pr-800 February 27, 2019 12:26 Inactive
This is only required for the JS feature specs, where the headless
engine seems to need absolute paths in order to work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants