From a11e7a75ea5a931a8ba310831be142b72a15cc39 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 31 Dec 2022 01:27:45 +0530 Subject: [PATCH] feat: list forms associated with a hostname --- src/pages/dash/sites/forms/list.rs | 109 ++++++++++++++++++ src/pages/dash/sites/mod.rs | 3 + .../pages/dash/sites/forms/host-list.html | 16 +++ 3 files changed, 128 insertions(+) create mode 100644 src/pages/dash/sites/forms/list.rs create mode 100644 templates/pages/dash/sites/forms/host-list.html diff --git a/src/pages/dash/sites/forms/list.rs b/src/pages/dash/sites/forms/list.rs new file mode 100644 index 0000000..a05d7e0 --- /dev/null +++ b/src/pages/dash/sites/forms/list.rs @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::cell::RefCell; + +use actix_identity::Identity; +use actix_web::http::header::ContentType; +use serde::{Deserialize, Serialize}; + +use tera::Context; + +use super::get_auth_middleware; +use crate::api::v1::forms::Host; +use crate::errors::ServiceResult; +use crate::pages::errors::*; +use crate::settings::Settings; +use crate::AppCtx; + +pub use super::*; + +pub const DASH_SITE_FORM_HOST_LIST: TemplateFile = TemplateFile::new( + "dash_site_form_host_list", + "pages/dash/sites/forms/host-list.html", +); + +pub struct List { + ctx: RefCell, +} + +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] +pub struct Form { + pub path: String, + pub view: String, +} + +impl Form { + pub fn new(path: String, host: &str) -> Self { + let view = PAGES.dash.site.forms.get_view(0, host, &path); + Self { path, view } + } +} + +impl CtxError for List { + fn with_error(&self, e: &ReadableError) -> String { + self.ctx.borrow_mut().insert(ERROR_KEY, e); + self.render() + } +} + +impl List { + pub fn new(settings: &Settings, forms: Option<&[Form]>) -> Self { + let ctx = RefCell::new(context(settings)); + if let Some(forms) = forms { + ctx.borrow_mut().insert(PAYLOAD_KEY, forms); + } + Self { ctx } + } + + pub fn render(&self) -> String { + TEMPLATES + .render(DASH_SITE_FORM_HOST_LIST.name, &self.ctx.borrow()) + .unwrap() + } +} + +async fn get_forms(ctx: &AppCtx, id: &Identity, host: &str) -> ServiceResult> { + let owner = id.identity().unwrap(); + let mut db_forms = ctx.get_all_forms_for_host(&owner, host).await?; + let mut forms = Vec::with_capacity(db_forms.len()); + for form in db_forms.drain(0..) { + forms.push(Form::new(form, host)); + } + Ok(forms) +} + +#[actix_web_codegen_const_routes::get( + path = "PAGES.dash.site.forms.list_forms", + wrap = "get_auth_middleware()" +)] +#[tracing::instrument(name = "List all forms belonging to host" skip(ctx, id))] +pub async fn list_forms( + ctx: AppCtx, + id: Identity, + query: web::Query, +) -> PageResult { + let sites = get_forms(&ctx, &id, &query.host) + .await + .map_err(|e| PageError::new(List::new(&ctx.settings, None), e))?; + let home = List::new(&ctx.settings, Some(&sites)).render(); + let html = ContentType::html(); + Ok(HttpResponse::Ok().content_type(html).body(home)) +} + +pub fn services(cfg: &mut web::ServiceConfig) { + cfg.service(list_forms); +} diff --git a/src/pages/dash/sites/mod.rs b/src/pages/dash/sites/mod.rs index 2a7621e..433e7b4 100644 --- a/src/pages/dash/sites/mod.rs +++ b/src/pages/dash/sites/mod.rs @@ -22,6 +22,7 @@ pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES}; pub mod add; pub mod delete; +pub mod forms; pub mod view; pub fn register_templates(t: &mut tera::Tera) { @@ -34,10 +35,12 @@ pub fn register_templates(t: &mut tera::Tera) { delete::DASH_SITE_DELETE .register(t) .expect(delete::DASH_SITE_DELETE.name); + forms::register_templates(t); } pub fn services(cfg: &mut web::ServiceConfig) { add::services(cfg); view::services(cfg); delete::services(cfg); + forms::services(cfg); } diff --git a/templates/pages/dash/sites/forms/host-list.html b/templates/pages/dash/sites/forms/host-list.html new file mode 100644 index 0000000..540f556 --- /dev/null +++ b/templates/pages/dash/sites/forms/host-list.html @@ -0,0 +1,16 @@ +{% extends 'base' %}{% block title %} List Forms {% endblock title %} {% block nav +%} {% include "auth_nav" %} {% endblock nav %} {% block main %} + +
+
+ {% if payload|length > 0 %} + {% for form in payload %} + {{ form.path }} + {% endfor %} + {% else %} +

Form is empty

+ {% endif %} +
+
+ +{% endblock main %}