Skip to content

Commit

Permalink
fix #33: check if resource is availabe externally before showing dele…
Browse files Browse the repository at this point in the history
…te button, remove log from pagebase
  • Loading branch information
PetoMPP committed Dec 22, 2023
1 parent 5305ced commit e286e5d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
56 changes: 46 additions & 10 deletions src/components/organisms/editor/atoms/delete_button.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
api::client::ApiClient,
async_event,
components::atoms::modal::{show_modal_callback, Buttons, ModalButton, ModalData, ModalStore},
components::{
atoms::modal::{show_modal_callback, Buttons, ModalButton, ModalData, ModalStore},
state::State,
},
data::{
locales::{store::LocalesStore, tk::TK},
resources::id::ResId,
Expand All @@ -11,7 +14,7 @@ use crate::{
router::route::Route,
};
use petompp_web_models::models::country::Country;
use yew::prelude::*;
use yew::{platform::spawn_local, prelude::*};
use yew_router::prelude::*;
use yewdux::prelude::*;

Expand All @@ -21,16 +24,46 @@ pub fn delete_button(props: &EditorProps) -> Html {
let (locales_store, _) = use_store::<LocalesStore>();
let (_, modal_dispatch) = use_store::<ModalStore>();
let navigator = use_navigator().unwrap();
let err = use_state(|| None);
let state = use_state(|| State::Ok(false));
let onstatechange = &props.onstatechanged;
let token = session_store.token.clone().unwrap_or_default();
{
let props = props.clone();
let token = token.clone();
let state = state.clone();
let onstatechange = onstatechange.clone();
use_effect_with_deps(
move |props| {
let Some(resid) = &props.resid else {
return;
};
if let EditorState::Loading = &props.state {
return;
}
let resid = resid.clone();
state.set(State::Loading);
spawn_local(async move {
match ApiClient::get_res_ids(token.as_str()).await {
Ok((res, pos)) => match &resid {
ResId::Blob(_) => state.set(State::Ok(pos.contains(&resid))),
ResId::ResKey(_) => state.set(State::Ok(res.contains(&resid))),
},
Err(e) => {
onstatechange.emit(EditorState::Err(e));
}
}
});
},
props.clone(),
);
}
let (Some(resid), Some(lang)) = (&props.resid, &props.lang) else {
return html! {};
return Html::default();
};
if let EditorState::Loading = &props.state {
return html! {};
return Html::default();
}
let onstatechange = &props.onstatechanged;
let token = session_store.token.clone().unwrap_or_default();
let onclick = async_event!(|onstatechange, navigator, resid, lang, err, token| {
let onclick = async_event!(|onstatechange, navigator, resid, lang, state, token| {
onstatechange.emit(EditorState::Loading);
match match resid {
ResId::Blob(id) => ApiClient::delete_post(&id, lang.key(), &token).await,
Expand All @@ -40,7 +73,7 @@ pub fn delete_button(props: &EditorProps) -> Html {
},
} {
Ok(_) => navigator.push(&Route::Editor),
Err(e) => err.set(Some(e)),
Err(e) => state.set(State::Err(e)),
}
});
let onclick = show_modal_callback(
Expand All @@ -54,12 +87,15 @@ pub fn delete_button(props: &EditorProps) -> Html {
},
modal_dispatch.clone(),
);
if let Some(e) = &*err {
if let State::Err(e) = &*state {
if let Err(redirect) = e.handle_failed_auth(session_dispatch) {
return redirect;
}
gloo::dialogs::alert(e.to_string().as_str());
}
if &*state != &State::Ok(true) {
return html! {};
}
html! {
<button {onclick} class={"flex btn btn-error"}>{locales_store.get(TK::Delete)}</button>
}
Expand Down
1 change: 0 additions & 1 deletion src/pages/page_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub fn page_base(props: &PageBaseProps) -> Html {
true => "PetoMPP.NET".to_string(),
false => format!("{} - PetoMPP.NET", title),
};
gloo::console::info!(format!("Setting title to {}", &title));
web_sys::window()
.and_then(|w| w.document())
.unwrap()
Expand Down

0 comments on commit e286e5d

Please sign in to comment.