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::{
components::avatar_cache::{AvatarCacheContext},
};
use crate::components::avatar_cache::AvatarCacheContext;
use yew::{function_component, prelude::*};
#[derive(Properties, PartialEq)]
@ -13,7 +11,11 @@ pub struct Props {
#[function_component(ShowAvatar)]
pub fn show_avatar(props: &Props) -> Html {
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 {
Some(avatar) => html! {
<img

View File

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

View File

@ -13,8 +13,8 @@ use gloo_file::{
use graphql_client::GraphQLQuery;
use validator_derive::Validate;
use web_sys::{FileList, HtmlInputElement, InputEvent};
use yew::prelude::*;
use yew::context::ContextHandle;
use yew::prelude::*;
use yew_form_derive::Model;
#[derive(Default)]
@ -412,7 +412,10 @@ impl UserDetailsForm {
self.user.avatar = Some(avatar);
}
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)
}