mirror of
https://github.com/meichthys/church.git
synced 2026-02-12 11:33:43 +00:00
Fetch chapter and verse counts from api and rename verses when edited
This commit is contained in:
parent
7e3a5da870
commit
a261fb8b90
@ -1,8 +1,76 @@
|
||||
// Copyright (c) 2025, meichthys and contributors
|
||||
// For license information, please see license.txt
|
||||
|
||||
// frappe.ui.form.on("Church Bible Verse", {
|
||||
// refresh(frm) {
|
||||
frappe.ui.form.on('Church Bible Verse', {
|
||||
// Automatically rename the document before saving
|
||||
after_save: async function(frm) {
|
||||
try {
|
||||
// Call backend to rename the document
|
||||
const new_name = await frm.call("rename_verse");
|
||||
|
||||
// },
|
||||
// });
|
||||
if (new_name.message && new_name.message !== frm.doc.name) {
|
||||
// Navigate to the new form
|
||||
frappe.set_route("Form", "Church Bible Verse", new_name.message);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
frappe.msgprint(`❌ Error renaming verse: ${err.message}`);
|
||||
}
|
||||
},
|
||||
// Dynamically set select values for chapter and verse fields
|
||||
// Alternatively, we could store the number of chapters and verses for each book/chatper
|
||||
book: async function(frm) {
|
||||
// Set options for chapter field
|
||||
try{
|
||||
let chapters = await get_chapter_count(frm.doc.book)
|
||||
let options = Array.from({ length: chapters }, (_, i) => i + 1).join('\n');
|
||||
frm.set_df_property('chapter', 'options', options);
|
||||
} catch (e) {
|
||||
frm.set_df_property('chapter', 'options', Array.from({ length: 150}, (_, i) => i + 1).join('\n'));
|
||||
}
|
||||
},
|
||||
chapter: async function(frm) {
|
||||
// Set options for verse field
|
||||
try {
|
||||
let verses = await get_verse_count(frm.doc.book, frm.doc.chapter)
|
||||
let options = Array.from({ length: verses }, (_, i) => i + 1).join('\n');
|
||||
frm.set_df_property('verse', 'options', options);
|
||||
} catch (e) {
|
||||
frm.set_df_property('verse', 'options', Array.from({ length: 176 }, (_, i) => i + 1).join('\n'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async function get_chapter_count(book) {
|
||||
const book_doc = await frappe.db.get_doc("Church Bible Book", book);
|
||||
let book_abbreviation = book_doc.abbreviation
|
||||
const url = `https://bible-api.com/data/kjv/${book_abbreviation}`;
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) {
|
||||
frappe.show_alert(`Failed to fetch chapter count for ${book}`, indicator="yellow")
|
||||
}
|
||||
const data = await resp.json();
|
||||
const chapters = data.chapters;
|
||||
if (!Array.isArray(chapters)) {
|
||||
frappe.show_alert(`Failed to fetch chapter count for ${book}`, indicator="yellow")
|
||||
}
|
||||
return chapters.length;
|
||||
}
|
||||
|
||||
async function get_verse_count(book, chapter) {
|
||||
const book_doc = await frappe.db.get_doc("Church Bible Book", book);
|
||||
let book_abbreviation = book_doc.abbreviation
|
||||
const url = `https://bible-api.com/data/kjv/${book_abbreviation}/${chapter}`;
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) {
|
||||
frappe.show_alert({message:`Failed to fetch verse count for ${book}`, indicator:"yellow"},5)
|
||||
throw new Error("Failed to fetch verse count");
|
||||
}
|
||||
const data = await resp.json();
|
||||
const verses = data.verses;
|
||||
if (!Array.isArray(verses)) {
|
||||
frappe.show_alert({message:`Failed to fetch verse count for ${book}`, indicator:"yellow"},5)
|
||||
}
|
||||
return verses.length;
|
||||
}
|
||||
@ -1,9 +1,24 @@
|
||||
# Copyright (c) 2025, meichthys and contributors
|
||||
# For license information, please see license.txt
|
||||
|
||||
# import frappe
|
||||
import frappe
|
||||
from frappe.model.document import Document
|
||||
|
||||
|
||||
class ChurchBibleVerse(Document):
|
||||
pass
|
||||
|
||||
@frappe.whitelist()
|
||||
def rename_verse(self):
|
||||
"""
|
||||
Rename Church Bible Verse document to 'Book Chapter:Verse'.
|
||||
Returns the new name.
|
||||
"""
|
||||
doc = frappe.get_doc("Church Bible Verse", self.name)
|
||||
new_name = f"{doc.book} {doc.chapter}:{doc.verse}"
|
||||
|
||||
# Only rename if different
|
||||
if doc.name != new_name:
|
||||
frappe.rename_doc("Church Bible Verse", doc.name, new_name)
|
||||
|
||||
return new_name
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user