Skip to main content

rumtk_web/components/
portrait_card.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) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use crate::components::contact_card::{contact_card, ContactCard};
22use crate::utils::defaults::{DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_SECTION, PARAMS_TYPE};
23use crate::utils::types::{SharedAppState, URLParams, URLPath};
24use crate::{rumtk_web_get_config, rumtk_web_get_config_string, rumtk_web_get_text_item, rumtk_web_params_map, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
25use rumtk_core::base::RUMResult;
26use rumtk_core::strings::RUMString;
27
28type PortraitGrid = Vec<Vec<ContactCard>>;
29
30#[derive(RUMWebTemplate, Debug)]
31#[template(
32    source = "
33        {% if custom_css_enabled %}
34            <link href='/static/components/portrait_card.css' rel='stylesheet'>
35        {% endif %}
36        <div class='centered twothird-width portrait-card-{{ css_class }}-container'>
37            <table>
38                <thead></thead>
39                <tbody>
40                {% for row in icon_data %}
41                    <tr class='portrait-card-{{ css_class }}-row'>
42                        {% for item in row %}
43                        <td class='portrait-card-{{ css_class }}-item'>
44                            {{item | safe}}
45                        </td>
46                        {% endfor %}
47                    </tr>
48                {% endfor %}
49                </tbody>
50            </table>
51        </div>
52    ",
53    ext = "html"
54)]
55pub struct PortraitCard {
56    icon_data: PortraitGrid,
57    css_class: RUMString,
58    custom_css_enabled: bool,
59}
60
61impl RUMWebTemplateSafe for PortraitCard {}
62
63fn get_portrait_grid<'a>(section: &'a str, typ: &'a str, app_state: &'a SharedAppState) -> RUMResult<PortraitGrid> {
64    let text_conf = rumtk_web_get_config_string!(app_state, typ);
65
66    let mut grid = Vec::with_capacity(text_conf.len());
67    for (_r_name, r_list) in text_conf {
68        let mut grid_row = Vec::with_capacity(r_list.len());
69        for (i_name, _i_item) in r_list {
70            let item_params = rumtk_web_params_map!([
71                    (PARAMS_SECTION, section),
72                    (PARAMS_TYPE, i_name.as_str()),
73            ]);
74            let item = contact_card(
75                &[],
76                item_params.get_inner(),
77                app_state.clone()
78            )?;
79            grid_row.push(item);
80        }
81        grid.push(grid_row);
82    }
83    Ok(grid)
84}
85
86pub fn portrait_card<'a>(
87    _path_components: URLPath<'a, 'a>,
88    params: URLParams<'a>,
89    state: SharedAppState,
90) -> ComponentResult<PortraitCard> {
91    let section = rumtk_web_get_text_item!(params, PARAMS_SECTION, DEFAULT_TEXT_ITEM);
92    let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_TEXT_ITEM);
93    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
94    let icon_data = get_portrait_grid(section, typ, &state)?;
95
96    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
97
98    Ok(PortraitCard {
99        icon_data,
100        css_class,
101        custom_css_enabled
102    })
103}