refactor: make the project form component handle the server calls itself

This commit is contained in:
Matouš Volf
2024-08-18 21:17:31 +02:00
parent 1656177e66
commit c537fb4e03
5 changed files with 27 additions and 21 deletions

View File

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