Skip to content

Commit

Permalink
招待URLの変更をHTTPリクエスト無しで行うように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
naokinaokiboo committed May 28, 2024
1 parent a895a56 commit 67537ec
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
9 changes: 8 additions & 1 deletion app/controllers/admin/invitation_url_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# frozen_string_literal: true

class Admin::InvitationUrlController < AdminController
def index; end
def index
@invitation_url_template = new_user_url(
company_id: 'dummy_company_id',
role: 'dummy_role',
course_id: 'dummy_course_id',
token: ENV['TOKEN'] || 'token'
)
end
end
13 changes: 0 additions & 13 deletions app/controllers/api/admin/invitation_url_controller.rb

This file was deleted.

31 changes: 11 additions & 20 deletions app/javascript/invitation-url-updater.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
document.addEventListener('DOMContentLoaded', async () => {
const invitationElements = Array.from(
document.querySelectorAll('.invitation__element select')
document.querySelectorAll('.invitation__element select')
)
const invitationUrl = document.querySelector('.js-invitation-url')
const invitationUrlText = document.querySelector('.js-invitation-url-text')
Expand All @@ -14,27 +14,18 @@ document.addEventListener('DOMContentLoaded', async () => {
const invitationRole = document.querySelector('.js-invitation-role')
const invitationCourse = document.querySelector('.js-invitation-course')

const params = new URLSearchParams()
params.append(
'company_id',
invitationCompany.options[invitationCompany.selectedIndex].value
)
params.append(
'role',
invitationRole.options[invitationRole.selectedIndex].value
)
params.append(
'course_id',
invitationCourse.options[invitationCourse.selectedIndex].value
)
const selectedCompanyId = invitationCompany.options[invitationCompany.selectedIndex].value
const selectedRole = invitationRole.options[invitationRole.selectedIndex].value
const selectedCourseId = invitationCourse.options[invitationCourse.selectedIndex].value

const response = await fetch(
`/api/admin/invitation_url?${params.toString()}`
).catch((error) => console.warn(error))
const json = await response.json().catch((error) => console.warn(error))
const invitationUrlTemplate = document.querySelector('.invitation__url').dataset.invitationUrlTemplate
const targetUrl = invitationUrlTemplate
.replace(/company_id=[^&]*/, `company_id=${selectedCompanyId}`)
.replace(/role=[^&]*/, `role=${selectedRole}`)
.replace(/course_id=[^&]*/, `course_id=${selectedCourseId}`)

invitationUrl.href = json.url
invitationUrlText.value = json.url
invitationUrl.href = targetUrl
invitationUrlText.value = targetUrl
}

invitationElements.forEach((invitationElement) => {
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/invitation_url/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ header.page-header
= label_tag :course, 'コース', class: 'a-form-label'
.a-button.is-md.is-secondary.is-select.is-block
= select_tag :course, options_from_collection_for_select(Course.order(:created_at), :id, :title), class: 'js-invitation-course'
.invitation__url
.invitation__url data-invitation-url-template=@invitation_url_template
.a-form-label
| URL
.invitation__url-inner.flex.gap-1
Expand Down
1 change: 0 additions & 1 deletion config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace 'admin' do
resource :count, controller: 'count', only: %i(show)
resources :companies, only: %i(index destroy)
resources :invitation_url, only: %i(index)
end
namespace 'mentor' do
resources :practices, only: %i(index)
Expand Down

0 comments on commit 67537ec

Please sign in to comment.