refactor: make the project form component handle the server calls itself
This commit is contained in:
parent
ce7dafb576
commit
9324d841fb
@ -1,5 +1,5 @@
|
||||
use crate::components::project_form::ProjectForm;
|
||||
use crate::server::projects::create_project;
|
||||
use crate::server::projects::{create_project, testing};
|
||||
use dioxus::core_macro::rsx;
|
||||
use dioxus::dioxus_core::Element;
|
||||
use dioxus::prelude::*;
|
||||
@ -7,12 +7,6 @@ use dioxus::prelude::*;
|
||||
#[component]
|
||||
pub(crate) fn Home() -> Element {
|
||||
rsx! {
|
||||
ProjectForm {
|
||||
onsubmit: move |title| {
|
||||
spawn(async move {
|
||||
create_project(title).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
ProjectForm {}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,20 @@
|
||||
use crate::models::project::NewProject;
|
||||
use crate::server::projects::create_project;
|
||||
use dioxus::core_macro::{component, rsx};
|
||||
use dioxus::dioxus_core::Element;
|
||||
use dioxus::prelude::*;
|
||||
|
||||
#[component]
|
||||
pub(crate) fn ProjectForm(onsubmit: EventHandler<NewProject>) -> Element {
|
||||
pub(crate) fn ProjectForm() -> Element {
|
||||
rsx! {
|
||||
form {
|
||||
onsubmit: move |event| {
|
||||
onsubmit(NewProject::new(event.values().get("title").unwrap().as_value()));
|
||||
async move {
|
||||
let new_project = NewProject::new(
|
||||
event.values().get("title").unwrap().as_value()
|
||||
);
|
||||
let _ = create_project(new_project).await;
|
||||
}
|
||||
},
|
||||
input {
|
||||
r#type: "text",
|
||||
|
@ -7,7 +7,7 @@ use validator::{ValidationErrors, ValidationErrorsKind};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum ProjectCreateError {
|
||||
TitleTooShort,
|
||||
TitleLengthInvalid,
|
||||
Error(Error),
|
||||
}
|
||||
|
||||
@ -16,17 +16,18 @@ impl From<ValidationErrors> for ErrorVec<ProjectCreateError> {
|
||||
e.errors()
|
||||
.iter()
|
||||
.flat_map(|(&field, error_kind)| match field {
|
||||
"title_length" => match error_kind {
|
||||
"title" => match error_kind {
|
||||
ValidationErrorsKind::Field(validation_errors) => validation_errors
|
||||
.iter()
|
||||
.map(|validation_error| match validation_error.code.as_ref() {
|
||||
"length" => ProjectCreateError::TitleTooShort,
|
||||
_ => ProjectCreateError::Error(Error::ServerInternal),
|
||||
.map(|validation_error| validation_error.code.as_ref())
|
||||
.map(|code| match code {
|
||||
"title_length" => ProjectCreateError::TitleLengthInvalid,
|
||||
_ => panic!("unexpected validation error code: {code}"),
|
||||
})
|
||||
.collect::<Vec<ProjectCreateError>>(),
|
||||
_ => panic!("unexpected error kind"),
|
||||
_ => panic!("unexpected validation error kind"),
|
||||
},
|
||||
_ => panic!("unexpected field name"),
|
||||
_ => panic!("unexpected validation field name: {field}"),
|
||||
})
|
||||
.collect::<Vec<ProjectCreateError>>()
|
||||
.into()
|
||||
@ -36,7 +37,7 @@ impl From<ValidationErrors> for ErrorVec<ProjectCreateError> {
|
||||
// has to be implemented for Dioxus server functions
|
||||
impl Display for ProjectCreateError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", dbg!(self))
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +45,7 @@ impl Display for ProjectCreateError {
|
||||
impl FromStr for ProjectCreateError {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(ProjectCreateError::Error(Error::ServerInternal))
|
||||
fn from_str(_: &str) -> Result<Self, Self::Err> {
|
||||
Ok(ProjectCreateError::TitleLengthInvalid)
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use validator::Validate;
|
||||
const TITLE_LENGTH_MIN: u64 = 1;
|
||||
const TITLE_LENGTH_MAX: u64 = 255;
|
||||
|
||||
#[derive(Queryable, Selectable, Serialize, Deserialize)]
|
||||
#[derive(Queryable, Selectable, Serialize, Deserialize, Debug)]
|
||||
#[diesel(table_name = crate::schema::projects)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Project {
|
||||
|
@ -7,6 +7,11 @@ use diesel::{RunQueryDsl, SelectableHelper};
|
||||
use dioxus::prelude::*;
|
||||
use validator::Validate;
|
||||
|
||||
#[server]
|
||||
pub(crate) async fn testing(input: i32) -> Result<i32, ServerFnError> {
|
||||
Ok(input + 1)
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub(crate) async fn create_project(
|
||||
new_project: NewProject,
|
||||
|
Loading…
x
Reference in New Issue
Block a user