add: user settings ui

This commit is contained in:
trisua 2025-03-31 11:45:34 -04:00
parent e7e9b49195
commit f3c2157dfc
24 changed files with 1015 additions and 187 deletions

View file

@ -40,155 +40,15 @@
<span>{{ text "communities:action.leave" }}</span>
</button>
{% endif %} {% else %}
<button
href="/community/{{ community.title }}"
<a
href="/community/{{ community.title }}/manage"
class="button primary"
onclick="document.getElementById('manage').showModal()"
>
{{ icon "settings" }}
<span
>{{ text "communities:action.configure" }}</span
>
</button>
<dialog id="manage">
<div class="inner">
<div
id="manage_fields"
class="flex flex-col gap-2"
>
<div class="card-nest">
<div class="card small">
<b>Read access</b>
</div>
<div class="card">
<select
onchange="save_access(event, 'read')"
>
<option value="Everybody">
Everybody
</option>
<option value="Unlisted">
Unlisted
</option>
<option value="Private">
Private
</option>
</select>
</div>
</div>
<div class="card-nest">
<div class="card small">
<b>Write access</b>
</div>
<div class="card">
<select
onchange="save_access(event, 'write')"
>
<option value="Everybody">
Everybody
</option>
<option value="Joined">
Joined
</option>
<option value="Owner">
Owner only
</option>
</select>
</div>
</div>
</div>
<hr class="margin" />
<button
onclick="document.getElementById('manage').close(); save_context()"
>
{{ icon "check" }}
<span
>{{ text "dialog:action.save_and_close"
}}</span
>
</button>
</div>
</dialog>
<script>
setTimeout(() => {
const ui = ns("ui");
const settings = JSON.parse(
"{{ community_context_serde|safe }}",
);
ui.generate_settings_ui(
document.getElementById("manage_fields"),
[
[
["display_name", "Display title"],
"{{ community.context.display_name }}",
"input",
],
[
["description", "Description"],
"{{ community.context.description }}",
"textarea",
],
],
settings,
);
window.save_context = () => {
fetch(
"/api/v1/communities/{{ community.id }}/context",
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
context: settings,
}),
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
window.save_access = (event, mode) => {
const selected =
event.target.selectedOptions[0];
fetch(
`/api/v1/communities/{{ community.id }}/access/${mode}`,
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
access: selected.value,
}),
},
)
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
}, 250);
</script>
</a>
{% endif %}
</div>
{% endif %}

View file

@ -13,7 +13,7 @@
onsubmit="create_community_from_form(event)"
>
<div class="flex flex-col gap-1">
<label for="">{{ text "communities:label.name" }}</label>
<label for="title">{{ text "communities:label.name" }}</label>
<input
type="text"
name="title"

View file

