This commit is contained in:
Austin 2023-03-30 19:50:26 +00:00
parent e2fd9686a8
commit d868bbf651
3 changed files with 17 additions and 14 deletions

View File

@ -1,6 +1,4 @@
use crate::{ use crate::components::avatar_cache::AvatarCacheContext;
components::avatar_cache::{AvatarCacheContext},
};
use yew::{function_component, prelude::*}; use yew::{function_component, prelude::*};
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
@ -13,7 +11,11 @@ pub struct Props {
#[function_component(ShowAvatar)] #[function_component(ShowAvatar)]
pub fn show_avatar(props: &Props) -> Html { pub fn show_avatar(props: &Props) -> Html {
let cache = use_context::<AvatarCacheContext>().expect("no ctx found"); let cache = use_context::<AvatarCacheContext>().expect("no ctx found");
let avatar = cache.avatars.get(&props.username).map(|val| val.clone()).unwrap_or(None); let avatar = cache
.avatars
.get(&props.username)
.map(|val| val.clone())
.unwrap_or(None);
match avatar { match avatar {
Some(avatar) => html! { Some(avatar) => html! {
<img <img

View File

@ -18,19 +18,19 @@ pub struct GetUserAvatar;
#[graphql( #[graphql(
schema_path = "../schema.graphql", schema_path = "../schema.graphql",
query_path = "queries/list_users.graphql", query_path = "queries/list_users.graphql",
response_derives = "Debug,Clone,PartialEq,Eq,Hash", response_derives = "Debug",
variables_derives = "Clone", variables_derives = "Clone",
custom_scalars_module = "crate::infra::graphql" custom_scalars_module = "crate::infra::graphql"
)] )]
pub struct ListUserNames; pub struct ListUserNames;
#[derive(Debug, PartialEq, Clone)] #[derive(PartialEq, Clone)]
pub enum CacheAction { pub enum CacheAction {
Clear, Clear,
AddAvatar((String, Option<String>)), AddAvatar((String, Option<String>)),
} }
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct AvatarCache { pub struct AvatarCache {
pub avatars: HashMap<String, Option<String>>, pub avatars: HashMap<String, Option<String>>,
} }
@ -55,14 +55,14 @@ impl Reducible for AvatarCache {
pub type AvatarCacheContext = UseReducerHandle<AvatarCache>; pub type AvatarCacheContext = UseReducerHandle<AvatarCache>;
#[derive(Debug, PartialEq, Clone)] #[derive(PartialEq, Clone)]
pub enum CacheMode { pub enum CacheMode {
AllUsers, AllUsers,
SingleUser(String), SingleUser(String),
None, None,
} }
#[derive(Properties, Debug, PartialEq)] #[derive(Properties, PartialEq)]
pub struct AvatarCacheProviderProps { pub struct AvatarCacheProviderProps {
#[prop_or_default] #[prop_or_default]
pub children: Children, pub children: Children,
@ -81,9 +81,7 @@ pub fn avatar_cache_provider(props: &AvatarCacheProviderProps) -> Html {
use_effect_with_deps( use_effect_with_deps(
move |mode| { move |mode| {
match mode { match mode {
CacheMode::None => { CacheMode::None => cache.dispatch(CacheAction::Clear),
cache.dispatch(CacheAction::Clear)
}
CacheMode::AllUsers => { CacheMode::AllUsers => {
let cache = cache.clone(); let cache = cache.clone();
wasm_bindgen_futures::spawn_local(async move { wasm_bindgen_futures::spawn_local(async move {

View File

@ -13,8 +13,8 @@ use gloo_file::{
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use validator_derive::Validate; use validator_derive::Validate;
use web_sys::{FileList, HtmlInputElement, InputEvent}; use web_sys::{FileList, HtmlInputElement, InputEvent};
use yew::prelude::*;
use yew::context::ContextHandle; use yew::context::ContextHandle;
use yew::prelude::*;
use yew_form_derive::Model; use yew_form_derive::Model;
#[derive(Default)] #[derive(Default)]
@ -412,7 +412,10 @@ impl UserDetailsForm {
self.user.avatar = Some(avatar); self.user.avatar = Some(avatar);
} }
self.just_updated = true; self.just_updated = true;
self.avatar_cache.dispatch(CacheAction::AddAvatar((self.user.id.clone(), self.user.avatar.clone()))); self.avatar_cache.dispatch(CacheAction::AddAvatar((
self.user.id.clone(),
self.user.avatar.clone(),
)));
Ok(true) Ok(true)
} }