Skip to content

Commit

Permalink
Manual migration steps
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Oct 29, 2023
1 parent 0f77add commit a096ab9
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 97 deletions.
2 changes: 1 addition & 1 deletion app/src/api/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn use_entity<T: Entity>(entity: T) -> Memo<T> {
let id = Signal::derive(move || orig_entity.with_value(|w| w.get_id().clone()));

let entity_resource = use_entity_resource::<T>(id.get_untracked());
create_memo(move |_| match entity_resource.read().flatten() {
create_memo(move |_| match entity_resource().flatten() {
Some(entity) => entity,
None => orig_entity(),
})
Expand Down
12 changes: 6 additions & 6 deletions app/src/auth/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ impl Auth {
#[cfg(not(debug_assertions))]
LocalStorage::set(TOKEN_STORAGE_KEY, new_token.clone()).unwrap();

let navigate = use_navigate(self.cx);
self.cx.batch(|| {
let navigate = use_navigate();
batch(|| {
(self.set_token)(Token(Some(new_token)));
navigate("/", Default::default()).unwrap();
navigate("/", Default::default());
});
});
}
Expand All @@ -66,10 +66,10 @@ impl Auth {
#[cfg(not(debug_assertions))]
LocalStorage::delete(TOKEN_STORAGE_KEY);

let navigate = use_navigate(self.cx);
self.cx.batch(|| {
let navigate = use_navigate();
batch(|| {
(self.set_token)(Token(None));
navigate("/", Default::default()).unwrap();
navigate("/", Default::default());
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/auth/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub fn create_auth_guard() {
// workaround for navigating during initial routing
// https://docs.rs/leptos_router/0.4.2/leptos_router/fn.use_navigate.html#panics
request_animation_frame(move || {
navigate("/login", Default::default()).unwrap();
navigate("/login", Default::default());
});
}
});
let settings = create_settings_resource();
create_effect(move |_| {
if let Some(Err(_)) = settings.read() {
if let Some(Err(_)) = settings() {
let navigate = use_navigate();
// TODO handle other errors than "unauthenticated"
request_animation_frame(move || {
navigate("/login", Default::default()).unwrap();
navigate("/login", Default::default());
});
}
});
Expand Down
12 changes: 6 additions & 6 deletions app/src/components/add_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn AddButton() -> impl IntoView {
view! {
<Show
when=edit_mode
fallback=|_| ()
fallback=|| ()
>
<div class="absolute bottom-3 right-3">
<IconButton on:click=move |_| set_state.update(|prev| *prev = if *prev == State::Picking {
Expand All @@ -51,7 +51,7 @@ pub fn AddButton() -> impl IntoView {

<Show
when=move || state() == State::Picking
fallback=|_| ()
fallback=|| ()
>
<div class="absolute bottom-8 right-8
bg-slate-600 rounded p-4 border-2 border-white
Expand All @@ -62,15 +62,15 @@ pub fn AddButton() -> impl IntoView {
</div>
</Show>

<Show when=move || state().is_entity() fallback=|_| ()>
<Show when=move || state().is_entity() fallback=|| ()>
<Dialog on_close>
<Show when=move || state() == State::Page fallback=|_| ()>
<Show when=move || state() == State::Page fallback=|| ()>
<PageForm on_close />
</Show>
<Show when=move || state() == State::Widget fallback=|_| ()>
<Show when=move || state() == State::Widget fallback=|| ()>
<WidgetForm on_close />
</Show>
<Show when=move || state() == State::Bookmark fallback=|_| ()>
<Show when=move || state() == State::Bookmark fallback=|| ()>
<BookmarkForm on_close />
</Show>
</Dialog>
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn Bookmark(
<XMarkIcon />
</button>
</div>
<Show when=form_open fallback=|_| () >
<Show when=form_open fallback=|| () >
<Dialog on_close >
<BookmarkForm on_close prev_bookmark=bookmark.get_untracked() />
</Dialog>
Expand Down
49 changes: 20 additions & 29 deletions app/src/components/bookmark_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@ pub fn BookmarkForm<F: Fn() + Copy + 'static>(
if let Some(bookmark) = prev_bookmark() {
let all_widgets = use_entities::<Widget>();
create_effect(move |_| {
let p_id = all_widgets
.read()
let p_id = all_widgets()
.unwrap_or_default()
.into_iter()
.find(|w| w.id == bookmark.widget_id)
.map(|w| w.page_id);
set_page_id(p_id);
})
});
};

let (widget_id, set_widget_id) =
create_signal::<Option<Id<Widget>>>(prev_bookmark().map(|b| b.widget_id));
let widget_resource =
create_memo(move |_| page_id().map(|p_id| use_filtered_entities::<Widget>(p_id)));
let page_widgets = create_memo(move |_| {
widget_resource()
.and_then(|rsc| rsc.read())
.unwrap_or_default()
});
let widget_resource = create_memo(move |_| page_id().map(use_filtered_entities::<Widget>));
let page_widgets =
create_memo(move |_| widget_resource().and_then(|rsc| rsc()).unwrap_or_default());

// So, this is a bit of a hack. When the page_widgets are updated
// *after* widget_id has been updated, the DOM does not pick up on
Expand Down Expand Up @@ -72,14 +67,14 @@ pub fn BookmarkForm<F: Fn() + Copy + 'static>(
on:input=move |ev| {
let val = event_target_value(&ev);
if val.is_empty() {
cx.batch(|| {
batch(|| {
set_page_id(None);
if widget_id.with(|w| w.is_some()) {
set_widget_id(None);
}
});
} else {
cx.batch(|| {
batch(|| {
set_page_id(Some(Id::from(val)));
if widget_id.with(|w| w.is_some()) {
set_widget_id(None);
Expand All @@ -90,16 +85,14 @@ pub fn BookmarkForm<F: Fn() + Copy + 'static>(
>
<option value="">"Select a page"</option>
<For
each=move || pages.read().unwrap_or_default()
each=move || pages().unwrap_or_default()
key=|page| page.id.clone()
view=move | page| {
view! {
<option value=page.id.to_string() >
{ page.name }
</option>
}
}
/>
let:page
>
<option value=page.id.to_string() >
{ page.name }
</option>
</For>
</select>
<select
class="bg-slate-600 rounded p-2"
Expand All @@ -118,14 +111,12 @@ pub fn BookmarkForm<F: Fn() + Copy + 'static>(
<For
each=page_widgets
key=|widget| widget.id.clone()
view=move | widget| {
view! {
<option value=widget.id.to_string() >
{ widget.name }
</option>
}
}
/>
let:widget
>
<option value=widget.id.to_string() >
{ widget.name }
</option>
</For>
</select>
<input
class="bg-slate-600 rounded px-2 py-1.5"
Expand Down
52 changes: 28 additions & 24 deletions app/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ pub fn Home() -> impl IntoView {
let (selected_page, set_selected_page) = create_signal::<Option<PageType>>(None);
create_effect(move |_| {
pages.with(move |pages| {
if let Some(sel) = selected_page.get_untracked() {
if !pages.iter().any(|p| p.id == sel.id) {
// selected page doesn't exist anymore, was probably deleted.
// set it to the first page (or none).
set_selected_page(pages.iter().next().cloned());
};
} else if let Some(first_page) = pages.iter().next() {
// no page was selected, but a page exists. select it.
set_selected_page(Some(first_page.clone()));
if let Some(pages) = pages {
if let Some(sel) = selected_page.get_untracked() {
if !pages.iter().any(|p| p.id == sel.id) {
// selected page doesn't exist anymore, was probably deleted.
// set it to the first page (or none).
set_selected_page(pages.iter().next().cloned());
};
} else if let Some(first_page) = pages.iter().next() {
// no page was selected, but a page exists. select it.
set_selected_page(Some(first_page.clone()));
}
}
});
});
Expand All @@ -47,22 +49,24 @@ pub fn Home() -> impl IntoView {

<div class="flex gap-2 p-2 w-full">
<For
each=move || pages.read().unwrap_or_default()
each=move || pages().unwrap_or_default()
key=|page| page.id.clone()
view=move | page| {
let id = store_value( page.id.clone());
let is_selected = Signal::derive( move || {
selected_page().is_some_and(|sp| sp.id == id())
});
let select = SignalSetter::map( move |p| set_selected_page(Some(p)));
view! { <PageTab
page
is_selected
select
delete_page
/> }
}
/>
let:page
>
{
let id = store_value( page.id.clone());
let is_selected = Signal::derive( move || {
selected_page().is_some_and(|sp| sp.id == id())
});
let select = SignalSetter::map( move |p| set_selected_page(Some(p)));
view! { <PageTab
page
is_selected
select
delete_page
/> }
}
</For>

<FlexSpace />

Expand Down
10 changes: 5 additions & 5 deletions app/src/components/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pub fn Page(page: Signal<PageType>) -> impl IntoView {
<div class="flex flex-col gap-4 items-center">
<div class="flex flex-col gap-2 items-stretch">
<For
each=move || widgets.read().unwrap_or_default()
each=move || widgets().unwrap_or_default()
key=|widget| widget.id.clone()
view=move | widget| {
view! { <Widget widget /> }
}
/>
let:widget
>
<Widget widget />
</For>
</div>
</div>
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/components/page_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ pub fn PageTab(
</button>
</button>

<Show when=form_open fallback=|_| () >
<Show when=form_open fallback=|| () >
<Dialog on_close >
<PageForm on_close prev_page=page.get_untracked() />
</Dialog>
</Show>

<Show when=delete_open fallback=|_| () >
<Show when=delete_open fallback=|| () >
<ConfirmationDialog
on_confirm=move || delete_page.dispatch(id())
on_close=on_delete_close
Expand Down
19 changes: 8 additions & 11 deletions app/src/components/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn Widget(widget: WidgetType) -> impl IntoView {
<FlexSpace />
<Show
when=edit_mode
fallback=|_| ()
fallback=|| ()
>
<div class="flex flex-row gap-1 items-center">
<button on:click=move |_| set_form_open(true)>
Expand All @@ -50,22 +50,19 @@ pub fn Widget(widget: WidgetType) -> impl IntoView {
</Show>
</div>
<For
each=move || bookmarks.read().unwrap_or_default()
each=move || bookmarks().unwrap_or_default()
key=|bookmark| bookmark.id.clone()
view=move | bookmark| {
let bookmark = store_value( bookmark);
view! {
<Bookmark bookmark=bookmark() delete_bookmark />
}
}
/>
let:bookmark
>
<Bookmark bookmark delete_bookmark />
</For>
</div>
<Show when=form_open fallback=|_| () >
<Show when=form_open fallback=|| () >
<Dialog on_close=on_form_close >
<WidgetForm on_close=on_form_close prev_widget=widget.get_untracked() />
</Dialog>
</Show>
<Show when=delete_open fallback=|_| () >
<Show when=delete_open fallback=|| () >
<ConfirmationDialog
on_confirm=move || delete_widget.dispatch(id())
on_close=on_delete_close
Expand Down
16 changes: 7 additions & 9 deletions app/src/components/widget_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ pub fn WidgetForm<F: Fn() + Copy + 'static>(
>
<option value="">"Select a page"</option>
<For
each=move || pages.read().unwrap_or_default()
each=move || pages().unwrap_or_default()
key=|page| page.id.clone()
view=move | page| {
view! {
<option value=page.id.to_string() >
{ page.name }
</option>
}
}
/>
let:page
>
<option value=page.id.to_string() >
{ page.name }
</option>
</For>
</select>
<input
class="bg-slate-600 rounded px-2 py-1.5"
Expand Down

0 comments on commit a096ab9

Please sign in to comment.