-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #527 from TelosLabs/feature/nonprofit-unsaved-chan…
…ges-alert Feature/nonprofit unsaved changes alert
- Loading branch information
Showing
22 changed files
with
269 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/javascript/controllers/detect_form_changes_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = ["form", "utilityInput"]; | ||
static values = { | ||
inputTypes: String, | ||
} | ||
|
||
connect() { | ||
const initialFormInputs = [...this.formTarget.querySelectorAll(this.inputTypesValue)]; | ||
this.formTarget.initialNumberOfInputs = this.validInputsLength(initialFormInputs); | ||
this.formTarget.changed = false; | ||
} | ||
|
||
captureUserInput(event) { | ||
const input = event.target; | ||
|
||
if (this.utilityInputTargets.includes(input) || this.eventAndInputIncompatible(event, input)) { | ||
return; | ||
} | ||
|
||
this.detectChanges(this.formTarget); | ||
} | ||
|
||
detectChanges(form, newInputAdded = false) { | ||
const didFormChange = newInputAdded || this.changesInForm(form); | ||
form.changed = didFormChange; | ||
} | ||
|
||
changesInForm(form) { | ||
const currentFormInputs = [...form.querySelectorAll(this.inputTypesValue)]; | ||
|
||
return (this.validInputsLength(currentFormInputs) !== form.initialNumberOfInputs) || | ||
currentFormInputs.some(input => this.inputValueChanged(input)); | ||
} | ||
|
||
inputValueChanged(input) { | ||
if (input.type === "checkbox" || input.type === "radio") { | ||
return input.checked !== input.defaultChecked | ||
} | ||
|
||
if (input.type === "select-one") { | ||
const selectedOption = input.options[input.selectedIndex]; | ||
return selectedOption.selected !== selectedOption.defaultSelected; | ||
} | ||
|
||
return input.value.trim() !== input.defaultValue; | ||
} | ||
|
||
// Some inputs don't send any data | ||
validInputsLength(currentFormInputs) { | ||
const validInputs = currentFormInputs.filter(input => { | ||
return !this.utilityInputTargets.includes(input); | ||
}); | ||
|
||
return validInputs.length; | ||
} | ||
|
||
// `change` event is more suitable for input types that involve user selection or choice. | ||
eventAndInputIncompatible(event, input) { | ||
const inputIsSelectable = | ||
["radio", "checkbox", "select-one", "select-multiple"].includes(input.type); | ||
|
||
return (event.type === "input" && inputIsSelectable) || | ||
(event.type === "change" && !inputIsSelectable); | ||
} | ||
|
||
// users can add or remove inputs | ||
captureDOMUpdate(event) { | ||
this.detectChanges(event.currentTarget, event.detail.newInputAdded); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/javascript/controllers/halt_navigation_on_change_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = ["form", "modalTemplate"] | ||
static values = { | ||
modalContainerId: String, | ||
discardOptionId: String, | ||
} | ||
|
||
// click-based | ||
displayModalOnChange(event) { | ||
if (this.formTarget.changed) { | ||
event.preventDefault(); | ||
const modal = this.modal; | ||
this.prepareDiscardOption(modal, event.detail.url); | ||
this.addModalToDocument(modal); | ||
} | ||
} | ||
|
||
// Modal is added out of this controller scope | ||
prepareDiscardOption(modal, targetLocation) { | ||
const discardOption = modal.querySelector(`#${this.discardOptionIdValue}`); | ||
discardOption.setAttribute("href", targetLocation); | ||
} | ||
|
||
addModalToDocument(modal) { | ||
const modalContainer = document.getElementById(this.modalContainerIdValue); | ||
modalContainer.appendChild(modal); | ||
} | ||
|
||
get modal() { | ||
const modalFragment = this.modalTemplateTarget.content; | ||
return document.importNode(modalFragment, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["dialog", "backdrop"] | ||
|
||
close() { | ||
this.element.remove(); | ||
} | ||
|
||
closeAfterSubmit(event) { | ||
if (event.detail.success) { | ||
this.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class OrganizationFormPresenter | ||
# Make sure to assign to :data option | ||
def change_detection_form_container_setup | ||
{ | ||
controller: 'detect-form-changes halt-navigation-on-change', | ||
detect_form_changes_input_types_value: 'input, textarea, select', | ||
halt_navigation_on_change_modal_container_id_value: 'main-modal-container', | ||
halt_navigation_on_change_discard_option_id_value: 'discard-changes-option', | ||
action: 'turbo:before-visit@document->halt-navigation-on-change#displayModalOnChange' | ||
} | ||
end | ||
|
||
def change_detection_form_setup | ||
{ | ||
detect_form_changes_target: 'form', | ||
halt_navigation_on_change_target: 'form', | ||
action: "input->detect-form-changes#captureUserInput change->detect-form-changes#captureUserInput \ | ||
nested-form:domupdate->detect-form-changes#captureDOMUpdate" | ||
} | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= turbo_stream.update "main-modal-container" do %> | ||
<%= render "shared/main_modal" do %> | ||
<%= render( | ||
"alerts/form", | ||
alert: @alert, | ||
url: alert_path(@alert), | ||
method: :patch, | ||
filters: @filters, | ||
filters_list: @filters_list | ||
) %> | ||
<% end %> | ||
<% end %> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= turbo_stream.update "main-modal-container" do %> | ||
<%= render "shared/main_modal" do %> | ||
<%= render( | ||
"alerts/form", | ||
alert: @alert, | ||
url: alerts_path, | ||
method: :post, | ||
filters: @filters, | ||
filters_list: @filters_list | ||
) %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,10 @@ | |
<%= render "shared/flash_messages" %> | ||
</div> | ||
|
||
<%= turbo_frame_tag "modal" %> | ||
|
||
<%= yield %> | ||
</main> | ||
<%= render Footer::Component.new() %> | ||
<div id="main-modal-container"></div> | ||
|
||
<script src="https://cdn.jsdelivr.net/gh/stevenschobert/[email protected]/src/instafeed.min.js"></script> | ||
<script> (function(){ var s = document.createElement('script'); var h = document.querySelector('head') || document.body; s.src = 'https://acsbapp.com/apps/app/dist/js/app.js'; s.async = true; s.onload = function(){ acsbJS.init({ statementLink : '', footerHtml : '', hideMobile : false, hideTrigger : false, disableBgProcess : false, language : 'en', position : 'right', leadColor : '#146FF8', triggerColor : '#146FF8', triggerRadius : '50%', triggerPositionX : 'right', triggerPositionY : 'bottom', triggerIcon : 'people', triggerSize : 'bottom', triggerOffsetX : 20, triggerOffsetY : 20, mobile : { triggerSize : 'small', triggerPositionX : 'right', triggerPositionY : 'bottom', triggerOffsetX : 20, triggerOffsetY : 20, triggerRadius : '20' } }); }; h.appendChild(s); })();</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.