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