mentorenwahl/frontend/src/routes/settings/assignments.rs
Dominic Grimm d6dbd18090
Some checks failed
continuous-integration/drone/push Build is failing
Add student vote enable toggle to admin panel
2023-02-04 12:14:11 +01:00

160 lines
5.5 KiB
Rust

use graphql_client::reqwest::post_graphql;
use yew::prelude::*;
use crate::components;
use crate::graphql;
pub enum Msg {
DoneFetchingCanVote {
errors: graphql::Errors,
can_vote: bool,
},
UpdateCanVote,
UpdateCanVoteDone(graphql::Errors),
StartAssignment,
StartAssignmentDone(Option<Vec<String>>),
}
#[derive(Properties, PartialEq, Eq)]
pub struct AssignmentsProps {
pub token: String,
}
pub struct Assignments {
errors: Option<Vec<String>>,
fetching_students_can_vote: bool,
students_can_vote: bool,
}
impl Component for Assignments {
type Message = Msg;
type Properties = AssignmentsProps;
fn create(ctx: &Context<Self>) -> Self {
let client = graphql::client(Some(&ctx.props().token)).unwrap();
ctx.link().send_future(async move {
let response = post_graphql::<graphql::queries::students_can_vote::StudentsCanVote, _>(
&client,
graphql::URL.as_str(),
graphql::queries::students_can_vote::students_can_vote::Variables,
)
.await
.unwrap();
Msg::DoneFetchingCanVote {
errors: graphql::convert(response.errors),
can_vote: response.data.unwrap().students_can_vote,
}
});
Self {
errors: None,
fetching_students_can_vote: true,
students_can_vote: false,
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::DoneFetchingCanVote { errors, can_vote } => {
self.errors = errors;
self.students_can_vote = can_vote;
self.fetching_students_can_vote = false;
true
}
Msg::UpdateCanVote => {
self.students_can_vote = !self.students_can_vote;
let state = self.students_can_vote;
let client = graphql::client(Some(&ctx.props().token)).unwrap();
ctx.link().send_future(async move {
let response = post_graphql::<graphql::mutations::set_voting::SetVoting, _>(
&client,
graphql::URL.as_str(),
graphql::mutations::set_voting::set_voting::Variables { state },
)
.await
.unwrap();
Msg::UpdateCanVoteDone(graphql::convert(response.errors))
});
true
}
Msg::UpdateCanVoteDone(errors) => {
self.errors = errors;
true
}
Msg::StartAssignment => {
let client = graphql::client(Some(&ctx.props().token)).unwrap();
ctx.link().send_future(async move {
let response =
post_graphql::<graphql::mutations::start_assignment::StartAssignment, _>(
&client,
graphql::URL.as_str(),
graphql::mutations::start_assignment::start_assignment::Variables,
)
.await
.unwrap();
Msg::StartAssignmentDone(graphql::convert(response.errors))
});
false
}
Msg::StartAssignmentDone(errors) => {
self.errors = errors;
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<fieldset class={classes!("fieldset")}>
<legend>{ "Zuweisungen" }</legend>
<table class={classes!("table")}>
<thead>
<tr>
<th>{ "Aktion" }</th>
<th>{ "Option" }</th>
</tr>
</thead>
<tbody>
<tr>
if self.fetching_students_can_vote {
<td><components::fetching::Fetching /></td>
} else {
<td>{ "Wahl erlauben" }</td>
<td>
<button onclick={ctx.link().callback(|_| Msg::UpdateCanVote)}
class={classes!("button", if self.students_can_vote { "is-success" } else { "is-danger" })}
>
if self.students_can_vote {
{ "An" }
} else {
{ "Aus" }
}
</button>
</td>
}
</tr>
<tr>
<td>{ "Zuweisung starten" }</td>
<td>
<button onclick={ctx.link().callback(|_| Msg::StartAssignment)} class={classes!("button")}>
{ "Ausführen" }
</button>
</td>
</tr>
</tbody>
</table>
<components::graphql_errors::GraphQLErrors errors={self.errors.to_owned()} />
</fieldset>
}
}
}