Skip to main content

rumtk_web/components/
job_loader.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::components::loader::{loader, Loader};
21use crate::components::sanitize::Sanitized;
22use crate::defaults::{DEFAULT_NO_TEXT, DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_ID};
23use crate::{rumtk_web_check_on_job, rumtk_web_get_text_item, rumtk_web_params_string, ComponentResult, RUMWebTemplateSafe, DEFAULT_HTMX_CHECK_TICKS, PARAMS_TICKS};
24use crate::{RUMWebTemplate, SharedAppState, URLParams, URLPath};
25use rumtk_core::strings::RUMString;
26
27#[derive(RUMWebTemplate, Debug)]
28#[template(
29    source = "
30        <div id='loader-{{job_id}}' class='centered container-default job-loader-{{css_class}}-container'>
31            {% if let Some(loader) = loader %}
32                <div class='centered' hx-get='/jobs/{{params_string}}' hx-trigger='every {{ticks}}' hx-swap='outerHTML' hx-target='#loader-{{job_id}}'>
33                    {{loader|safe}}
34                </div>
35            {% elif let Some(result) = results %}
36                {{result|safe}}
37            {% endif %}
38        </div>
39    ",
40    ext = "html"
41)]
42pub struct JobLoader {
43    job_id: RUMString,
44    ticks: RUMString,
45    results: Option<Sanitized>,
46    loader: Option<Loader>,
47    params_string: RUMString,
48    css_class: RUMString,
49}
50
51impl RUMWebTemplateSafe for JobLoader {}
52
53pub fn job_loader<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<JobLoader> {
54    let job_id = rumtk_web_get_text_item!(params, PARAMS_ID, DEFAULT_NO_TEXT).to_string();
55    let ticks = rumtk_web_get_text_item!(params, PARAMS_TICKS, DEFAULT_HTMX_CHECK_TICKS).to_string();
56    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
57    let params_string = rumtk_web_params_string!(&params);
58
59    match rumtk_web_check_on_job!(&job_id, state) {
60        Some(r) => {
61            Ok(JobLoader {
62                job_id,
63                ticks,
64                results: Some(r),
65                loader: None,
66                params_string,
67                css_class
68            })
69        },
70        None => {
71            let loader = loader(_path_components, params, state)?;
72
73            Ok(JobLoader {
74                job_id,
75                ticks,
76                results: None,
77                loader: Some(loader),
78                params_string,
79                css_class
80            })
81        }
82    }
83}