-
Notifications
You must be signed in to change notification settings - Fork 10
T2690 - Selectable prewritten texts #182
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
Open
Dylanvenancio
wants to merge
1
commit into
14.0
Choose a base branch
from
T2690
base: 14.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
92 changes: 59 additions & 33 deletions
92
my_compassion/static/src/js/my2_letter_template_loader.js
This file contains hidden or 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 |
|---|---|---|
| @@ -1,43 +1,69 @@ | ||
| /** | ||
| * Handles the selection of template images by adding an ID to the clicked image | ||
| * and removing it from any previously selected image. | ||
| * | ||
| * Is used in /templates/pages/my2_new_letter.xml | ||
| * Fetches a letter template from the server and populates the text input field with it. | ||
| * | ||
| * Used in /templates/pages/my2_new_letter.xml | ||
| */ | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
| const textInput = document.getElementById("letter-input"); | ||
| const childSelector = document.getElementById("child-dropdown"); | ||
| const TEMPLATE_URL = `/my2/children/letter/templates`; | ||
| // Open the modal when the select element is clicked | ||
| document.getElementById("letter_template").addEventListener("click", function (event) { | ||
| event.preventDefault(); | ||
| $("#textTemplateSelectionModal").modal("show"); | ||
| }); | ||
| document.body.addEventListener("click", function (event) { | ||
| const clickedButton = event.target.closest(".text-template-item"); | ||
|
|
||
| const fetchTemplate = (childId) => { | ||
| if (!textInput) { | ||
| console.error("Required HTML element #letter-input is missing."); | ||
| return; | ||
| } | ||
| fetch(`${TEMPLATE_URL}?child_id=${childId}`) | ||
| .then((response) => { | ||
| if (!response.ok) { | ||
| throw new Error("Network response was not ok"); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .then((data) => { | ||
| if (data && data.template_text) { | ||
| textInput.value = data.template_text; | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Failed to fetch template:", error); | ||
| }); | ||
| }; | ||
| if (clickedButton) { | ||
| const templateId = clickedButton.dataset.templateId; | ||
| if (!templateId) { | ||
| console.error("'data-template-id' is undefined."); | ||
| return; | ||
| } | ||
| // Store the template ID in a hidden input for later use if needed | ||
| const hiddenInput = document.getElementById("selected_template_id"); | ||
| if (hiddenInput) { | ||
| hiddenInput.value = templateId; | ||
| } else { | ||
| console.error("Could not find '#selected_template_id' to store template ID."); | ||
| } | ||
|
|
||
| const childSelector = document.getElementById("child-dropdown"); | ||
| if (!childSelector) { | ||
| console.error("Could not find '#child-dropdown'."); | ||
| return; | ||
| } | ||
| const childId = childSelector.value; | ||
|
|
||
| const TEMPLATE_URL = `/my2/children/letter/templates`; | ||
|
|
||
| if (childSelector) { | ||
| // Fetch template on initial load only if input is empty | ||
| if (!textInput.value) { | ||
| fetchTemplate(childSelector.value); | ||
| fetch(`${TEMPLATE_URL}?child_id=${childId}&template_id=${templateId}`) | ||
| .then((response) => { | ||
| // 1. Open curly brace | ||
| if (!response.ok) { | ||
| throw new Error("Network response was not ok"); | ||
| } | ||
| return response.json(); // 2. Explicit return | ||
| }) // 3. Close curly brace | ||
| .then((data) => { | ||
| if (data && data.template_text) { | ||
| const targetInput = document.getElementById("letter-input"); | ||
| if (targetInput) { | ||
| targetInput.value = data.template_text; | ||
| // Trigger input event so framework/listeners detect the change | ||
| targetInput.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); | ||
| } else { | ||
| console.error("Could not find '#letter-input' for insertion."); | ||
| } | ||
| } else { | ||
| console.error("Fetch did not return 'template_text'."); | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Fetch failed:", error); // Fixed typo "etch" | ||
| }); | ||
| } | ||
| // Re-fetch template when child selection changes | ||
| childSelector.addEventListener("change", (event) => { | ||
| fetchTemplate(event.target.value); | ||
| }); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.