forked from rubygems/rubygems.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert API key form to stimulus, add exclusive checkbox (rubygems#4418)
* The form now helps you with exclusive scopes and auto-deselects the other checkboxes when exclusive scopes are chosen. * Convert api key form to partial and add missing translations keys
- Loading branch information
1 parent
4f94ab4
commit 7e9a0d3
Showing
21 changed files
with
349 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/javascript/controllers/exclusive_checkbox_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
// Supports one exclusive target, but many inclusive targets | ||
export default class extends Controller { | ||
static targets = ["inclusive", "exclusive"] | ||
|
||
connect() { | ||
// Unselect all inclusive targets if the exclusive target is selected on load | ||
this.updateExclusive() | ||
} | ||
|
||
exclusiveTargetConnected(el) { | ||
el.addEventListener("change", () => this.updateExclusive()) | ||
} | ||
|
||
inclusiveTargetConnected(el) { | ||
el.addEventListener("change", (e) => { | ||
if (e.currentTarget.checked) { this.uncheck(this.exclusiveTarget) } | ||
}) | ||
} | ||
|
||
updateExclusive() { | ||
if (this.exclusiveTarget.checked) { | ||
this.inclusiveTargets.forEach(this.uncheck) | ||
} | ||
} | ||
|
||
uncheck(checkbox) { | ||
if (checkbox.checked) { | ||
checkbox.checked = false | ||
checkbox.dispatchEvent(new Event("change")) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["checkbox", "selector"] | ||
|
||
connect() { | ||
this.toggleSelector() | ||
} | ||
|
||
checkboxTargetConnected(el) { | ||
el.addEventListener("change", () => this.toggleSelector()) | ||
} | ||
|
||
toggleSelector() { | ||
const selected = this.checkboxTargets.find((target) => target.checked) | ||
|
||
if (selected) { | ||
this.selectorTarget.disabled = false; | ||
this.removeHiddenRubygemField(); | ||
} else { | ||
this.selectorTarget.value = ""; | ||
this.selectorTarget.disabled = true; | ||
this.addHiddenRubygemField(); | ||
} | ||
} | ||
|
||
addHiddenRubygemField() { | ||
if (this.hiddenField) { return } | ||
this.hiddenField = document.createElement("input"); | ||
this.hiddenField.type = "hidden"; | ||
this.hiddenField.name = "api_key[rubygem_id]"; | ||
this.hiddenField.value = ""; | ||
this.element.appendChild(this.hiddenField); | ||
} | ||
|
||
removeHiddenRubygemField() { | ||
if (this.hiddenField) { | ||
this.hiddenField.remove(); | ||
this.hiddenField = null; | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<%= form_with model: [:profile, @api_key], data: { controller: "exclusive-checkbox gem-scope" } do |f| %> | ||
<%= label_tag :name, t("api_keys.index.name"), class: "form__label" %> | ||
<%= f.text_field :name, class: "form__input", autocomplete: :off %> | ||
|
||
<div class="form__group"> | ||
<%= label_tag :scope, t(".exclusive_scopes"), class: "form__label" %> | ||
<% ApiKey::EXCLUSIVE_SCOPES.each do |api_scope| %> | ||
<div class = "form__checkbox__item"> | ||
<%= api_key_checkbox(f, api_scope) %> | ||
<%= label_tag api_scope, t("api_keys.index.#{api_scope}"), class: "form__checkbox__label" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<div class="form__group"> | ||
<%= label_tag :scope, t("api_keys.index.scopes"), class: "form__label" %> | ||
<% (ApiKey::API_SCOPES - ApiKey::EXCLUSIVE_SCOPES).each do |api_scope| %> | ||
<div class = "form__checkbox__item"> | ||
<%= api_key_checkbox(f, api_scope) %> | ||
<%= label_tag api_scope, t("api_keys.index.#{api_scope}"), class: "form__checkbox__label" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="form__group api_key_rubygem_id_form"> | ||
<%= label_tag :rubygem_id, t(".rubygem_scope"), class: "form__label" %> | ||
<p><%= t(".rubygem_scope_info") %></p> | ||
<%= f.collection_select :rubygem_id, current_user.rubygems.by_name, :id, :name, { include_blank: t("api_keys.all_gems") }, selected: :rubygem_id, class: "form__input form__select", data: { gem_scope_target: "selector" } %> | ||
</div> | ||
|
||
<% unless current_user.mfa_disabled? || current_user.mfa_ui_and_api? %> | ||
<div class="form__group"> | ||
<%= label_tag :mfa, t(".multifactor_auth"), class: "form__label" %> | ||
<div class="form__checkbox__item"> | ||
<%= f.check_box :mfa, { class: "form__checkbox__input", id: :mfa, checked: true } , "true", "false" %> | ||
<%= label_tag :mfa, t(".enable_mfa"), class: "form__checkbox__label" %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<%= f.submit class: "form__submit" %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,5 @@ | ||
<% @title = t(".edit_api_key") %> | ||
|
||
<div class="t-body"> | ||
<%= form_for @api_key, url: profile_api_key_path do |f| %> | ||
<%= label_tag :name, t("api_keys.index.name"), class: "form__label" %> | ||
<%= f.text_field :name, class: "form__input", autocomplete: :off %> | ||
|
||
<div class="form__group"> | ||
<%= label_tag :scope, t("api_keys.index.scopes"), class: "form__label" %> | ||
<% ApiKey::API_SCOPES.each do |api_scope| %> | ||
<div class = "form__checkbox__item"> | ||
<%= f.check_box "#{api_scope}", { class: "form__checkbox__input", id: "#{api_scope}" } , "true", "false" %> | ||
<%= label_tag api_scope, t("api_keys.index.#{api_scope}"), class: "form__checkbox__label" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="form__group api_key_rubygem_id_form"> | ||
<%= label_tag :rubygem_id, t(".rubygem_scope"), class: "form__label" %> | ||
<p><%= t(".rubygem_scope_info") %></p> | ||
<%= f.collection_select :rubygem_id, current_user.rubygems.by_name, :id, :name, { include_blank: t("api_keys.all_gems") }, selected: :rubygem_id, class: "form__input form__select" %> | ||
</div> | ||
|
||
<% unless current_user.mfa_disabled? || current_user.mfa_ui_and_api? %> | ||
<div class="form__group"> | ||
<%= label_tag :mfa, t(".multifactor_auth"), class: "form__label" %> | ||
<div class="form__checkbox__item"> | ||
<%= f.check_box :mfa, { class: "form__checkbox__input", id: :mfa } , "true", "false" %> | ||
<%= label_tag :mfa, t(".enable_mfa"), class: "form__checkbox__label" %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<%= f.submit "Update", class: "form__submit" %> | ||
<% end %> | ||
<%= render "form", api_key: @api_key %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,5 @@ | ||
<% @title = t(".new_api_key") %> | ||
|
||
<div class="t-body"> | ||
<%= form_for @api_key, url: profile_api_keys_path do |f| %> | ||
<%= label_tag :name, t("api_keys.index.name"), class: "form__label" %> | ||
<%= f.text_field :name, class: "form__input", autocomplete: :off %> | ||
|
||
<div class="form__group"> | ||
<%= label_tag :scope, t("api_keys.index.scopes"), class: "form__label" %> | ||
<% ApiKey::API_SCOPES.each do |api_scope| %> | ||
<div class = "form__checkbox__item"> | ||
<%= f.check_box "#{api_scope}", { class: "form__checkbox__input", id: "#{api_scope}" } , "true", "false" %> | ||
<%= label_tag api_scope, t("api_keys.index.#{api_scope}"), class: "form__checkbox__label" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="form__group api_key_rubygem_id_form"> | ||
<%= label_tag :rubygem_id, t(".rubygem_scope"), class: "form__label" %> | ||
<p><%= t(".rubygem_scope_info") %></p> | ||
<%= f.collection_select :rubygem_id, current_user.rubygems.by_name, :id, :name, { include_blank: t("api_keys.all_gems") }, selected: :rubygem_id, class: "form__input form__select" %> | ||
</div> | ||
|
||
<% unless current_user.mfa_disabled? || current_user.mfa_ui_and_api? %> | ||
<div class="form__group"> | ||
<%= label_tag :mfa, t(".multifactor_auth"), class: "form__label" %> | ||
<div class="form__checkbox__item"> | ||
<%= f.check_box :mfa, { class: "form__checkbox__input", id: :mfa, checked: true } , "true", "false" %> | ||
<%= label_tag :mfa, t(".enable_mfa"), class: "form__checkbox__label" %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<%= f.submit "Create", class: "form__submit" %> | ||
<% end %> | ||
<%= render "form", api_key: @api_key %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.