/*
* This file is part of laurelin/client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use iyes_loopless::prelude::*;
use belly::prelude::*;
use crate::{
cfg::CfgUser,
plugins::config::{SaveEvent, SaveEventValue},
};
use super::MenuState;
pub(super) struct LogoutEvent;
pub(super) fn account_loggedin_setup(mut commands: Commands, cfg_user: Res<CfgUser>) {
let username = cfg_user.username.clone();
commands.add(eml! {
<body>
<div c:menu>
<span c:menutitle>
"Account"
</span>
<span>
"Logged in as:"
</span>
<span>
{ username }
</span>
<button c:menubutton on:press=connect!(|ctx| {
ctx.send_event(LogoutEvent)
})>
"Logout"
</button>
<button c:menubutton on:press=connect!(|ctx| {
ctx.commands().insert_resource(NextState(MenuState::Main))
})>
"Back"
</button>
</div>
</body>
});
}
pub(super) fn handle_logout_event(
mut events: EventReader<LogoutEvent>,
mut commands: Commands,
mut cfg_user: ResMut<CfgUser>,
mut save_event_writer: EventWriter<SaveEvent>,
) {
for _event in events.iter() {
// Reset CfgUser to default
*cfg_user = CfgUser::default();
save_event_writer.send(SaveEvent {
value: SaveEventValue::User(cfg_user.clone()),
});
commands.insert_resource(NextState(MenuState::AccountLoggedOut));
}
}