@ -0,0 +1,222 @@
{% import "macros.html" as macros %} {% extends "root.html" %} {% block head %}
<title>Community settings - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav() }}
<main class="flex flex-col gap-2">
<div class="pillmenu">
<a href="#/general" data-tab-button="general" class="active"
>{{ text "settings:tab.general" }}</a
>
<a href="#/profile" data-tab-button="profile"
>{{ text "settings:tab.profile" }}</a
>
</div>
<div class="card tertiary w-full" data-tab="general">
<div id="manage_fields" class="flex flex-col gap-2">
<div class="card-nest" ui_ident="read_access">
<div class="card small">
<b>Read access</b>
</div>
<div class="card">
<select onchange="save_access(event, 'read')">
<option value="Everybody">Everybody</option>
<option value="Unlisted">Unlisted</option>
<option value="Private">Private</option>
</select>
</div>
</div>
<div class="card-nest" ui_ident="write_access">
<div class="card small">
<b>Write access</b>
</div>
<div class="card">
<select onchange="save_access(event, 'write')">
<option value="Everybody">Everybody</option>
<option value="Joined">Joined</option>
<option value="Owner">Owner only</option>
</select>
</div>
</div>
</div>
</div>
<div
class="card tertiary w-full hidden flex flex-col gap-2"
data-tab="profile"
>
<div class="card-nest" ui_ident="change_avatar">
<div class="card small">
<b>{{ text "settings:label.change_avatar" }}</b>
</div>
<form
class="card flex gap-2 flex-row flex-wrap items-center"
method="post"
enctype="multipart/form-data"
onsubmit="upload_avatar(event)"
>
<input
id="avatar_file"
name="file"
type="file"
accept="image/png,image/jpeg,image/avif,image/webp"
class="w-content"
/>
<button class="primary">{{ icon "check" }}</button>
</form>
</div>
<div class="card-nest" ui_ident="change_banner">
<div class="card small">
<b>{{ text "settings:label.change_banner" }}</b>
</div>
<form
class="card flex gap-2 flex-row flex-wrap items-center"
method="post"
enctype="multipart/form-data"
onsubmit="upload_banner(event)"
>
<input
id="banner_file"
name="file"
type="file"
accept="image/png,image/jpeg,image/avif,image/webp"
class="w-content"
/>
<button class="primary">{{ icon "check" }}</button>
</form>
</div>
</div>
<div class="flex gap-2 flex-wrap">
<button onclick="save_context()">
{{ icon "check" }}
<span>{{ text "general:action.save" }}</span>
</button>
<a href="/community/{{ community.title }}" class="button secondary">
{{ icon "arrow-left" }}
<span>{{ text "general:action.back" }}</span>
</a>
</div>
</main>
<script>
setTimeout(() => {
const ui = ns("ui");
const settings = JSON.parse("{{ community_context_serde|safe }}");
globalThis.upload_avatar = (e) => {
e.preventDefault();
e.target.querySelector("button").style.display = "none";
fetch("/api/v1/communities/{{ community.id }}/upload/avatar", {
method: "POST",
body: e.target.file.files[0],
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
e.target.querySelector("button").removeAttribute("style");
});
alert("Avatar upload in progress. Please wait!");
};
globalThis.upload_banner = (e) => {
e.preventDefault();
e.target.querySelector("button").style.display = "none";
fetch("/api/v1/communities/{{ community.id }}/upload/banner", {
method: "POST",
body: e.target.file.files[0],
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
e.target.querySelector("button").removeAttribute("style");
});
alert("Banner upload in progress. Please wait!");
};
globalThis.save_context = () => {
fetch("/api/v1/communities/{{ community.id }}/context", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
context: settings,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.save_access = (event, mode) => {
const selected = event.target.selectedOptions[0];
fetch(`/api/v1/communities/{{ community.id }}/access/${mode}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
access: selected.value,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
ui.refresh_container(document.getElementById("manage_fields"), [
"read_access",
"write_access",
"change_avatar",
"change_banner",
]);
ui.generate_settings_ui(
document.getElementById("manage_fields"),
[
[
["display_name", "Display title"],
"{{ community.context.display_name }}",
"input",
],
[
["description", "Description"],
"{{ community.context.description }}",
"textarea",
],
],
settings,
);
}, 250);
</script>
{% endblock %}

View file

@ -4,6 +4,7 @@
src="/api/v1/auth/profile/{{ username }}/avatar?selector_type={{ selector_type }}"
alt="@{{ username }}"
class="avatar shadow"
loading="lazy"
style="--size: {{ size }}"
/>
{%- endmacro %} {% macro community_avatar(id, community=false, size="24px") -%}
@ -12,6 +13,7 @@
src="/api/v1/communities/{{ id }}/avatar"
alt="{{ community.title }}"
class="avatar shadow"
loading="lazy"
style="--size: {{ size }}"
/>
{% else %}
@ -19,6 +21,7 @@
src="/api/v1/communities/{{ id }}/avatar"
alt="{{ id }}"
class="avatar shadow"
loading="lazy"
style="--size: {{ size }}"
/>
{% endif %} {%- endmacro %} {% macro community_listing_card(community) -%}
@ -28,7 +31,7 @@
>
{{ components::community_avatar(id=community.id, community=community,
size="48px") }}
<div class="flex flex-col gap-1">
<div class="flex flex-col">
<h3>{{ community.context.display_name }}</h3>
<span class="fade"><b>{{ community.member_count }}</b> members</span>
</div>

View file

@ -31,7 +31,11 @@ show_lhs=true) -%}
<div class="flex nav_side">
{% if user %}
<a href="/notifs" class="button" title="Notifications">
<a
href="/notifs"
class="button {% if selected == 'notifications' %}active{% endif %}"
title="Notifications"
>
{% if user.notification_count > 0 %} {{ icon "bell-dot" }}
<span class="notification tr"
>{{ user.notification_count }}</span

View file

@ -1,6 +1,6 @@
{% import "macros.html" as macros %} {% extends "root.html" %} {% block head %}
<title>Notifications - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav(selected="communities") }}
{% endblock %} {% block body %} {{ macros::nav(selected="notifications") }}
<main class="flex flex-col gap-2">
<button onclick="trigger('me::clear_notifs')">
{{ icon "bomb" }}

View file

@ -16,11 +16,7 @@
<div class="flex flex-col">
<!-- prettier-ignore -->
<h3 id="username" class="username">
{% if profile.settings.display_name %}
{{ profile.settings.display_name }}
{% else %}
{{ profile.username }}
{% endif %}
{{ components::username(user=profile) }}
{% if profile.is_verified %}
<span title="Verified">

View file

@ -0,0 +1,395 @@
{% import "macros.html" as macros %} {% extends "root.html" %} {% block head %}
<title>Settings - {{ config.name }}</title>
{% endblock %} {% block body %} {{ macros::nav() }}
<main class="flex flex-col gap-2">
<div class="pillmenu">
<a
data-tab-button="account"
class="active"
href="#/account"
onclick="show_save_button()"
>
{{ text "settings:tab.account" }}
</a>
<a
data-tab-button="profile"
href="#/profile"
onclick="show_save_button()"
>
{{ text "settings:tab.profile" }}
</a>
<a
data-tab-button="sessions"
href="#/sessions"
onclick="hide_save_button()"
>
{{ text "settings:tab.sessions" }}
</a>
</div>
<div class="card w-full tertiary" data-tab="account">
<div class="flex flex-col gap-2" id="account_settings">
<div class="card-nest" ui_ident="change_password">
<div class="card small">
<b>{{ text "settings:label.change_password" }}</b>
</div>
<form
class="card flex flex-col gap-2"
onsubmit="change_password(event)"
>
<div class="flex flex-col gap-1">
<label for="current_password"
>{{ text "settings:label.current_password" }}</label
>
<input
type="password"
name="current_password"
id="current_password"
placeholder="current_password"
required
minlength="6"
autocomplete="off"
/>
</div>
<div class="flex flex-col gap-1">
<label for="new_password"
>{{ text "settings:label.new_password" }}</label
>
<input
type="password"
name="new_password"
id="new_password"
placeholder="new_password"
required
minlength="6"
autocomplete="off"
/>
</div>
<button class="primary">
{{ icon "check" }}
<span>{{ text "general:action.save" }}</span>
</button>
</form>
</div>
<div class="card-nest" ui_ident="change_username">
<div class="card small">
<b>{{ text "settings:label.change_username" }}</b>
</div>
<form
class="card flex flex-col gap-2"
onsubmit="change_username(event)"
>
<div class="flex flex-col gap-1">
<label for="new_username"
>{{ text "settings:label.new_username" }}</label
>
<input
type="text"
name="new_username"
id="new_username"
placeholder="new_username"
required
minlength="2"
/>
</div>
<button class="primary">
{{ icon "check" }}
<span>{{ text "general:action.save" }}</span>
</button>
</form>
</div>
</div>
</div>
<div class="card w-full tertiary hidden" data-tab="profile">
<div class="flex flex-col gap-2" id="profile_settings">
<div class="card-nest" ui_ident="change_avatar">
<div class="card small">
<b>{{ text "settings:label.change_avatar" }}</b>
</div>
<form
class="card flex gap-2 flex-row flex-wrap items-center"
method="post"
enctype="multipart/form-data"
onsubmit="upload_avatar(event)"
>
<input
id="avatar_file"
name="file"
type="file"
accept="image/png,image/jpeg,image/avif,image/webp"
class="w-content"
/>
<button class="primary">{{ icon "check" }}</button>
</form>
</div>
<div class="card-nest" ui_ident="change_banner">
<div class="card small">
<b>{{ text "settings:label.change_banner" }}</b>
</div>
<form
class="card flex gap-2 flex-row flex-wrap items-center"
method="post"
enctype="multipart/form-data"
onsubmit="upload_banner(event)"
>
<input
id="banner_file"
name="file"
type="file"
accept="image/png,image/jpeg,image/avif,image/webp"
class="w-content"
/>
<button class="primary">{{ icon "check" }}</button>
</form>
</div>
</div>
</div>
<div
class="card w-full tertiary hidden flex flex-col gap-2"
data-tab="sessions"
>
{% for token in user.tokens %}
<div class="card w-full flex justify-between flex-collapse gap-2">
<div class="flex flex-col gap-1">
<b
style="
width: 200px;
overflow: hidden;
text-overflow: ellipsis;
"
>{{ token[1] }}</b
>
<span class="fade">{{ token[0] }}</span>
<span class="fade date">{{ token[2] }}</span>
</div>
<button
class="quaternary red"
onclick="remove_token('{{ token[1] }}')"
>
{{ text "general:action.delete" }}
</button>
</div>
{% endfor %}
</div>
<button onclick="save_settings()" id="save_button" data-turbo-permanent>
{{ icon "check" }}
<span>{{ text "general:action.save" }}</span>
</button>
<script>
function show_save_button() {
document.getElementById("save_button").removeAttribute("style");
}
function hide_save_button() {
document.getElementById("save_button").style.display = "none";
}
setTimeout(() => {
const ui = ns("ui");
const settings = JSON.parse("{{ user_settings_serde|safe }}");
let tokens = JSON.parse("{{ user_tokens_serde|safe }}");
globalThis.remove_token = async (id) => {
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
// reconstruct tokens (but without the token with the given id)
const new_tokens = [];
for (const token of tokens) {
if (token[1] === id) {
continue;
}
new_tokens.push(token);
}
tokens = new_tokens;
// send request to save
fetch("/api/v1/auth/profile/{{ user.id }}/tokens", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(tokens),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.save_settings = () => {
fetch("/api/v1/auth/profile/{{ user.id }}/settings", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(settings),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.change_password = (e) => {
e.preventDefault();
fetch("/api/v1/auth/profile/{{ user.id }}/password", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
from: e.target.current_password.value,
to: e.target.new_password.value,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.change_username = async (e) => {
e.preventDefault();
if (
!(await trigger("atto::confirm", [
"Are you sure you would like to do this?",
]))
) {
return;
}
fetch("/api/v1/auth/profile/{{ user.id }}/username", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
to: e.target.new_username.value,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
};
globalThis.upload_avatar = (e) => {
e.preventDefault();
e.target.querySelector("button").style.display = "none";
fetch("/api/v1/auth/upload/avatar", {
method: "POST",
body: e.target.file.files[0],
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
e.target
.querySelector("button")
.removeAttribute("style");
});
alert("Avatar upload in progress. Please wait!");
};
globalThis.upload_banner = (e) => {
e.preventDefault();
e.target.querySelector("button").style.display = "none";
fetch("/api/v1/auth/upload/banner", {
method: "POST",
body: e.target.file.files[0],
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
e.target
.querySelector("button")
.removeAttribute("style");
});
alert("Banner upload in progress. Please wait!");
};
const account_settings =
document.getElementById("account_settings");
const profile_settings =
document.getElementById("profile_settings");
ui.refresh_container(account_settings, [
"change_password",
"change_username",
]);
ui.refresh_container(profile_settings, [
"change_avatar",
"change_banner",
]);
ui.generate_settings_ui(
account_settings,
[
[
["display_name", "Display name"],
"{{ user.settings.display_name }}",
"input",
],
[
["biography", "Biography"],
"{{ user.settings.biography }}",
"textarea",
],
],
settings,
);
});
</script>
</main>
{% endblock %}