Skip to content

Commit

Permalink
JSコードのフォーマットをする
Browse files Browse the repository at this point in the history
  • Loading branch information
yocchan-git committed Feb 23, 2024
1 parent ca7f6c8 commit fab4392
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
60 changes: 30 additions & 30 deletions app/javascript/following.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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)
})
}
}

Expand All @@ -98,4 +98,4 @@ document.addEventListener('DOMContentLoaded', () => {
}
})

window.usersIndex = usersIndex;
window.usersIndex = usersIndex
37 changes: 18 additions & 19 deletions app/javascript/search-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}` : '')
)
},
Expand All @@ -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',
Expand All @@ -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)
})
}
})

0 comments on commit fab4392

Please sign in to comment.