feat: port new campaign page to tera

This commit is contained in:
Aravinth Manivannan 2023-01-24 23:29:47 +05:30
parent e5bf7feebe
commit 8e6a3afb4c
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 63 additions and 62 deletions

View file

@ -14,56 +14,59 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * 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::HttpResponseBuilder; use actix_web::http::header;
use actix_web::{error::ResponseError, http::header, web, HttpResponse, Responder}; use actix_web::http::header::ContentType;
use lazy_static::lazy_static; use actix_web::{web, HttpResponse, Responder};
use my_codegen::{get, post}; use tera::Context;
use sailfish::TemplateOnce;
use crate::api::v1::admin::campaigns::{runners, AddCapmaign}; use crate::api::v1::admin::campaigns::{runners, AddCapmaign};
use crate::errors::*;
use crate::pages::errors::ErrorPage;
use crate::AppData; use crate::AppData;
use crate::PAGES;
#[derive(Clone, TemplateOnce)] pub use super::*;
#[template(path = "panel/campaigns/new/index.html")]
struct NewCampaign<'a> { pub struct NewCampaign {
error: Option<ErrorPage<'a>>, ctx: RefCell<Context>,
} }
const PAGE: &str = "New Campaign"; pub const NEW_CAMPAIGN: TemplateFile =
TemplateFile::new("new_campaign", "panel/campaigns/new/index.html");
pub const NEW_CAMPAIGN_FORM: TemplateFile =
TemplateFile::new("new_campaign_form", "panel/campaigns/new/form.html");
impl<'a> Default for NewCampaign<'a> { impl CtxError for NewCampaign {
fn default() -> Self { fn with_error(&self, e: &ReadableError) -> String {
NewCampaign { error: None } self.ctx.borrow_mut().insert(ERROR_KEY, e);
self.render()
} }
} }
impl<'a> NewCampaign<'a> { impl NewCampaign {
pub fn new(title: &'a str, message: &'a str) -> Self { pub fn new(settings: &Settings) -> Self {
Self { let ctx = RefCell::new(context(settings, "Login"));
error: Some(ErrorPage::new(title, message)), Self { ctx }
} }
pub fn render(&self) -> String {
TEMPLATES
.render(NEW_CAMPAIGN.name, &self.ctx.borrow())
.unwrap()
} }
} }
lazy_static! { #[actix_web_codegen_const_routes::get(
static ref INDEX: String = NewCampaign::default().render_once().unwrap();
}
#[get(
path = "PAGES.panel.campaigns.new", path = "PAGES.panel.campaigns.new",
wrap = "crate::pages::get_page_check_login()" wrap = "crate::pages::get_page_check_login()"
)] )]
pub async fn new_campaign() -> impl Responder { pub async fn new_campaign(data: AppData) -> PageResult<impl Responder, NewCampaign> {
HttpResponse::Ok() let new_campaign = NewCampaign::new(&data.settings).render();
.content_type("text/html; charset=utf-8") let html = ContentType::html();
.body(&*INDEX.as_str()) Ok(HttpResponse::Ok().content_type(html).body(new_campaign))
} }
#[post( #[actix_web_codegen_const_routes::post(
path = "PAGES.panel.campaigns.new", path = "PAGES.panel.campaigns.new",
wrap = "crate::pages::get_page_check_login()" wrap = "crate::pages::get_page_check_login()"
)] )]
@ -71,30 +74,23 @@ pub async fn new_campaign_submit(
id: Identity, id: Identity,
payload: web::Json<AddCapmaign>, payload: web::Json<AddCapmaign>,
data: AppData, data: AppData,
) -> PageResult<impl Responder> { ) -> PageResult<impl Responder, NewCampaign> {
let username = id.identity().unwrap(); let username = id.identity().unwrap();
let mut payload = payload.into_inner(); let mut payload = payload.into_inner();
match runners::add_runner(&username, &mut payload, &data).await { runners::add_runner(&username, &mut payload, &data)
Ok(_) => { .await
Ok(HttpResponse::Found() .map_err(|e| PageError::new(NewCampaign::new(&data.settings), e))?;
//TODO show stats of new campaign
.insert_header((header::LOCATION, PAGES.panel.campaigns.home))
.finish())
}
Err(e) => {
let status = e.status_code();
let heading = status.canonical_reason().unwrap_or("Error");
Ok(HttpResponseBuilder::new(status) Ok(HttpResponse::Found()
.content_type("text/html; charset=utf-8") //TODO show stats of new campaign
.body( .insert_header((header::LOCATION, PAGES.panel.campaigns.home))
NewCampaign::new(heading, &format!("{}", e)) .finish())
.render_once() }
.unwrap(),
)) pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
} cfg.service(new_campaign);
} cfg.service(new_campaign_submit);
} }
#[cfg(test)] #[cfg(test)]
@ -103,20 +99,19 @@ mod tests {
use super::*; use super::*;
use crate::data::Data;
use crate::tests::*; use crate::tests::*;
use crate::*; use crate::*;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
#[actix_rt::test] #[actix_rt::test]
async fn new_campaign_form_works() { async fn new_campaign_form_works() {
let data = Data::new().await;
const NAME: &str = "testusercampaignform"; const NAME: &str = "testusercampaignform";
const EMAIL: &str = "testcampaignuser@aaa.com"; const EMAIL: &str = "testcampaignuser@aaa.com";
const PASSWORD: &str = "longpassword"; const PASSWORD: &str = "longpassword";
const CAMPAIGN_NAME: &str = "testcampaignuser"; const CAMPAIGN_NAME: &str = "testcampaignuser";
let data = get_test_data().await;
let app = get_app!(data).await; let app = get_app!(data).await;
delete_user(NAME, &data).await; delete_user(NAME, &data).await;
let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await; let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await;

View file

@ -1,11 +1,11 @@
<h1>Create new campaigns</h1> <h1>Create new campaigns</h1>
<. include!("../../../components/error/index.html"); .>
<form <form
action="<.= crate::PAGES.panel.campaigns.new .>" action="<.= page.panel.campaigns.new .>"
method="POST" method="POST"
class="new-campaign__form" class="new-campaign__form"
accept-charset="utf-8" accept-charset="utf-8"
> >
{% include "error_comp" %}
<label class="form__label" for="name"> <label class="form__label" for="name">
Name Name
<input <input

View file

@ -1,7 +1,13 @@
<. include!("../../../components/base/top.html"); .> {% extends 'base' %}
<body class="panel__body">
<. include!("../../nav/index.html"); .> {% block nav %}
<main class="panel__container"><. include!("./form.html"); .></main> {% include "panel_nav" %}
<. include!("../../../components/footer/index.html"); .> {% endblock nav %}
</body>
<. include!("../../../components/base/bottom.html"); .> {% block body %}
<body class="panel__body">
<main class="panel__container">
{% include "new_campaign_form" %}
</main>
</body>
{% endblock body %}