FIX: ensures loading more doesn’t erase filter on browse (#19675)

This commit is contained in:
Joffrey JAFFEUX 2023-01-02 14:40:13 +01:00 committed by GitHub
parent e82689c1bf
commit b97fff444e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 14 deletions

View File

@ -5,16 +5,14 @@ CATEGORY_CHANNEL_EDITABLE_PARAMS = %i[auto_join_users allow_channel_wide_mention
class Chat::Api::ChatChannelsController < Chat::Api
def index
options = { status: params[:status] ? ChatChannel.statuses[params[:status]] : nil }.merge(
params.permit(:filter, :limit, :offset),
).symbolize_keys!
permitted = params.permit(:filter, :limit, :offset, :status)
options[:offset] = options[:offset].to_i
options[:limit] = (options[:limit] || 25).to_i
options = { filter: permitted[:filter], limit: (permitted[:limit] || 25).to_i }
options[:offset] = permitted[:offset].to_i
options[:status] = ChatChannel.statuses[permitted[:status]] ? permitted[:status] : nil
memberships = Chat::ChatChannelMembershipManager.all_for_user(current_user)
channels = Chat::ChatChannelFetcher.secured_public_channels(guardian, memberships, options)
serialized_channels =
channels.map do |channel|
ChatChannelSerializer.new(
@ -24,14 +22,10 @@ class Chat::Api::ChatChannelsController < Chat::Api
)
end
pagination_options =
options.slice(:offset, :limit, :filter).merge(offset: options[:offset] + options[:limit])
pagination_params = pagination_options.map { |k, v| "#{k}=#{v}" }.join("&")
render json: serialized_channels,
root: "channels",
meta: {
load_more_url: "/chat/api/channels?#{pagination_params}",
}
load_more_params = options.merge(offset: options[:offset] + options[:limit]).to_query
load_more_url = URI::HTTP.build(path: "/chat/api/channels", query: load_more_params)
render json: serialized_channels, root: "channels", meta: { load_more_url: load_more_url }
end
def destroy

View File

@ -149,6 +149,18 @@ RSpec.describe "Browse page", type: :system, js: true do
expect(browse_view).to have_no_content(category_channel_4.name)
end
context "when loading more" do
fab!(:valid_channel) { Fabricate(:chat_channel, status: :open) }
fab!(:invalid_channel) { Fabricate(:chat_channel, status: :closed) }
it "keeps the filter" do
visit("/chat/browse/open")
expect(page).to have_content(valid_channel.title)
expect(page).to have_no_content(invalid_channel.title)
end
end
include_examples "never visible channels" do
before { visit("/chat/browse/open") }
end
@ -164,6 +176,18 @@ RSpec.describe "Browse page", type: :system, js: true do
expect(browse_view).to have_no_content(category_channel_4.name)
end
context "when loading more" do
fab!(:valid_channel) { Fabricate(:chat_channel, status: :closed) }
fab!(:invalid_channel) { Fabricate(:chat_channel, status: :open) }
it "keeps the filter" do
visit("/chat/browse/closed")
expect(page).to have_content(valid_channel.title)
expect(page).to have_no_content(invalid_channel.title)
end
end
include_examples "never visible channels" do
before { visit("/chat/browse/closed") }
end
@ -181,6 +205,18 @@ RSpec.describe "Browse page", type: :system, js: true do
expect(browse_view).to have_content(category_channel_4.name)
end
context "when loading more" do
fab!(:valid_channel) { Fabricate(:chat_channel, status: :archived) }
fab!(:invalid_channel) { Fabricate(:chat_channel, status: :open) }
it "keeps the filter" do
visit("/chat/browse/archived")
expect(page).to have_content(valid_channel.title)
expect(page).to have_no_content(invalid_channel.title)
end
end
include_examples "never visible channels" do
before { visit("/chat/browse/archived") }
end