1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2025-01-24 02:45:49 +00:00

Various fixes for profile editing

This commit is contained in:
Felix Ableitner 2025-01-13 15:37:02 +01:00
parent 3cdb7d09ea
commit a6b11d6395
6 changed files with 36 additions and 16 deletions

View file

@ -126,3 +126,8 @@ pub async fn edit_list(
};
Ok(Json(DbEdit::view(params, &data)?))
}
/// Trims the string param, and converts to None if it is empty
fn empty_to_none(val: &mut Option<String>) {
(*val) = val.as_ref().map(|s| s.trim().to_owned());
}

View file

@ -1,4 +1,4 @@
use super::check_is_admin;
use super::{check_is_admin, empty_to_none};
use crate::{
backend::{
database::{conflict::DbConflict, read_jwt_secret, IbisData},
@ -147,8 +147,10 @@ pub(in crate::backend::api) async fn get_user(
#[debug_handler]
pub(in crate::backend::api) async fn update_user_profile(
data: Data<IbisData>,
Form(params): Form<UpdateUserForm>,
Form(mut params): Form<UpdateUserForm>,
) -> MyResult<Json<SuccessResponse>> {
empty_to_none(&mut params.display_name);
empty_to_none(&mut params.bio);
DbPerson::update_profile(&params, &data)?;
Ok(Json(SuccessResponse::default()))
}

View file

@ -222,7 +222,6 @@ impl ApiClient {
&self,
data: UpdateUserForm,
) -> Result<SuccessResponse, ServerFnError> {
log::info!("{:?}", &data);
self.post("/api/v1/account/update", Some(data)).await
}

View file

@ -7,7 +7,7 @@ use leptos_router::components::Redirect;
#[component]
pub fn Login() -> impl IntoView {
let (login_response, set_login_response) = signal(None::<()>);
let (login_response, set_login_response) = signal(false);
let (login_error, set_login_error) = signal(None::<String>);
let (wait_for_response, set_wait_for_response) = signal(false);
@ -22,7 +22,7 @@ pub fn Login() -> impl IntoView {
match result {
Ok(_res) => {
site().refetch();
set_login_response.update(|v| *v = Some(()));
set_login_response.set(true);
set_login_error.update(|e| *e = None);
}
Err(err) => {
@ -38,7 +38,7 @@ pub fn Login() -> impl IntoView {
view! {
<Show
when=move || login_response.get().is_some()
when=move || login_response.get()
fallback=move || {
view! {
<CredentialsForm

View file

@ -7,7 +7,7 @@ use log::info;
#[component]
pub fn Register() -> impl IntoView {
let (register_response, set_register_response) = signal(None::<()>);
let (register_response, set_register_response) = signal(false);
let (register_error, set_register_error) = signal(None::<String>);
let (wait_for_response, set_wait_for_response) = signal(false);
@ -17,19 +17,19 @@ pub fn Register() -> impl IntoView {
let credentials = RegisterUserForm { username, password };
info!("Try to register new account for {}", credentials.username);
async move {
set_wait_for_response.update(|w| *w = true);
set_wait_for_response.set(true);
let result = CLIENT.register(credentials).await;
set_wait_for_response.update(|w| *w = false);
set_wait_for_response.set(false);
match result {
Ok(_res) => {
site().refetch();
set_register_response.update(|v| *v = Some(()));
set_register_error.update(|e| *e = None);
set_register_response.set(true);
set_register_error.set(None);
}
Err(err) => {
let msg = err.to_string();
log::warn!("Unable to register new account: {msg}");
set_register_error.update(|e| *e = Some(msg));
set_register_error.set(Some(msg));
}
}
}
@ -39,7 +39,7 @@ pub fn Register() -> impl IntoView {
view! {
<Show
when=move || register_response.get().is_some()
when=move || register_response.get()
fallback=move || {
view! {
<CredentialsForm

View file

@ -9,6 +9,7 @@ use leptos::prelude::*;
#[component]
pub fn UserEditProfile() -> impl IntoView {
let (saved, set_saved) = signal(false);
let (submit_error, set_submit_error) = signal(None::<String>);
let submit_action = Action::new(move |form: &UpdateUserForm| {
@ -17,12 +18,14 @@ pub fn UserEditProfile() -> impl IntoView {
let result = CLIENT.update_user_profile(form).await;
match result {
Ok(_res) => {
set_submit_error.update(|e| *e = None);
site().refetch();
set_saved.set(true);
set_submit_error.set(None);
}
Err(err) => {
let msg = err.to_string();
log::warn!("Unable to update profile: {msg}");
set_submit_error.update(|e| *e = Some(msg));
set_submit_error.set(Some(msg));
}
}
}
@ -56,6 +59,7 @@ pub fn UserEditProfile() -> impl IntoView {
id="displayname"
class="w-80 input input-secondary input-bordered"
prop:value=display_name
value=display_name
on:change=move |ev| {
let val = event_target_value(&ev);
set_display_name.set(val);
@ -74,7 +78,9 @@ pub fn UserEditProfile() -> impl IntoView {
let val = evt.target().value();
set_bio.set(val);
}
></textarea>
>
bio.get()
</textarea>
</div>
<button
class="btn btn-primary"
@ -89,6 +95,14 @@ pub fn UserEditProfile() -> impl IntoView {
>
Submit
</button>
<Show when=move || saved.get()>
<div class="toast">
<div class="alert alert-info">
<span>Saved!</span>
</div>
</div>
</Show>
}
}