feat(group): add leave and join

This commit is contained in:
rick 2022-10-29 23:14:51 +02:00
parent 52fb59cc01
commit 3be7ac3336
Signed by: Rick
GPG key ID: 4A6223D66294EB20

View file

@ -1,6 +1,6 @@
use gloo_net::http::Request;
use gloo_net::Error;
use patternfly_yew::{Button, Card, Flex, FlexModifier, Icon, Modal, ModalVariant, Title, ToastViewer, ToFlexItems, Variant, WithBreakpointExt}; use yew::prelude::*;
use patternfly_yew::{Button, Card, Flex, FlexModifier, Gallery, Icon, Modal, ModalVariant, Title, ToastViewer, ToFlexItems, Variant, WithBreakpointExt}; use yew::prelude::*;
use crate::components::{Group, Home, TestMessage, ToastFields, ToastType};
@ -11,6 +11,7 @@ pub struct ListGroups {
free_groups: Vec<Group>,
nb_show: usize,
selected_group: usize,
quit_group: Option<usize>,
join: bool,
}
@ -21,10 +22,17 @@ pub struct PropGroups {
#[derive(PartialEq)]
pub enum MsgListGroups {
/// Rafraichi la page
Refresh,
/// Tous les groupes ont été récupéré
Done(Vec<Group>),
/// Tous les groupes sans l'utilisateur ont été récupéré
DoneFree(Vec<Group>),
/// Quitte un groupe
Leave(usize),
FinishLeave(usize),
/// Confirmation du retrait du groupe
//ConfirmLeave,
/// Afficher plus de groupes
More,
/// Afficher ou cacher la fenetre pour rejoindre des groupes
@ -35,6 +43,8 @@ pub enum MsgListGroups {
/// son ajout futur
SelectGroup(usize),
/// Sélection du nouveau groupe faite, envoie d'un requete en back
SendJoin,
/// Requete en back fini
FinishJoin,
/// Popup d'informations
Info(String, String),
@ -117,13 +127,14 @@ impl Component for ListGroups {
let more = ctx.link().callback(|_| MsgListGroups::More);
let join = ctx.link().callback(|_| MsgListGroups::ToggleJoin);
let cancel_join = ctx.link().callback(|_| MsgListGroups::CancelJoin);
let finish_join = ctx.link().callback(|_| MsgListGroups::FinishJoin);
let finish_join = ctx.link().callback(|_| MsgListGroups::SendJoin);
let groups: Vec<Html> = self.groups[..self.nb_show].iter().map(|group| { html!{
<div>
let groups: Vec<Html> = self.groups[..self.nb_show].iter().enumerate().map(|(id, group)| { html!{
<Card>
<h1>{ group.name.clone() }</h1>
<h2>{ group.game.clone() }</h2>
</div>
<Button onclick={ ctx.link().callback(move |_| MsgListGroups::Leave(id)) }variant={Variant::DangerSecondary}>{ "Quitter" }</Button>
</Card>
}}).collect::<Vec<Html>>();
let mut groups_final: Vec<Html> = Vec::new();
@ -138,6 +149,8 @@ impl Component for ListGroups {
<Button onclick={join} icon={Some(Icon::PlusCircleIcon)}>{ "Rejoindre un groupe" }</Button>
if self.join {
<Modal onclose={cancel_join} variant={ ModalVariant::Medium }>
<br />
<Gallery gutter=true>
{ self.free_groups.iter().enumerate().map(|(id, group)| {
html! {
<Card
@ -148,8 +161,9 @@ impl Component for ListGroups {
{group.name.clone()}
</Card>
}
}).collect::<Html>() }
<Button onclick={finish_join}>{ "Valider" }</Button>
}).collect::<Vec<Html>>() }
</Gallery>
<Button variant={Variant::Primary} onclick={finish_join}>{ "Valider" }</Button>
</Modal>
}
@ -186,6 +200,8 @@ impl Component for ListGroups {
body,
})))
}
false
}
MsgListGroups::Error(title, body) => {
if let Some(parent) = ctx.link().get_parent() {
@ -196,6 +212,8 @@ impl Component for ListGroups {
body,
})))
}
false
}
MsgListGroups::Done(groups) => {
let len = groups.len();
@ -206,37 +224,110 @@ impl Component for ListGroups {
3
};
return true;
true
}
MsgListGroups::DoneFree(groups) => {
let len = groups.len();
self.free_groups = groups;
return true;
true
}
MsgListGroups::ToggleJoin => {
self.join = !self.join;
return true;
true
}
MsgListGroups::CancelJoin => {
self.join = false;
return true;
true
}
MsgListGroups::SendJoin => {
if let Some(group) = self.free_groups.get(self.selected_group) {
let id = group.id.clone();
ctx.link().send_future(async move {
match Request::put(&format!("/api/groups/{}/join", id)).send().await {
Ok(resp) => {
if resp.ok() {
MsgListGroups::FinishJoin
} else {
MsgListGroups::Error(
String::from("Erreur"),
String::from("Pas de réponse 200")
)
}
}
Err(_) => {
MsgListGroups::Error(
String::from("Erreur"),
String::from("Bruh")
)
}
}
});
} else {
ctx.link().send_message(MsgListGroups::Error(
String::from("Erreur"),
String::from("Contactez nous si vous êtes tombés sur cette erreur.")
));
}
false
}
MsgListGroups::FinishJoin => {
return false;
self.groups.push(self.free_groups.remove(self.selected_group));
if self.nb_show < 3 {
self.nb_show += 1;
}
true
}MsgListGroups::FinishLeave(id) => {
self.free_groups.push(self.groups.remove(id));
if self.nb_show > 0 && id <= self.nb_show {
self.nb_show -= 1;
}
true
}
MsgListGroups::SelectGroup(id) => {
self.selected_group = id;
return true;
true
}
MsgListGroups::Leave(id_group) => {
if let Some(group) = self.groups.get(id_group) {
let id = group.id.clone();
ctx.link().send_future(async move {
match Request::put(&format!("/api/groups/{}/leave", id)).send().await {
Ok(resp) => {
if resp.ok() {
MsgListGroups::FinishLeave(id_group)
} else {
MsgListGroups::Error(
String::from("Erreur"),
String::from("Pas de réponse 200")
)
}
}
Err(_) => {
MsgListGroups::Error(
String::from("Erreur"),
String::from("Bruh")
)
}
}
});
} else {
ctx.link().send_message(MsgListGroups::Error(
String::from("Erreur"),
String::from("Contactez nous si vous êtes tombés sur cette erreur.")
));
}
false
}
MsgListGroups::Refresh => return true,
MsgListGroups::More => {
self.nb_show += 3;
if self.nb_show > self.groups.len() {
self.nb_show -= self.nb_show - self.groups.len();
}
return true;
true
}
}
false
}
}