feat: port list campaigns to tera
This commit is contained in:
parent
67d50a3d80
commit
599777cbf3
2 changed files with 114 additions and 46 deletions
|
@ -13,23 +13,47 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
|
use actix_web::http::header::ContentType;
|
||||||
use actix_web::{HttpResponse, Responder};
|
use actix_web::{HttpResponse, Responder};
|
||||||
use my_codegen::get;
|
use serde::{Deserialize, Serialize};
|
||||||
use sailfish::TemplateOnce;
|
use tera::Context;
|
||||||
|
|
||||||
use crate::api::v1::admin::campaigns::{
|
use crate::api::v1::admin::campaigns::{
|
||||||
runners::list_campaign_runner, ListCampaignResp,
|
runners::list_campaign_runner, ListCampaignResp,
|
||||||
};
|
};
|
||||||
|
use crate::pages::errors::*;
|
||||||
use crate::AppData;
|
use crate::AppData;
|
||||||
use crate::PAGES;
|
use crate::Settings;
|
||||||
|
|
||||||
pub mod about;
|
pub mod about;
|
||||||
pub mod bench;
|
pub mod bench;
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
pub mod new;
|
pub mod new;
|
||||||
|
|
||||||
|
pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
|
||||||
|
|
||||||
|
pub fn register_templates(t: &mut tera::Tera) {
|
||||||
|
for template in [
|
||||||
|
CAMPAIGNS,
|
||||||
|
about::INTRO,
|
||||||
|
new::NEW_CAMPAIGN,
|
||||||
|
new::NEW_CAMPAIGN_FORM,
|
||||||
|
bench::BENCH,
|
||||||
|
delete::SUDO_DELETE,
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
{
|
||||||
|
template.register(t).expect(template.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod routes {
|
pub mod routes {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||||
pub struct Campaigns {
|
pub struct Campaigns {
|
||||||
pub home: &'static str,
|
pub home: &'static str,
|
||||||
pub new: &'static str,
|
pub new: &'static str,
|
||||||
|
@ -69,40 +93,86 @@ pub mod routes {
|
||||||
|
|
||||||
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
||||||
cfg.service(home);
|
cfg.service(home);
|
||||||
cfg.service(new::new_campaign);
|
about::services(cfg);
|
||||||
cfg.service(new::new_campaign_submit);
|
new::services(cfg);
|
||||||
cfg.service(about::about);
|
bench::services(cfg);
|
||||||
cfg.service(bench::bench);
|
delete::services(cfg);
|
||||||
cfg.service(delete::delete_campaign);
|
|
||||||
cfg.service(delete::delete_campaign_submit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(TemplateOnce)]
|
pub use super::*;
|
||||||
#[template(path = "panel/campaigns/index.html")]
|
|
||||||
struct HomePage {
|
pub struct Campaigns {
|
||||||
data: Vec<ListCampaignResp>,
|
ctx: RefCell<Context>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HomePage {
|
pub const CAMPAIGNS: TemplateFile =
|
||||||
fn new(data: Vec<ListCampaignResp>) -> Self {
|
TemplateFile::new("campaigns", "panel/campaigns/index.html");
|
||||||
Self { data }
|
|
||||||
|
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TemplateCampaign {
|
||||||
|
pub name: String,
|
||||||
|
pub uuid: String,
|
||||||
|
pub route: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ListCampaignResp> for TemplateCampaign {
|
||||||
|
fn from(c: ListCampaignResp) -> Self {
|
||||||
|
let route = crate::PAGES.panel.campaigns.get_about_route(&c.uuid);
|
||||||
|
let uuid = c.uuid;
|
||||||
|
let name = c.name;
|
||||||
|
Self { route, name, uuid }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const PAGE: &str = "Campaigns";
|
impl CtxError for Campaigns {
|
||||||
|
fn with_error(&self, e: &ReadableError) -> String {
|
||||||
|
self.ctx.borrow_mut().insert(ERROR_KEY, e);
|
||||||
|
|
||||||
#[get(
|
self.render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Campaigns {
|
||||||
|
pub fn new(settings: &Settings, payload: Option<Vec<TemplateCampaign>>) -> Self {
|
||||||
|
let ctx = RefCell::new(context(settings, "Campaigns"));
|
||||||
|
if let Some(payload) = payload {
|
||||||
|
if !payload.is_empty() {
|
||||||
|
ctx.borrow_mut().insert(PAYLOAD_KEY, &payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self { ctx }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&self) -> String {
|
||||||
|
TEMPLATES
|
||||||
|
.render(CAMPAIGNS.name, &self.ctx.borrow())
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn page(s: &Settings) -> String {
|
||||||
|
let p = Self::new(s, None);
|
||||||
|
p.render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web_codegen_const_routes::get(
|
||||||
path = "PAGES.panel.campaigns.home",
|
path = "PAGES.panel.campaigns.home",
|
||||||
wrap = "crate::pages::get_page_check_login()"
|
wrap = "crate::pages::get_page_check_login()"
|
||||||
)]
|
)]
|
||||||
pub async fn home(data: AppData, id: Identity) -> impl Responder {
|
pub async fn home(data: AppData, id: Identity) -> PageResult<impl Responder, Campaigns> {
|
||||||
let username = id.identity().unwrap();
|
let username = id.identity().unwrap();
|
||||||
let campaigns = list_campaign_runner(&username, &data).await.unwrap();
|
let mut campaigns = list_campaign_runner(&username, &data)
|
||||||
let page = HomePage::new(campaigns).render_once().unwrap();
|
.await
|
||||||
|
.map_err(|e| PageError::new(Campaigns::new(&data.settings, None), e))?;
|
||||||
|
let mut template_campaigns = Vec::with_capacity(campaigns.len());
|
||||||
|
for c in campaigns.drain(0..) {
|
||||||
|
template_campaigns.push(c.into())
|
||||||
|
}
|
||||||
|
|
||||||
HttpResponse::Ok()
|
let list_campaigns =
|
||||||
.content_type("text/html; charset=utf-8")
|
Campaigns::new(&data.settings, Some(template_campaigns)).render();
|
||||||
.body(page)
|
let html = ContentType::html();
|
||||||
|
Ok(HttpResponse::Ok().content_type(html).body(list_campaigns))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -145,7 +215,7 @@ mod tests {
|
||||||
const EMAIL: &str = "templateuser@surveyuserpages.com";
|
const EMAIL: &str = "templateuser@surveyuserpages.com";
|
||||||
const CAMPAIGN_NAME: &str = "delcappageusercamaping";
|
const CAMPAIGN_NAME: &str = "delcappageusercamaping";
|
||||||
|
|
||||||
let data = Data::new().await;
|
let data = get_test_data().await;
|
||||||
{
|
{
|
||||||
delete_user(NAME, &data).await;
|
delete_user(NAME, &data).await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,8 @@
|
||||||
<. include!("../../components/base/top.html"); .>
|
{% extends 'base' %}
|
||||||
|
{% block body %}
|
||||||
<body class="panel__body">
|
<body class="panel__body">
|
||||||
<. include!("../nav/index.html"); .>
|
|
||||||
<main class="panel__container">
|
<main class="panel__container">
|
||||||
<. if data.is_empty() { .>
|
{% if payload %}
|
||||||
<p>
|
|
||||||
Looks like you don't have any campaings registered,
|
|
||||||
<a class="link__btn" href="<.= crate::PAGES.panel.campaigns.new .>"> click here </a>
|
|
||||||
to create a new campaign!
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<. } else { .>
|
|
||||||
<table class="campaign__table">
|
<table class="campaign__table">
|
||||||
<thead class="campaign__heading">
|
<thead class="campaign__heading">
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -18,25 +11,30 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="campaign__body">
|
<tbody class="campaign__body">
|
||||||
<. for campaign in data.iter() { .>
|
{% for campaign in payload %}
|
||||||
<. let route = crate::PAGES.panel.campaigns.get_about_route(&campaign.uuid); .>
|
|
||||||
<tr class="campaign__item">
|
<tr class="campaign__item">
|
||||||
<td>
|
<td>
|
||||||
<a href="<.= &route .>">
|
<a href="{{ campaign.route }}">
|
||||||
<p class="campaign__item-heading"><.= campaign.name .></p>
|
<p class="campaign__item-heading">{{ campaign.name }}</p>
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="<.= &route .>">
|
<a href="{{ campaign.route }}">
|
||||||
<p class="campaign__item-text"><.= campaign.uuid .></p></a
|
<p class="campaign__item-text">{{ campaign.uuid }}</p></a
|
||||||
>
|
>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<. } .>
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<. } .>
|
|
||||||
|
{% else %}
|
||||||
|
<p>
|
||||||
|
Looks like you don't have any campaigns registered,
|
||||||
|
<a class="link__btn" href="{{ page.panel.campaigns.new }}"> click here </a>
|
||||||
|
to create a new campaign!
|
||||||
|
</p>
|
||||||
|
{% endif %}
|
||||||
</main>
|
</main>
|
||||||
<. include!("../../components/footer/index.html"); .>
|
|
||||||
</body>
|
</body>
|
||||||
<. include!("../../components/base/bottom.html"); .>
|
{% endblock body %}
|
||||||
|
|
Loading…
Reference in a new issue