This commit is contained in:
Daan Vanoverloop 2022-09-09 13:01:30 +02:00
parent d9988fbf6c
commit 8b11832c22
Signed by: Danacus
GPG Key ID: F2272B50E129FC5C
13 changed files with 233 additions and 182545 deletions

View File

@ -1,10 +1,11 @@
mod api;
use rocket::fs::FileServer;
use std::path::{Path, PathBuf};
use rocket::fs::{FileServer, NamedFile, Options};
use rocket_cors::CorsOptions;
use rocket_db_pools::{mongodb, Database};
use rocket_okapi::{
openapi,
rapidoc::*,
settings::{OpenApiSettings, UrlObject},
swagger_ui::{make_swagger_ui, SwaggerUIConfig},
@ -17,12 +18,6 @@ extern crate rocket;
#[database("party")]
pub struct Db(mongodb::Client);
#[openapi]
#[get("/")]
fn index() -> String {
format!("Hello, world!")
}
#[launch]
fn rocket() -> _ {
let cors = CorsOptions::default()
@ -33,7 +28,9 @@ fn rocket() -> _ {
let building_rocket = rocket::build()
.attach(Db::init())
.attach(cors)
.mount("/", FileServer::from("./web/dist"))
.mount("/", FileServer::from("./web/dist").rank(5))
.mount("/users", FileServer::from("./web/dist"))
.mount("/events", FileServer::from("./web/dist"))
.mount(
"/swagger",
make_swagger_ui(&SwaggerUIConfig {

16
web/dist/index.html vendored
View File

@ -1,12 +1,12 @@
<!DOCTYPE html><html><head>
<meta charset="utf-8">
<link rel="stylesheet" href="/tailwind-9741b801a155bf5f.css">
<!--<link data-trunk href="tailwind.css" rel="css">-->
<link rel="stylesheet" href="/simple.min-d15f5ff500b4c62a.css">
<link rel="stylesheet" href="/style-13bbcf1f99e2f75c.css">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css" rel="stylesheet">
<title>Yew App</title>
<title>LAN Party</title>
<link rel="preload" href="/index-15f6ea5300c4a038_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/index-15f6ea5300c4a038.js"></head>
<body class="base theme-dark bg-gray-900 text-gray-400">
<script type="module">import init from '/index-15f6ea5300c4a038.js';init('/index-15f6ea5300c4a038_bg.wasm');</script></body></html>
<link rel="preload" href="/index-fd3fb1b7ad6689f9_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="/index-fd3fb1b7ad6689f9.js"></head>
<body>
<script type="module">import init from '/index-fd3fb1b7ad6689f9.js';init('/index-fd3fb1b7ad6689f9_bg.wasm');</script></body></html>

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,10 @@
<html>
<head>
<meta charset="utf-8" />
<link data-trunk href="tailwind.css" rel="css">
<!--<link data-trunk href="tailwind.css" rel="css">-->
<link data-trunk href="simple.min.css" rel="css">
<link data-trunk href="style.css" rel="css">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css" rel="stylesheet">
<title>Yew App</title>
<title>LAN Party</title>
</head>
<body class="base theme-dark bg-gray-900 text-gray-400">
</body>
</html>

1
web/simple.min.css vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,60 +0,0 @@
use yew::{function_component, html, Callback, Classes, Component, Properties};
/*
pub enum Msg {
Click,
}
pub struct Button;
impl Component for Button {
type Message = Msg;
type Properties = Props;
fn create(ctx: &yew::Context<Self>) -> Self {
Self
}
fn update(&mut self, ctx: &yew::Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Click => ctx.props().onclick.emit(()),
};
false
}
fn view(&self, ctx: &yew::Context<Self>) -> yew::Html {
let link = ctx.link().clone();
html! {
<button class="bg-gray-700 hover:bg-gray-800 text-gray-400 font-bold py-2 px-2 rounded inline-flex items-center" onclick={link.callback(|_| Msg::Click)}>{ "+1" }</button>
}
}
}
*/
#[derive(Properties, PartialEq, Default)]
pub struct Props {
pub onclick: Callback<()>,
#[prop_or_default]
pub text: String,
#[prop_or_default]
pub icon: String,
}
#[function_component(Button)]
pub fn button(props: &Props) -> Html {
let mut icon_class = Classes::from("mdi");
icon_class.push("text-lg");
if !props.icon.is_empty() {
icon_class.push(&props.icon);
}
html! {
<button class="bg-gray-700 hover:bg-gray-800 text-gray-400 font-bold py-2 px-2 rounded inline-flex items-center" onclick={props.onclick.reform(move |_| ())}>
<span class={icon_class}></span>
<span>{ &props.text }</span>
</button>
}
}

View File

@ -5,11 +5,10 @@ use lan_party_core::event::{
free_for_all_game::FreeForAllGameSpec, team_game::TeamGameSpec, test::TestSpec, EventSpec,
EventTypeSpec,
};
use log::debug;
use paste::paste;
use sycamore::prelude::*;
use super::input_classes;
macro_rules! editable {
($type:ty => $editor:ty) => {
impl<'a, G: Html> Editable<'a, G> for $type {
@ -23,7 +22,8 @@ macro_rules! edit_fields {
view! { $cx,
$(
p {
($name) ": " ($prop.edit($cx))
label { ($name) }
($prop.edit($cx))
}
)*
}
@ -65,6 +65,58 @@ macro_rules! edit_struct {
};
}
macro_rules! link_variants {
($cx:ident, $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)? => $state:ident as $t:ident) => {
let $selected = create_signal($cx, String::from("0"));
$(let $var_name = if let $t::$variant(v) = $state.get().as_ref().clone() {
create_signal($cx, v.clone())
} else {
create_signal($cx, <$var_type>::default())
};)*
create_effect($cx, || {
debug!("{:#?}", $selected.get());
match $selected.get().as_str() {
$(stringify!($index) => $state.set($t::$variant($var_name.get().as_ref().clone())),)*
_ => unreachable!()
}
});
};
}
macro_rules! edit_enum {
($enum:ident => $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)?) => {
paste! {
pub struct [<$enum Edit>];
impl<'a, G: Html> Editor<'a, G, $enum> for [<$enum Edit>] {
fn edit(cx: Scope<'a>, props: EditProps<'a, $enum>) -> View<G> {
let state = props.state;
link_variants!(cx, $selected =>
$($index: $var_name = $variant: $var_type,)*
=> state as $enum
);
view! { cx,
Block(title=stringify!($enum).to_string()) {
select(bind:value=$selected) {
$(option(value={stringify!($index)}, selected=$index==0) { (stringify!($variant)) })*
}
(match state.get().as_ref().clone() {
$($enum::$variant(_) => $var_name.edit(cx),)*
})
}
}
}
}
editable!($enum => [<$enum Edit>]);
}
};
}
#[derive(Prop)]
pub struct EditProps<'a, T> {
pub state: &'a Signal<T>,
@ -108,6 +160,13 @@ pub trait Editable<'a, G: Html>: Sized {
}
edit_struct!(EventSpec => ("Name", name), ("Description", description), ("Event type", event_type));
edit_enum!(EventTypeSpec => selected =>
0: test = Test: TestSpec,
1: team_game = TeamGame: TeamGameSpec,
2: free_for_all_game = FreeForAllGame: FreeForAllGameSpec
);
edit_struct!(TestSpec => ("Number of players", num_players));
edit_struct!(TeamGameSpec => );
edit_struct!(FreeForAllGameSpec => );
@ -117,7 +176,7 @@ pub struct StringEdit;
impl<'a, G: Html> Editor<'a, G, String> for StringEdit {
fn edit(cx: Scope<'a>, props: EditProps<'a, String>) -> View<G> {
view! { cx,
input(class=input_classes(), bind:value=props.state)
input(bind:value=props.state)
}
}
}
@ -132,12 +191,12 @@ where
{
fn edit(cx: Scope<'a>, _props: EditProps<'a, T>) -> View<G> {
view! { cx,
"Unimplemented"
i { "Editor Unimplemented" }
}
}
}
editable!(EventTypeSpec => StubEdit);
//editable!(EventTypeSpec => StubEdit);
pub struct InputEdit;
@ -148,14 +207,14 @@ where
fn edit(cx: Scope<'a>, props: EditProps<'a, T>) -> View<G> {
let value = create_signal(cx, props.state.get().to_string());
create_memo(cx, || {
create_effect(cx, || {
props
.state
.set((*value.get()).parse().unwrap_or(T::default()))
});
view! { cx,
input(class=input_classes(), bind:value=value)
input(bind:value=value)
}
}
}
@ -182,3 +241,19 @@ impl<'a, G: Html> Editor<'a, G, bool> for BoolEdit {
}
editable!(bool => BoolEdit);
#[derive(Default, Clone)]
pub struct Test {
inner: TestInner,
inner2: TestInner,
}
edit_struct!(Test => ("Inner", inner), ("Inner 2", inner2));
#[derive(Default, Clone)]
pub struct TestInner {
some_text: String,
some_number: usize,
}
edit_struct!(TestInner => ("Text", some_text), ("Number", some_number));

View File

@ -3,20 +3,6 @@ pub mod event;
use sycamore::prelude::*;
use web_sys::Event;
#[derive(Prop)]
pub struct PageProps<'a, G: Html> {
pub children: Children<'a, G>,
}
#[component]
pub fn Page<'a, G: Html>(cx: Scope<'a>, props: PageProps<'a, G>) -> View<G> {
let children = props.children.call(cx);
view! { cx,
div(class="max-w-7xl mx-auto") { (children) }
}
}
#[derive(Prop)]
pub struct Props<F: FnMut(Event)> {
pub onclick: F,
@ -28,14 +14,14 @@ pub struct Props<F: FnMut(Event)> {
#[component]
pub fn Button<'a, G: Html, F: 'a + FnMut(Event)>(cx: Scope<'a>, props: Props<F>) -> View<G> {
let mut icon_class = String::from("mdi text-lg ");
let mut icon_class = String::from("mdi ");
if !props.icon.is_empty() {
icon_class.push_str(&props.icon);
}
view! { cx,
button(class="bg-gray-700 hover:bg-gray-800 text-gray-400 font-bold py-2 px-2 rounded inline-flex items-center",on:click=props.onclick) {
button(on:click=props.onclick) {
span(class=icon_class)
span { (props.text) }
}
@ -54,27 +40,19 @@ pub fn Table<'a, G: Html>(cx: Scope<'a>, props: TableProps<'a, G>) -> View<G> {
let children = props.children.call(cx);
view! { cx,
div(class="inline-block min-w-full py-2 align-middle") {
div(class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg") {
table(class="min-w-full divide-y divide-gray-600") {
thead(class="bg-gray-800") {
tr {
(View::new_fragment(props.headers.iter().cloned().map(|header| view! { cx,
th(
scope="col",
class="px-3 py-3.5 text-left
text-sm font-semibold text-gray-400"
) {
(header)
}
}).collect()))
table {
thead {
tr {
(View::new_fragment(props.headers.iter().cloned().map(|header| view! { cx,
th(scope="col") {
(header)
}
}
tbody(class="divide-y divide-gray-600 bg-gray-700") {
(children)
}
}).collect()))
}
}
tbody {
(children)
}
}
}
}
@ -88,46 +66,11 @@ pub struct BlockProps<'a, G: Html> {
#[component]
pub fn Block<'a, G: Html>(cx: Scope<'a>, props: BlockProps<'a, G>) -> View<G> {
let children = props.children.call(cx);
let open = create_signal(cx, false);
let class = create_memo(cx, || {
if *open.get() {
"overflow-hidden"
} else {
"overflow-hidden max-h-1"
}
});
view! { cx,
div(class="px-3 py-3 rounded-lg border-solid border-gray-800 border-2 my-3") {
p(class="cursor-pointer text-lg",on:click=move |_| open.set(!*open.get())) { (props.title) }
div(class=class) {
br {}
div {
(children)
}
}
details {
summary { (props.title) }
p { (children) }
}
}
}
pub fn input_classes() -> &'static str {
"mx-2 appearance-none block bg-gray-700 text-slate-400 border border-gray-600 rounded py-2 px-2 w-50 leading-tight focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500"
}
/*
#[function_component(Loading)]
pub fn loading() -> Html {
html! {
<svg role="status" class="inline h-8 w-8 animate-spin mr-2 text-gray-200 dark:text-gray-600 fill-gray-400"
viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor" />
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill" />
</svg>
}
}
*/

View File

@ -1,49 +0,0 @@
use yew::{function_component, html, Children, Html, Properties};
#[derive(Properties, PartialEq, Default)]
pub struct Props {
pub headers: Vec<String>,
pub rows: Vec<Vec<String>>,
pub loading: bool,
#[prop_or_default]
pub children: Children,
}
#[function_component(Table)]
pub fn table(props: &Props) -> Html {
html! {
<div class="inline-block min-w-full py-2 align-middle">
<div class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
<table class="min-w-full divide-y divide-gray-600">
<thead class="bg-gray-800">
<tr>
{props.headers.iter().map(|header| html! {
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-400">{header}</th>
}).collect::<Html>()}
</tr>
</thead>
<tbody class="divide-y divide-gray-600 bg-gray-700">
{props.rows.iter().map(|row| html! {
<tr>
{row.iter().map(|value| html! {
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-400">{value}</td>
}).collect::<Html>()}
</tr>
}).collect::<Html>()}
{ for props.children.iter() }
{if props.loading == true { html! {
<tr>
<td colspan={(props.headers.len() + 1).to_string()} class="py-3">
<div class="grid place-items-center">
{ "Loading ..." }
</div>
</td>
</tr>
}} else { html! {} }}
</tbody>
</table>
</div>
</div>
}
}

View File

@ -34,22 +34,11 @@ pub struct NavbarProps<'a, G: Html> {
pub fn Navbar<'a, G: Html>(cx: Scope<'a>, props: NavbarProps<'a, G>) -> View<G> {
let children = props.children.call(cx);
view! { cx,
nav(class="bg-gray-800") {
div(class="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8") {
div(class="relative flex h-16 items-center justify-between") {
div(class="flex flex-1 items-center justify-center sm:items-stretch sm:justify-start") {
div(class="hidden sm:ml-6 sm:block") {
div(class="flex space-x-4") {
(View::new_fragment(props.pages.iter().cloned().enumerate().map(|(_i, page)| {
view! { cx, a(href=page.target) { (page.name) } }
}).collect()))
}
}
}
}
(children)
}
nav {
(View::new_fragment(props.pages.iter().cloned().enumerate().map(|(_i, page)| {
view! { cx, a(href=page.target) { (page.name) } }
}).collect()))
(children)
}
}
}
@ -78,10 +67,12 @@ fn main() {
integration=HistoryIntegration::new(),
view=|cx, route: &ReadSignal<AppRoutes>| {
view! { cx,
Navbar(pages=pages) {
header {
Navbar(pages=pages) {
}
}
div(class="app") {
main {
(match route.get().as_ref() {
AppRoutes::Home => view! { cx,
"This is the home page"

View File

@ -1,22 +1,23 @@
use crate::components::event::{IntoEdit, Test};
use lan_party_core::event::EventSpec;
use log::debug;
use sycamore::prelude::*;
use crate::components::{
event::{Edit, EditProps},
Block, Button, Page,
Block, Button,
};
#[component]
pub fn EventsPage<'a, G: Html>(cx: Scope<'a>) -> View<G> {
let event_spec = create_signal(cx, EventSpec::default());
let test = create_signal(cx, Test::default());
view! { cx,
Page {
Block(title="Create new event".into()) {
EventSpec::edit(EditProps { state: event_spec })
Button(icon="mdi-check".into(), onclick=move |_| debug!("{:#?}", event_spec.get()))
}
Block(title="Create new event".into()) {
EventSpec::edit(EditProps { state: event_spec })
Button(icon="mdi-check".into(), onclick=move |_| debug!("{:#?}", event_spec.get()))
(test.edit(cx))
}
}
}

View File

@ -1,4 +1,4 @@
use crate::components::{input_classes, Button, Page, Table};
use crate::components::{Button, Table};
use lan_party_core::user::User;
use log::debug;
use reqwasm::http::Method;
@ -100,53 +100,51 @@ pub fn UsersPage<'a, G: Html>(cx: Scope<'a>) -> View<G> {
};
view! { cx,
Page {
Table(headers=headers) {
Keyed(
iterable=users,
view=move |cx, user| {
let user = create_ref(cx, user);
view! { cx,
tr {
td(class="whatespace-nowrap px-3 py-4 text-sm") { (user.name) }
td(class="whatespace-nowrap px-3 text-sm") {
(if Some(&user.name) == (*score_edit.get()).as_ref() { view! { cx,
span(class="inline-block") {
input(bind:value=new_score, class=input_classes())
Table(headers=headers) {
Keyed(
iterable=users,
view=move |cx, user| {
let user = create_ref(cx, user);
view! { cx,
tr {
td { (user.name) }
td {
(if Some(&user.name) == (*score_edit.get()).as_ref() { view! { cx,
span(class="user-score") {
input(bind:value=new_score)
}
Button(icon="mdi-check".into(), onclick=oncheck)
}} else { view! { cx,
span(class="user-score") {
(user.score)
}
Button(
icon="mdi-pencil".into(),
onclick=move |_| {
score_edit.set(Some(user.name.clone()));
new_score.set(user.score.to_string());
}
Button(icon="mdi-check".into(), onclick=oncheck)
}} else { view! { cx,
span(class="my-3,w-20") {
(user.score)
}
Button(
icon="mdi-pencil".into(),
onclick=move |_| {
score_edit.set(Some(user.name.clone()));
new_score.set(user.score.to_string());
}
)
}})
}
td(class="whatespace-nowrap px-3 py-4 text-sm") {
Button(icon="mdi-delete".into(),onclick=ondelete(user.name.clone()))
}
)
}})
}
td {
Button(icon="mdi-delete".into(),onclick=ondelete(user.name.clone()))
}
}
},
key=|user| (user.name.clone(), user.score.clone()),
)
tr {
td(class="whatespace-nowrap px-3 text-sm") {
span(class="inline-block") {
input(bind:value=new_username, class=input_classes())
}
}
td(class="whatespace-nowrap px-3 py-4 text-sm") {}
td(class="whatespace-nowrap py-4 px-3 text-sm") {
Button(icon="mdi-plus".into(),onclick=onadd)
},
key=|user| (user.name.clone(), user.score.clone()),
)
tr {
td {
span {
input(bind:value=new_username)
}
}
td {}
td {
Button(icon="mdi-plus".into(),onclick=onadd)
}
}
}
}

60
web/style.css Normal file
View File

@ -0,0 +1,60 @@
:root {
--accent: #1769c1;
}
@media (prefers-color-scheme: dark) {
:root {
--accent: #1769c1;
}
}
.user-score {
display: inline-block;
margin-right: 1em;
width: 100px;
}
.block-title {
margin: 0px;
cursor: pointer;
}
.block {
padding: 1em;
border: 1px solid var(--border);
border-radius: 5px;
}
.collapse {
overflow: hidden;
}
.collapse.closed {
max-height: 0em;
}
details {
margin-bottom: 0;
}
details[open] summary {
margin-bottom: -0.7rem;
}
details[open] > summary {
margin-bottom: .5rem;
}
textarea,
select,
input,
progress {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
textarea:focus, input:focus{
outline: none;
}