mirror of
https://github.com/ae-utbm/sith.git
synced 2026-05-14 04:58:06 +00:00
Fix subscription form
This commit is contained in:
@@ -1,38 +1,43 @@
|
|||||||
import { userFetchUser } from "#openapi";
|
import { userFetchUser } from "#openapi";
|
||||||
|
|
||||||
document.addEventListener("alpine:init", () => {
|
Alpine.data("existing_user_subscription_form", () => ({
|
||||||
Alpine.data("existing_user_subscription_form", () => ({
|
loading: false,
|
||||||
loading: false,
|
selectedUser: "",
|
||||||
profileFragment: "" as string,
|
profileFragment: "",
|
||||||
|
dateOfBirth: "",
|
||||||
|
dateOfBirthHidden: true,
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
const userSelect = document.getElementById("id_member") as HTMLSelectElement;
|
this.$watch("selectedUser", async () => {
|
||||||
userSelect.addEventListener("change", async () => {
|
await this.loadProfile(Number.parseInt(this.selectedUser, 10));
|
||||||
await this.loadProfile(Number.parseInt(userSelect.value, 10));
|
});
|
||||||
});
|
|
||||||
await this.loadProfile(Number.parseInt(userSelect.value, 10));
|
|
||||||
},
|
|
||||||
|
|
||||||
async loadProfile(userId: number) {
|
// Wait for web components to load
|
||||||
const birthdayInput = document.getElementById("id_birthdate") as HTMLInputElement;
|
await this.$nextTick();
|
||||||
if (!Number.isInteger(userId)) {
|
|
||||||
this.profileFragment = "";
|
// Force to detect the initial value
|
||||||
birthdayInput.hidden = true;
|
this.selectedUser = this.$refs.userSelect.widget.getValue();
|
||||||
return;
|
},
|
||||||
}
|
|
||||||
this.loading = true;
|
async loadProfile(userId: number) {
|
||||||
const [miniProfile, userInfos] = await Promise.all([
|
if (!Number.isInteger(userId)) {
|
||||||
fetch(`/user/${userId}/mini/`),
|
this.profileFragment = "";
|
||||||
// biome-ignore lint/style/useNamingConvention: api is snake_case
|
this.dateOfBirth = "";
|
||||||
userFetchUser({ path: { user_id: userId } }),
|
this.dateOfBirthHidden = true;
|
||||||
]);
|
return;
|
||||||
this.profileFragment = await miniProfile.text();
|
}
|
||||||
// If the user has no birthdate yet, show the form input
|
this.loading = true;
|
||||||
// to fill this info.
|
const [miniProfile, userInfos] = await Promise.all([
|
||||||
// Else keep the input hidden and change its value to the user birthdate
|
fetch(`/user/${userId}/mini/`),
|
||||||
birthdayInput.value = userInfos.data.date_of_birth;
|
// biome-ignore lint/style/useNamingConvention: api is snake_case
|
||||||
birthdayInput.hidden = userInfos.data.date_of_birth !== null;
|
userFetchUser({ path: { user_id: userId } }),
|
||||||
this.loading = false;
|
]);
|
||||||
},
|
this.profileFragment = await miniProfile.text();
|
||||||
}));
|
// If the user has no birthdate yet, show the form input
|
||||||
});
|
// to fill this info.
|
||||||
|
// Else keep the input hidden and change its value to the user birthdate
|
||||||
|
this.dateOfBirth = userInfos.data.date_of_birth;
|
||||||
|
this.dateOfBirthHidden = userInfos.data.date_of_birth !== null;
|
||||||
|
this.loading = false;
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|||||||
@@ -6,13 +6,28 @@
|
|||||||
>
|
>
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
||||||
<div x-data="existing_user_subscription_form" class="form-content existing-user">
|
{% set alpine_input_attrs = {
|
||||||
|
'id_member': 'x-ref=userSelect,x-model=selectedUser',
|
||||||
|
'id_birthdate': ':value=dateOfBirth,:hidden=dateOfBirthHidden',
|
||||||
|
} %}
|
||||||
|
|
||||||
|
{% set alpine_field_attrs = {
|
||||||
|
'id_birthdate': (':hidden=dateOfBirthHidden', 'x-cloack')
|
||||||
|
} %}
|
||||||
|
|
||||||
|
<div x-data="existing_user_subscription_form()" class="form-content existing-user">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
{{ form.errors }}
|
{% if form.errors %}
|
||||||
|
{{ form.errors.__all__ }}
|
||||||
|
{% endif %}
|
||||||
{% for field in form %}
|
{% for field in form %}
|
||||||
<div class="form-group">
|
<div
|
||||||
|
class="form-group"
|
||||||
|
{% for attr in alpine_field_attrs.get(field.auto_id, []) %}
|
||||||
|
{{ attr }}
|
||||||
|
{% endfor %}>
|
||||||
{{ field.label_tag() }}
|
{{ field.label_tag() }}
|
||||||
{{ field }}
|
{{ field|add_attr(alpine_input_attrs.get(field.auto_id, "")) }}
|
||||||
{% if field.help_text %}
|
{% if field.help_text %}
|
||||||
<span class="helptext">{{ field.help_text }}</span>
|
<span class="helptext">{{ field.help_text }}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
Reference in New Issue
Block a user