Ensure a min of 2 metadata input rows on profile edit form (#297)

This commit is contained in:
Michael Manfre 2022-12-27 19:45:51 -05:00 committed by GitHub
parent c6c3914cc7
commit efe6864418
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 7 deletions

View file

@ -42,11 +42,17 @@
set two to the first <input.{{ field.name }}-{{ name_two }}/> in f then set two to the first <input.{{ field.name }}-{{ name_two }}/> in f then
set two@value to item.{{ name_two }} set two@value to item.{{ name_two }}
end end
get the (@data-min-empty of #id_{{ field.name }})
set min_empty to it
if items.length < min_empty then
repeat (min_empty - items.length) times
call {{ field.name }}.addEmptyField()
end
"></span> "></span>
<div class="option"> <div class="option">
<span class="option-field"> <span class="option-field">
<button class="fa-solid fa-add" _="on click {{ field.name }}.addEmptyField() then halt"></button> <button class="fa-solid fa-add" title="Add Row" _="on click {{ field.name }}.addEmptyField() then halt"></button>
</span> </span>
</div> </div>
@ -58,7 +64,7 @@
<input type=text class="{{ field.name }}-{{ name_two }}" name="{{ field.name }}_{{ name_two }}" value=""> <input type=text class="{{ field.name }}-{{ name_two }}" name="{{ field.name }}_{{ name_two }}" value="">
</span> </span>
<div class="right"> <div class="right">
<button class="fa-solid fa-trash delete" <button class="fa-solid fa-trash delete" title="Delete Row"
_="on click remove (closest parent .option) _="on click remove (closest parent .option)
then {{ field.name }}.collect{{ field.name|title }}Fields() then {{ field.name }}.collect{{ field.name|title }}Fields()
then halt" /> then halt" />

View file

@ -50,10 +50,18 @@ class ProfilePage(FormView):
metadata = forms.JSONField( metadata = forms.JSONField(
label="Profile Metadata Fields", label="Profile Metadata Fields",
help_text="These values will appear on your profile below your Bio", help_text="These values will appear on your profile below your Bio",
widget=forms.HiddenInput, widget=forms.HiddenInput(attrs={"data-min-empty": 2}),
required=False, required=False,
) )
def clean_metadata(self):
metadata = self.cleaned_data["metadata"]
if metadata:
metadata = [x for x in metadata if x["name"] and x["value"]]
if not metadata:
return None
return metadata
def get_initial(self): def get_initial(self):
identity = self.request.identity identity = self.request.identity
return { return {
@ -63,7 +71,7 @@ class ProfilePage(FormView):
"image": identity.image and identity.image.url, "image": identity.image and identity.image.url,
"discoverable": identity.discoverable, "discoverable": identity.discoverable,
"visible_follows": identity.config_identity.visible_follows, "visible_follows": identity.config_identity.visible_follows,
"metadata": identity.metadata, "metadata": identity.metadata or [],
} }
def form_valid(self, form): def form_valid(self, form):
@ -85,9 +93,7 @@ class ProfilePage(FormView):
image.name, image.name,
resize_image(image, size=(1500, 500)), resize_image(image, size=(1500, 500)),
) )
metadata = form.cleaned_data.get("metadata") identity.metadata = form.cleaned_data.get("metadata")
if metadata:
identity.metadata = metadata
identity.save() identity.save()
identity.transition_perform(IdentityStates.edited) identity.transition_perform(IdentityStates.edited)