gitea_pages/frontend/src/components/notification_listing.rs

59 lines
1.5 KiB
Rust

use std::rc::Rc;
use yew::prelude::*;
use yewdux::prelude::*;
use crate::{components, stores};
pub enum Msg {
UpdateNotifications(Rc<stores::Notifications>),
Remove(usize),
}
pub struct NotificationListing {
notifications: Rc<stores::Notifications>,
notifications_dispatch: Dispatch<stores::Notifications>,
}
impl Component for NotificationListing {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let notifications_dispatch =
Dispatch::subscribe(ctx.link().callback(Msg::UpdateNotifications));
Self {
notifications: notifications_dispatch.get(),
notifications_dispatch,
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::UpdateNotifications(x) => {
self.notifications = x;
true
}
Msg::Remove(i) => {
self.notifications_dispatch
.reduce_mut(|notifs| notifs.notifications.remove(&i));
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let remove = Rc::new(ctx.link().callback(Msg::Remove));
html! {
{
for self.notifications.notifications.iter().rev().map(|(i, _)| html! {
<components::Notification notifications={self.notifications.clone()} index={i} remove={remove.clone()} />
})
}
}
}
}