From fab4392551b980d1b89fc8fe87d711c21ffd7e2f Mon Sep 17 00:00:00 2001 From: yocchan-git Date: Fri, 23 Feb 2024 20:34:18 +0900 Subject: [PATCH] =?UTF-8?q?JS=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E3=83=95?= =?UTF-8?q?=E3=82=A9=E3=83=BC=E3=83=9E=E3=83=83=E3=83=88=E3=82=92=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/javascript/following.js | 60 +++++++++++++++++------------------ app/javascript/search-user.js | 37 +++++++++++---------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/app/javascript/following.js b/app/javascript/following.js index 2446657fb6f..3df20cfca24 100644 --- a/app/javascript/following.js +++ b/app/javascript/following.js @@ -11,13 +11,13 @@ const usersIndex = { }, getVerb(isFollow) { - return isFollow ? 'PATCH' : 'POST'; + return isFollow ? 'PATCH' : 'POST' }, followOrChangeFollow(userId, isFollow, isWatch) { const params = { id: userId - }; + } fetch(usersIndex.getUrl(userId, isFollow, isWatch), { method: usersIndex.getVerb(isFollow), headers: { @@ -29,25 +29,25 @@ const usersIndex = { redirect: 'manual', body: JSON.stringify(params) }) - .then(function(response) { - if (response.ok) { - return response.json(); - } else { - alert('フォロー処理に失敗しました'); - } - }) - .then(function(data) { - document.getElementById(`follow_${userId}`).innerHTML = data.html; - }) - .catch(function(error) { - console.warn(error); - }); + .then(function (response) { + if (response.ok) { + return response.json() + } else { + alert('フォロー処理に失敗しました') + } + }) + .then(function (data) { + document.getElementById(`follow_${userId}`).innerHTML = data.html + }) + .catch(function (error) { + console.warn(error) + }) }, unfollow(userId, isWatch) { const params = { id: userId - }; + } fetch(usersIndex.getUrl(userId, true, isWatch), { method: 'DELETE', headers: { @@ -59,19 +59,19 @@ const usersIndex = { redirect: 'manual', body: JSON.stringify(params) }) - .then(function(response) { - if (response.ok) { - return response.json(); - } else { - alert('フォロー処理に失敗しました'); - } - }) - .then(function(data) { - document.getElementById(`follow_${userId}`).innerHTML = data.html; - }) - .catch(function(error) { - console.warn(error); - }); + .then(function (response) { + if (response.ok) { + return response.json() + } else { + alert('フォロー処理に失敗しました') + } + }) + .then(function (data) { + document.getElementById(`follow_${userId}`).innerHTML = data.html + }) + .catch(function (error) { + console.warn(error) + }) } } @@ -98,4 +98,4 @@ document.addEventListener('DOMContentLoaded', () => { } }) -window.usersIndex = usersIndex; +window.usersIndex = usersIndex diff --git a/app/javascript/search-user.js b/app/javascript/search-user.js index df2ee8fd670..e340138f8cd 100644 --- a/app/javascript/search-user.js +++ b/app/javascript/search-user.js @@ -5,8 +5,7 @@ const SearchUser = { timeoutId: null, validateSearchUsersWord(searchUsersWord) { - if (searchUsersWord.match(/^[\w-]+$/)) - return searchUsersWord.length >= 3 + if (searchUsersWord.match(/^[\w-]+$/)) return searchUsersWord.length >= 3 return searchUsersWord.length >= 2 }, addParams(searchUsersWord) { @@ -26,7 +25,7 @@ const SearchUser = { `?page=${currentPage}` + (params.target ? `&target=${params.target}` : '') + (params.watch ? `&watch=${params.watch}` : '') + - "&require_html=true" + + '&require_html=true' + (searchWordParams ? `&${searchWordParams}` : '') ) }, @@ -48,10 +47,10 @@ const SearchUser = { async fetchUsersResource(searchUsersWord) { if (this.controller) { - this.controller.abort(); + this.controller.abort() } - this.controller = new AbortController(); + this.controller = new AbortController() const usersResource = await fetch(SearchUser.url(searchUsersWord), { method: 'GET', @@ -67,38 +66,38 @@ const SearchUser = { return usersResource.json() }, setupSearchedUsers(searchUsersWord) { - clearTimeout(this.timeoutId); + clearTimeout(this.timeoutId) this.timeoutId = setTimeout(() => { SearchUser.fetchUsersResource(searchUsersWord) .then((response) => { - document.getElementById(`user_lists`).innerHTML = response.html; - SearchUser.fixPaginationLinks(); + document.getElementById(`user_lists`).innerHTML = response.html + SearchUser.fixPaginationLinks() }) .catch((error) => { if (error.name === 'AbortError') { // 非同期通信がキャンセルされた場合は何もしない } else { - console.warn(error); + console.warn(error) } - }); - }, 300); + }) + }, 300) }, fixPaginationLinks() { - const paginationLinks = document.querySelectorAll('.pagination a'); + const paginationLinks = document.querySelectorAll('.pagination a') paginationLinks.forEach((link) => { - link.href = link.href.replace('/api/users', '/users'); - }); + link.href = link.href.replace('/api/users', '/users') + }) } } document.addEventListener('DOMContentLoaded', () => { - const inputElement = document.getElementById('js-user-search-input'); + const inputElement = document.getElementById('js-user-search-input') if (inputElement) { - inputElement.addEventListener('input', function() { - const inputValue = this.value.trim(); - SearchUser.setupSearchedUsers(inputValue); - }); + inputElement.addEventListener('input', function () { + const inputValue = this.value.trim() + SearchUser.setupSearchedUsers(inputValue) + }) } })