FIX: simplfies previous route handling (#18895)

This commits makes sure we correctly wait for the end of the transition to reopen the drawer on the correct channel/view. Also fixes a bug when previous URL was `/` and causing a double transition.
This commit is contained in:
Joffrey JAFFEUX 2022-11-07 14:48:18 +01:00 committed by GitHub
parent f30f9ec5d9
commit 7fca07821b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 32 deletions

View File

@ -20,7 +20,6 @@ import discourseLater from "discourse-common/lib/later";
import { inject as service } from "@ember/service";
import { Promise } from "rsvp";
import { resetIdle } from "discourse/lib/desktop-notifications";
import { defaultHomepage } from "discourse/lib/utilities";
import { capitalize } from "@ember/string";
import {
onPresenceChange,
@ -1269,12 +1268,14 @@ export default Component.extend({
this.chatPreferredMode.setDrawer();
this.appEvents.trigger("chat:open-channel", channel);
const previousRouteInfo = this.fullPageChat.exit();
if (previousRouteInfo) {
this._transitionToRoute(previousRouteInfo);
} else {
this.router.transitionTo(`discovery.${defaultHomepage()}`);
let previousURL = this.fullPageChat.exit();
if (!previousURL || previousURL === "/") {
previousURL = "discovery";
}
this.router.replaceWith(previousURL).then(() => {
this.appEvents.trigger("chat:open-channel", channel);
});
},
@action
@ -1433,18 +1434,6 @@ export default Component.extend({
});
},
_transitionToRoute(routeInfo) {
const routeName = routeInfo.name;
let params = [];
do {
params = Object.values(routeInfo.params).concat(params);
routeInfo = routeInfo.parent;
} while (routeInfo);
this.router.transitionTo(routeName, ...params);
},
@bind
_forceBodyScroll() {
// when keyboard is visible this will ensure body

View File

@ -14,12 +14,12 @@ export default class ChatRoute extends DiscourseRoute {
return I18n.t("chat.title_capitalized");
}
beforeModel(transition) {
beforeModel() {
if (!this.chat.userCanChat) {
return this.transitionTo(`discovery.${defaultHomepage()}`);
}
this.fullPageChat.enter(transition?.from);
this.fullPageChat.enter(this.router.currentURL);
}
activate() {

View File

@ -1,17 +1,17 @@
import Service from "@ember/service";
export default class FullPageChat extends Service {
_previousRouteInfo = null;
_previousURL = null;
_isActive = false;
enter(previousRouteInfo) {
this._previousRouteInfo = previousRouteInfo;
enter(previousURL) {
this._previousURL = previousURL;
this._isActive = true;
}
exit() {
this._isActive = false;
return this._previousRouteInfo;
return this._previousURL;
}
get isActive() {

View File

@ -7,6 +7,7 @@ RSpec.describe "Navigation", type: :system, js: true do
fab!(:user) { Fabricate(:admin) }
fab!(:category_channel) { Fabricate(:category_channel) }
fab!(:message) { Fabricate(:chat_message, chat_channel: category_channel) }
let(:chat_page) { PageObjects::Pages::Chat.new }
before do
# ensures we have one valid registered admin
@ -21,7 +22,7 @@ RSpec.describe "Navigation", type: :system, js: true do
context "when visiting /chat" do
it "opens full page" do
visit("/chat")
chat_page.open_full_page
expect(page).to have_current_path(
chat.channel_path(category_channel.id, category_channel.slug),
@ -34,7 +35,7 @@ RSpec.describe "Navigation", type: :system, js: true do
context "when opening chat" do
it "opens the drawer by default" do
visit("/")
find(".open-chat").click
chat_page.open_from_header
expect(page).to have_css(".topic-chat-container.expanded.visible")
end
@ -43,15 +44,15 @@ RSpec.describe "Navigation", type: :system, js: true do
context "when opening chat with full page as preferred mode" do
it "opens the full page" do
visit("/")
find(".open-chat").click
find(".topic-chat-drawer-header__full-screen-btn").click
chat_page.open_from_header
chat_page.maximize_drawer
expect(page).to have_current_path(
chat.channel_path(category_channel.id, category_channel.slug),
)
visit("/")
find(".open-chat").click
chat_page.open_from_header
expect(page).to have_current_path(
chat.channel_path(category_channel.id, category_channel.slug),
@ -61,15 +62,36 @@ RSpec.describe "Navigation", type: :system, js: true do
context "when opening chat with drawer as preferred mode" do
it "opens the full page" do
visit("/chat")
find(".chat-full-screen-button").click
chat_page.open_full_page
chat_page.minimize_full_page
expect(page).to have_css(".topic-chat-container.expanded.visible")
visit("/")
find(".open-chat").click
chat_page.open_from_header
expect(page).to have_css(".topic-chat-container.expanded.visible")
end
end
context "when collapsing full page with no previous state" do
it "redirects to home page" do
chat_page.open_full_page
chat_page.minimize_full_page
expect(page).to have_current_path("/")
end
end
context "when collapsing full page with previous state" do
it "redirects to previous state" do
visit("/t/-/#{topic.id}")
chat_page.open_from_header
chat_page.maximize_drawer
chat_page.minimize_full_page
expect(page).to have_current_path("/t/#{topic.slug}/#{topic.id}")
expect(page).to have_css(".chat-message-container[data-id='#{message.id}']")
end
end
end

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
module PageObjects
module Pages
class Chat < PageObjects::Pages::Base
def open_from_header
find(".open-chat").click
end
def open_full_page
visit("/chat")
end
def maximize_drawer
find(".topic-chat-drawer-header__full-screen-btn").click
end
def minimize_full_page
find(".chat-full-screen-button").click
end
end
end
end