Ajoute un administrateur qui gère les comptes
Le premier compte inscrit devient administrateur. La page /admin lui permet de lister, créer, modifier et supprimer les comptes, même quand les inscriptions sont fermées. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2ade50465e
commit
f3f5f0cee1
7 files changed
+539
-34
No files matched your search
+15
-3
@@ -97,7 +97,7 @@ fn AuthLayout(title: &'static str, children: Children) -> impl IntoView {
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn FormError(#[prop(into)] error: Signal<Option<String>>) -> impl IntoView {
|
||||
pub fn FormError(#[prop(into)] error: Signal<Option<String>>) -> impl IntoView {
|
||||
move || {
|
||||
error.get().map(|msg| {
|
||||
view! {
|
||||
@@ -207,7 +207,7 @@ pub fn RegisterPage() -> impl IntoView {
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Field(label: &'static str, children: Children) -> impl IntoView {
|
||||
pub fn Field(label: &'static str, children: Children) -> impl IntoView {
|
||||
view! {
|
||||
<label class="flex flex-col gap-1.5">
|
||||
<span class="font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">{label}</span>
|
||||
@@ -241,7 +241,19 @@ pub fn AccountSheet(user: User) -> impl IntoView {
|
||||
<Show when=move || ctx.account_open.get()>
|
||||
<Sheet title=user.with_value(|u| u.name.clone()) on_close=close>
|
||||
<p class="mt-1 text-ink/70">{user.with_value(|u| u.email.clone())}</p>
|
||||
<ActionForm action=auth.logout attr:class="mt-6">
|
||||
{user
|
||||
.with_value(|u| u.is_admin)
|
||||
.then(|| {
|
||||
view! {
|
||||
<A
|
||||
href="/admin"
|
||||
attr:class="mt-6 block w-full rounded-2xl border-2 border-ink bg-lime px-4 py-3 text-center font-semibold"
|
||||
>
|
||||
"Gérer les utilisateurs"
|
||||
</A>
|
||||
}
|
||||
})}
|
||||
<ActionForm action=auth.logout attr:class="mt-3">
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full rounded-2xl border-2 border-ink px-4 py-3 font-semibold"
|
||||
|
||||
@@ -0,0 +1,310 @@
|
||||
use leptos::ev;
|
||||
use leptos::prelude::*;
|
||||
use leptos::task::spawn_local;
|
||||
use leptos_router::components::A;
|
||||
|
||||
use super::account::{Auth, Field, FormError, RequireUser};
|
||||
use super::error_message;
|
||||
use super::sheets::Sheet;
|
||||
use crate::api::{create_account, delete_account, get_accounts, update_account};
|
||||
use crate::model::{Account, User};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum AccountEditor {
|
||||
New,
|
||||
Edit(User),
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn AdminPage() -> impl IntoView {
|
||||
view! { <RequireUser render=|user| view! { <AdminView me=user /> } /> }
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn AdminView(me: User) -> impl IntoView {
|
||||
if !me.is_admin {
|
||||
return view! {
|
||||
<AdminLayout>
|
||||
<section class="ticket relative mt-6 bg-paper px-5 pt-6 pb-8">
|
||||
<p class="text-lg text-ink/70">"Cette page est réservée aux administrateurs."</p>
|
||||
</section>
|
||||
</AdminLayout>
|
||||
}
|
||||
.into_any();
|
||||
}
|
||||
|
||||
let version = RwSignal::new(0);
|
||||
let editor = RwSignal::new(None::<AccountEditor>);
|
||||
let accounts = Resource::new(move || version.get(), |_| get_accounts());
|
||||
let me_id = me.id;
|
||||
|
||||
view! {
|
||||
<AdminLayout>
|
||||
<section class="ticket relative mt-6 bg-paper px-5 pt-6 pb-8">
|
||||
<p class="font-mono text-sm tracking-[0.2em] text-ink/60 uppercase">"Lisette · Comptes"</p>
|
||||
<h2 class="mt-1 font-display text-[1.7rem] leading-tight font-bold tracking-tight">
|
||||
"Utilisateurs"
|
||||
</h2>
|
||||
<Transition fallback=|| view! { <p class="mt-4 text-ink/50">"…"</p> }>
|
||||
{move || Suspend::new(async move {
|
||||
match accounts.await {
|
||||
Ok(accounts) => view! { <AccountRows accounts me_id editor /> }.into_any(),
|
||||
Err(err) => {
|
||||
view! { <p class="mt-4 font-medium">{error_message(err)}</p> }.into_any()
|
||||
}
|
||||
}
|
||||
})}
|
||||
</Transition>
|
||||
</section>
|
||||
|
||||
<button
|
||||
class="tile-shadow fixed right-[max(1rem,calc(50vw-14rem+1rem))] bottom-[max(1.25rem,env(safe-area-inset-bottom))] z-10 rounded-2xl border-2 border-ink bg-pink px-6 py-4 font-display text-xl font-bold active:translate-y-1 active:shadow-none"
|
||||
on:click=move |_| editor.set(Some(AccountEditor::New))
|
||||
>
|
||||
"+ compte"
|
||||
</button>
|
||||
</AdminLayout>
|
||||
|
||||
{move || {
|
||||
editor
|
||||
.get()
|
||||
.map(|current| {
|
||||
view! {
|
||||
<AccountForm
|
||||
editor=current
|
||||
me_id
|
||||
on_close=move || editor.set(None)
|
||||
on_saved=move || version.update(|v| *v += 1)
|
||||
/>
|
||||
}
|
||||
})
|
||||
}}
|
||||
}
|
||||
.into_any()
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn AdminLayout(children: Children) -> impl IntoView {
|
||||
view! {
|
||||
<main class="mx-auto flex min-h-dvh max-w-md flex-col px-4 pt-[max(1.5rem,env(safe-area-inset-top))] pb-32">
|
||||
<header class="flex items-start justify-between gap-4">
|
||||
<h1 class="font-display text-5xl leading-[0.9] font-extrabold tracking-tight">"admin"</h1>
|
||||
<A
|
||||
href="/"
|
||||
attr:class="shrink-0 rounded-2xl border-2 border-ink bg-paper px-4 py-3 font-semibold"
|
||||
>
|
||||
"← mes listes"
|
||||
</A>
|
||||
</header>
|
||||
{children()}
|
||||
</main>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn AccountRows(
|
||||
accounts: Vec<Account>,
|
||||
me_id: i64,
|
||||
editor: RwSignal<Option<AccountEditor>>,
|
||||
) -> impl IntoView {
|
||||
view! {
|
||||
<p class="mt-1 text-lg text-ink/70">
|
||||
{match accounts.len() {
|
||||
1 => "1 compte.".to_owned(),
|
||||
n => format!("{n} comptes."),
|
||||
}}
|
||||
</p>
|
||||
<ul class="mt-5 flex flex-col gap-2">
|
||||
{accounts
|
||||
.into_iter()
|
||||
.map(|Account { user, created_at }| {
|
||||
let is_me = user.id == me_id;
|
||||
let edited = user.clone();
|
||||
view! {
|
||||
<li>
|
||||
<button
|
||||
class="flex w-full items-center justify-between gap-3 rounded-xl bg-mist px-3 py-2 text-left active:bg-ink/10"
|
||||
on:click=move |_| editor.set(Some(AccountEditor::Edit(edited.clone())))
|
||||
>
|
||||
<div class="min-w-0">
|
||||
<p class="truncate font-semibold">
|
||||
{user.name.clone()}
|
||||
{is_me.then_some(" (toi)")}
|
||||
</p>
|
||||
<p class="truncate text-sm text-ink/60">{user.email.clone()}</p>
|
||||
<p class="text-sm text-ink/50">{format!("Inscrit·e le {}", french_date(&created_at))}</p>
|
||||
</div>
|
||||
{user
|
||||
.is_admin
|
||||
.then(|| {
|
||||
view! {
|
||||
<span class="shrink-0 rounded-lg border-2 border-ink bg-lime px-2 py-0.5 font-mono text-xs uppercase">
|
||||
"admin"
|
||||
</span>
|
||||
}
|
||||
})}
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
})
|
||||
.collect_view()}
|
||||
</ul>
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn AccountForm(
|
||||
editor: AccountEditor,
|
||||
me_id: i64,
|
||||
on_close: impl Fn() + Copy + Send + Sync + 'static,
|
||||
on_saved: impl Fn() + Copy + Send + Sync + 'static,
|
||||
) -> impl IntoView {
|
||||
let auth = expect_context::<Auth>();
|
||||
let existing = match editor {
|
||||
AccountEditor::Edit(user) => Some(user),
|
||||
AccountEditor::New => None,
|
||||
};
|
||||
let user_id = existing.as_ref().map(|u| u.id);
|
||||
let is_me = user_id == Some(me_id);
|
||||
let name = RwSignal::new(
|
||||
existing
|
||||
.as_ref()
|
||||
.map(|u| u.name.clone())
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
let email = RwSignal::new(
|
||||
existing
|
||||
.as_ref()
|
||||
.map(|u| u.email.clone())
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
let password = RwSignal::new(String::new());
|
||||
let is_admin = RwSignal::new(existing.as_ref().is_some_and(|u| u.is_admin));
|
||||
let error = RwSignal::new(None::<String>);
|
||||
let pending = RwSignal::new(false);
|
||||
|
||||
let run = move |call: std::pin::Pin<Box<dyn Future<Output = Result<(), ServerFnError>>>>| {
|
||||
pending.set(true);
|
||||
spawn_local(async move {
|
||||
let result = call.await;
|
||||
pending.set(false);
|
||||
match result {
|
||||
Ok(()) => {
|
||||
if is_me {
|
||||
// Name shown on the avatar.
|
||||
auth.me.refetch();
|
||||
}
|
||||
on_saved();
|
||||
on_close();
|
||||
}
|
||||
Err(err) => error.set(Some(error_message(err))),
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
let submit = move |ev: ev::SubmitEvent| {
|
||||
ev.prevent_default();
|
||||
let (name, email, password, is_admin) =
|
||||
(name.get(), email.get(), password.get(), is_admin.get());
|
||||
run(match user_id {
|
||||
Some(id) => Box::pin(update_account(id, name, email, password, is_admin)),
|
||||
None => Box::pin(create_account(name, email, password, is_admin)),
|
||||
});
|
||||
};
|
||||
let delete = move |_| {
|
||||
let Some(id) = user_id else { return };
|
||||
let confirmed = window()
|
||||
.confirm_with_message("Supprimer ce compte et toutes les listes qu'il possède ?")
|
||||
.unwrap_or(false);
|
||||
if confirmed {
|
||||
run(Box::pin(delete_account(id)));
|
||||
}
|
||||
};
|
||||
|
||||
let title = if user_id.is_some() {
|
||||
"Modifier le compte"
|
||||
} else {
|
||||
"Nouveau compte"
|
||||
};
|
||||
let password_label = if user_id.is_some() {
|
||||
"Nouveau mot de passe (facultatif)"
|
||||
} else {
|
||||
"Mot de passe (8 caractères min.)"
|
||||
};
|
||||
|
||||
view! {
|
||||
<Sheet title=title.to_owned() on_close>
|
||||
<form class="mt-4 flex flex-col gap-4" on:submit=submit>
|
||||
<FormError error=error />
|
||||
<Field label="Prénom ou pseudo">
|
||||
<input class="field" required maxlength="40" autocomplete="off" bind:value=name />
|
||||
</Field>
|
||||
<Field label="Email">
|
||||
<input class="field" type="email" required autocomplete="off" bind:value=email />
|
||||
</Field>
|
||||
<Field label=password_label>
|
||||
<input
|
||||
class="field"
|
||||
type="password"
|
||||
required=user_id.is_none()
|
||||
minlength="8"
|
||||
maxlength="128"
|
||||
autocomplete="new-password"
|
||||
bind:value=password
|
||||
/>
|
||||
</Field>
|
||||
<label class="flex items-center gap-3 font-medium">
|
||||
<input
|
||||
type="checkbox"
|
||||
class="size-6 accent-ink"
|
||||
disabled=is_me
|
||||
bind:checked=is_admin
|
||||
/>
|
||||
"Administrateur"
|
||||
</label>
|
||||
|
||||
<div class="mt-2 flex gap-3">
|
||||
{(user_id.is_some() && !is_me)
|
||||
.then(|| {
|
||||
view! {
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-2xl border-2 border-ink px-4 py-3 font-semibold"
|
||||
disabled=move || pending.get()
|
||||
on:click=delete
|
||||
>
|
||||
"Supprimer"
|
||||
</button>
|
||||
}
|
||||
})}
|
||||
<button
|
||||
type="submit"
|
||||
class="tile-shadow flex-1 rounded-2xl border-2 border-ink bg-pink px-4 py-3 font-display text-lg font-bold active:translate-y-1 active:shadow-none disabled:opacity-60"
|
||||
disabled=move || pending.get()
|
||||
>
|
||||
{if user_id.is_some() { "Enregistrer" } else { "Créer" }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</Sheet>
|
||||
}
|
||||
}
|
||||
|
||||
/// "2026-10-07" → "07/10/2026".
|
||||
fn french_date(iso: &str) -> String {
|
||||
match iso.split('-').collect::<Vec<_>>().as_slice() {
|
||||
[y, m, d] => format!("{d}/{m}/{y}"),
|
||||
_ => iso.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::french_date;
|
||||
|
||||
#[test]
|
||||
fn formats_french_date() {
|
||||
assert_eq!(french_date("2026-10-07"), "07/10/2026");
|
||||
assert_eq!(french_date("garbage"), "garbage");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod account;
|
||||
mod admin;
|
||||
mod icons;
|
||||
mod live;
|
||||
mod sheets;
|
||||
@@ -17,6 +18,7 @@ use leptos_router::path;
|
||||
use crate::api::{get_items, get_lists, get_suggestions};
|
||||
use crate::model::{Item, ShoppingList, Suggestion, User};
|
||||
use account::{AccountSheet, Auth, LoginPage, RegisterPage, RequireUser};
|
||||
use admin::AdminPage;
|
||||
use sheets::{ItemEditor, ItemSheet, ListEditor, ListSheet};
|
||||
use tiles::{BasketTile, ItemTile, SuggestionTile};
|
||||
|
||||
@@ -72,6 +74,7 @@ pub fn App() -> impl IntoView {
|
||||
<Route path=path!("/l/:id") view=ListsPage />
|
||||
<Route path=path!("/connexion") view=LoginPage />
|
||||
<Route path=path!("/inscription") view=RegisterPage />
|
||||
<Route path=path!("/admin") view=AdminPage />
|
||||
</Routes>
|
||||
</Router>
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user