Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

都道府県別ユーザー一覧の非React化 #8034

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

Shrimprin
Copy link
Contributor

@Shrimprin Shrimprin commented Aug 25, 2024

Issue

概要

[都道府県別ユーザー一覧(http://localhost:3000/users/areas)をReactからRails標準(html.slim)に変更しました。

変更確認方法

  1. feature/rewrite-users-by-area-from-react-to-htmlをローカルに取り込む
  2. bin/setupを実行
  3. foreman start -f Procfile.devでローカルサーバを立ち上げ
  4. 適当なユーザーでログイン
  5. /users/areasにアクセスして以下を確認する
    • サイドメニューが以下スクショのように表示されている
      image
    • 都道府県のカードが以下スクショのように表示されている
      image
    • サイドメニューの東京都(3)をクリックすると東京都のユーザー一覧に遷移する
    • 都道府県のカードの宮城県のタイトルをクリックすると宮城県のユーザー一覧に遷移する
  6. /users/areas/東京都にアクセスして以下を確認する
    • サイドメニューが以下スクショのように表示されている
      image
    • ユーザーのカードが以下スクショのように表示されている
      image
  7. users/aras/茨城県にアクセスして以下を確認する
    • 以下スクショのように「まだユーザーがいません」というメッセージが表示される
      image

変更前

  • サイドメニューから選択した都道府県のユーザー一覧を表示
  • 都道府県が選択されていない時は東京都を表示
    image

変更後

都道府県一覧

  • ユーザーの多い順に都道府県の一覧を表示
  • 都道府県カードのタイトルまたはサイドメニューから選択した都道府県のユーザー一覧ページに遷移
    image

選択した都道府県のユーザー一覧

image

@Shrimprin Shrimprin changed the title 都道府県ユーザー一覧の非React化 都道府県別ユーザー一覧の非React化 Aug 25, 2024
@Shrimprin Shrimprin force-pushed the feature/rewrite-users-by-area-from-react-to-html branch from 637446a to 2422cbb Compare August 28, 2024 14:37
@Shrimprin
Copy link
Contributor Author

@machida
お疲れ様です!
デザインの依頼をさせていただけますでしょうか 🙏
CSSクラスはReact版のものを流用していますが、手直しする必要があればご対応をお願いいたします。
ご確認いただきたいのは以下の2画面です。

よろしくお願いいたします 🙇‍♂️

@machida
Copy link
Member

machida commented Aug 30, 2024

@Shrimprin

確認しました!!
バッチリでした。
このままこれでリリースしたいと思います。
ほんの少しだけいじったものをpushしました。

実データを反映させたら、また新たな問題が見つかるかもしれませんが(東京が画面を埋め尽くすなど)、それは別Issueで対応していきたいと思いますー。

@machida machida removed their assignment Aug 30, 2024
@Shrimprin
Copy link
Contributor Author

@machida
ご確認と修正ありがとうございます!
このままメンバーレビューへと進めさせていただきます〜

@Shrimprin Shrimprin marked this pull request as ready for review August 30, 2024 10:24
@Shrimprin
Copy link
Contributor Author

@goruchanchan
お疲れ様です。
本PRのレビューをお願いできますでしょうか 🙏
もしご都合が悪ければ遠慮なくおっしゃってください!

よろしくお願いいたします 🙇‍♂️

@goruchanchan
Copy link
Contributor

@Shrimprin レビュー依頼ありがとうございます!ぜひやらせていただきます!一週間程度で確認しますので今しばらくお待ちください🙇‍♂️

@Shrimprin
Copy link
Contributor Author

@goruchanchan
ありがとうございます!よろしくお願いいたします 🙏

Copy link
Contributor

@goruchanchan goruchanchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shrimprin

お疲れ様です!レビュー致しました🙇‍♂️ご確認よろしくお願いします🙇‍♂️

if region == '海外'
country = ISO3166::Country.find_country_by_any_name(area)
def users_by_area(area)
if (subdivision = ISO3166::Country[:JP].find_subdivision_by_name(area))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

条件式での代入は == と勘違いしそうで避けた方がいいじゃないかと思いました🙇‍♂️

@@ -62,6 +59,17 @@ def number_of_users_by_region
end
end

def sorted_users_group_by_areas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted が指すのは「エリア毎のユーザ数」だと思うのですが、created_at とどっちを指すのかがわかりにくいと思いました。
また、返すのは「複数のgroup」だと思うので sourted_user_groups_by_area_user_num はどうでしょうか?

また、ユーザを取得する段階で created_at でソートしてしまってもいいんじゃないかなと思いました

def sourted_user_groups_by_area_user_num
  users_grouped_by_areas = User.with_attached_avatar.order(created_at: :desc).group_by(&:area)

  users_grouped_by_non_nil_area = users_grouped_by_areas.map do |area, users|
    { users:, area: } unless area.nil?
  end.compact

  users_grouped_by_non_nil_area.sort_by { |hash| -hash[:users].size }
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted が指すのは「エリア毎のユーザ数」だと思うのですが、created_at とどっちを指すのかがわかりにくいと思いました。
また、返すのは「複数のgroup」だと思うので sourted_user_groups_by_area_user_num はどうでしょうか?

ご指摘ありがとうございます。
ご提案いただいた変数名の方がい理解しやすそうなので修正します。

また、ユーザを取得する段階で created_at でソートしてしまってもいいんじゃないかなと思いました

そのほうがコードがスッキリしそうですね!修正します。

def users(region, area)
if region == '海外'
country = ISO3166::Country.find_country_by_any_name(area)
def users_by_area(area)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ユーザを返すならユーザモデルの方が適当なのかと思ったんですが、いかがでしょうか?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おっしゃる通りですね!
Userモデルに移します。

end

def show
@area = params[:area_name]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いくつか area_name が登場すると思うのですが、 area ではダメなのでしょうか?

@Shrimprin
Copy link
Contributor Author

Shrimprin commented Sep 3, 2024

コメントいただきありがとうございます!
以下5点を修正させていただきます。

  • 条件式での代入をやめる
  • sorted_users_group_by_areas -> sourted_user_groups_by_area_user_numに修正
  • 上記メソッドで、users取得時にcreated_atでソートする
  • users_by_areaUserモデルに移す
  • パラメータarea_name -> areaに修正

@Shrimprin Shrimprin force-pushed the feature/rewrite-users-by-area-from-react-to-html branch from 775cd7a to d3d4e9f Compare September 8, 2024 12:55
@Shrimprin
Copy link
Contributor Author

@goruchanchan
お疲れ様です!
コメントいただいた内容を修正しましたので再度ご確認をお願いできますでしょうか 🙏

Copy link
Contributor

@goruchanchan goruchanchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shrimprin お疲れ様です!ご対応ありがとうございます!細かなところで恐縮ですが2点ほど確認いただきたいです🙇‍♂️

ul.page-nav__items
- number_of_users_by_region[region].each do |area, number_of_users|
li.page-nav__item
= link_to "#{area}(#{number_of_users})", users_area_path(area: area), class: 'page-nav__item-link a-text-link'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かなところで恐縮ですが省略形で書けませんか?

Suggested change
= link_to "#{area}(#{number_of_users})", users_area_path(area: area), class: 'page-nav__item-link a-text-link'
= link_to "#{area}(#{number_of_users})", users_area_path(area:), class: 'page-nav__item-link a-text-link'


def show
@area = params[:area]
@users = User.users_by_area(@area).page(params[:page]).per(15)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User.users_by_area(@area) の呼び出し方を見ると下記ようなメソッド名でもいいかと思いました🙇‍♂️あくまで個人的にそう感じただけなので参考程度と思っていただければと思います🙇‍♂️

Suggested change
@users = User.users_by_area(@area).page(params[:page]).per(15)
@users = User.by_area(@area).page(params[:page]).per(15)

@Shrimprin
Copy link
Contributor Author

@goruchanchan
お疲れ様です!
追加のコメントありがとうございます。
2点対応しましたので確認お願いできますでしょうか 🙏

Copy link
Contributor

@goruchanchan goruchanchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shrimprin お疲れ様です!LGTMと思いますので承認いたします!非React化とても勉強になりました!ありがとうございました🙏

@Shrimprin
Copy link
Contributor Author

@goruchanchan
お疲れ様です!確認ありがとうございます。
こちらこそ命名や簡潔な書き方などいろいろとコメントいただけて勉強になりました~
引き続きよろしくお願いいたします 🙏

@Shrimprin
Copy link
Contributor Author

@komagata
お疲れ様です。
メンバーからApproveいただきましたのでレビューをお願いできますでしょうか 🙏

@@ -3,5 +3,12 @@
class Users::AreasController < ApplicationController
def index
@number_of_users_by_region = Area.number_of_users_by_region
@sorted_user_groups_by_area_user_num = Kaminari.paginate_array(Area.sorted_user_groups_by_area_user_num).page(params[:page]).per(15)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15

マジックナンバーになっているので定数か変数にしてみてください。
他の場所のコードを参考にしてみてください〜。

@@ -730,4 +730,20 @@ class UserTest < ActiveSupport::TestCase
test '.users_job returns all users when invalid job is passed' do
assert_equal User.all, User.users_job('destroy_all')
end

test 'area' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test 'area' do
test '#area' do

Shrimprin and others added 24 commits September 13, 2024 18:14
    - メソッドが返すのはエリアごとに分けられた「複数のグループ」のため、メソッド名をわかりやすく修正
    - メソッド名の変更に伴い、テストを修正
    - メソッド名の変更に伴い、コントローラーからの呼び出しを修正
    - ユーザー取得時にソートするように修正
- user_by_areaメソッドはusersを返すためUserモデルに移動
- テストやコントローラを合わせて修正
@Shrimprin Shrimprin force-pushed the feature/rewrite-users-by-area-from-react-to-html branch from 132f6b4 to d1ab442 Compare September 13, 2024 09:15
@Shrimprin
Copy link
Contributor Author

@komagata
レビューありがとうございます!
コメントいただいた2点を修正しましたので再度ご確認をお願いいたします 🙏

@@ -0,0 +1,10 @@
- number_of_users_by_region.each do |region, _|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- number_of_users_by_region.each do |region, _|
- number_of_users_by_region.each do |region|

これは不要かも?

Comment on lines +5 to +6
- users.each do |user|
= render 'users/user', user:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- users.each do |user|
= render 'users/user', user:
= render users

こう書けた気がします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants