feat: list forms associated with a hostname

This commit is contained in:
Aravinth Manivannan 2022-12-31 01:27:45 +05:30
parent c2c139575d
commit a11e7a75ea
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,109 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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<Context>,
}
#[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<Vec<Form>> {
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<Host>,
) -> PageResult<impl Responder, List> {
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);
}

View File

@ -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);
}

View File

@ -0,0 +1,16 @@
{% extends 'base' %}{% block title %} List Forms {% endblock title %} {% block nav
%} {% include "auth_nav" %} {% endblock nav %} {% block main %}
<main class="sites__main">
<div class="sites__collection">
{% if payload|length > 0 %}
{% for form in payload %}
<a href="{{ form.view }}" class="site__container">{{ form.path }}</a>
{% endfor %}
{% else %}
<p class="sites__banner">Form is empty</p>
{% endif %}
</div>
</main>
{% endblock main %}