DEV: Update replaceWith on Route (#22645)

Per https://deprecations.emberjs.com/v3.x/#toc_routing-transition-methods

We are upgrading all `this.replaceWith` calls on routes to directly call the router service (`this.router.replaceWith`)
This commit is contained in:
Isaac Janzen 2023-07-18 15:05:53 -05:00 committed by GitHub
parent 7422fe7c3f
commit 33db93c9b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 134 additions and 51 deletions

View File

@ -1,10 +1,15 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
export default class AdminCustomizeColorsShowRoute extends Route {
@service router;
model(params) {
const all = this.modelFor("adminCustomize.colors");
const model = all.findBy("id", parseInt(params.scheme_id, 10));
return model ? model : this.replaceWith("adminCustomize.colors.index");
return model
? model
: this.router.replaceWith("adminCustomize.colors.index");
}
serialize(model) {

View File

@ -1,7 +1,10 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
export default class AdminCustomizeEmailStyleIndexRoute extends Route {
@service router;
beforeModel() {
this.replaceWith("adminCustomizeEmailStyle.edit", "html");
this.router.replaceWith("adminCustomizeEmailStyle.edit", "html");
}
}

View File

@ -16,7 +16,7 @@ export default class AdminCustomizeThemesEditRoute extends Route {
target: params.target,
field_name: params.field_name,
}
: this.replaceWith("adminCustomizeThemes.index");
: this.router.replaceWith("adminCustomizeThemes.index");
}
serialize(wrapper) {

View File

@ -7,6 +7,7 @@ import { scrollTop } from "discourse/mixins/scroll-top";
export default class AdminCustomizeThemesShowRoute extends Route {
@service dialog;
@service router;
serialize(model) {
return { theme_id: model.get("id") };
@ -15,7 +16,7 @@ export default class AdminCustomizeThemesShowRoute extends Route {
model(params) {
const all = this.modelFor("adminCustomizeThemes");
const model = all.findBy("id", parseInt(params.theme_id, 10));
return model ? model : this.replaceWith("adminCustomizeThemes.index");
return model || this.router.replaceWith("adminCustomizeThemes.index");
}
setupController(controller, model) {

View File

@ -3,10 +3,13 @@
chosen a category. It will redirect to the first category.
**/
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class AdminSiteSettingsIndexRoute extends DiscourseRoute {
@service router;
beforeModel() {
this.replaceWith(
this.router.replaceWith(
"adminSiteSettingsCategory",
this.controllerFor("adminSiteSettings").get("visibleSiteSettings")[0]
.nameKey

View File

@ -1,8 +1,11 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class AdminWatchedWordsIndexRoute extends DiscourseRoute {
@service router;
beforeModel() {
this.replaceWith(
this.router.replaceWith(
"adminWatchedWords.action",
this.modelFor("adminWatchedWords")[0].nameKey
);

View File

@ -4,15 +4,18 @@ import { next } from "@ember/runloop";
import { popupAjaxError } from "discourse/lib/ajax-error";
import showModal from "discourse/lib/show-modal";
import cookie from "discourse/lib/cookie";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
beforeModel(transition) {
if (!this.currentUser) {
cookie("destination_url", transition.intent.url);
return this.replaceWith("login");
return this.router.replaceWith("login");
}
const params = this.paramsFor("associate-account");
this.replaceWith(`preferences.account`, this.currentUser).then(() =>
this.router.replaceWith(`preferences.account`, this.currentUser).then(() =>
next(() =>
ajax(`/associate/${encodeURIComponent(params.token)}.json`)
.then((model) => showModal("associate-account-confirm", { model }))

View File

@ -23,6 +23,7 @@ export default (filterArg, params) => {
return DiscourseRoute.extend({
queryParams,
composer: service(),
router: service(),
model(modelParams) {
const category = Category.findBySlugPathWithID(
@ -50,7 +51,7 @@ export default (filterArg, params) => {
afterModel(model, transition) {
if (!model) {
this.replaceWith("/404");
this.router.replaceWith("/404");
return;
}
@ -64,7 +65,7 @@ export default (filterArg, params) => {
) {
// TODO: avoid throwing away preload data by redirecting on the server
PreloadStore.getAndRemove("topic_list");
return this.replaceWith(
return this.router.replaceWith(
"discovery.categoryNone",
modelParams.category_slug_path_with_id
);

View File

@ -4,8 +4,10 @@ import Route from "@ember/routing/route";
import { once } from "@ember/runloop";
import { seenUser } from "discourse/lib/user-presence";
import { getOwner } from "discourse-common/lib/get-owner";
import { inject as service } from "@ember/service";
const DiscourseRoute = Route.extend({
router: service(),
showFooter: false,
willTransition() {
@ -49,7 +51,7 @@ const DiscourseRoute = Route.extend({
redirectIfLoginRequired() {
const app = this.controllerFor("application");
if (app.get("loginRequired")) {
this.replaceWith("login");
this.router.replaceWith("login");
}
},

View File

@ -8,8 +8,10 @@ import User from "discourse/models/user";
import { setTopicList } from "discourse/lib/topic-list-tracker";
import { action } from "@ember/object";
import { resetCachedTopicList } from "discourse/lib/cached-topic-list";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend(OpenComposer, {
router: service(),
queryParams: {
filter: { refreshModel: true },
},
@ -29,14 +31,14 @@ export default DiscourseRoute.extend(OpenComposer, {
User.currentProp("user_option.should_be_redirected_to_top", false);
const period =
User.currentProp("user_option.redirected_to_top.period") || "all";
this.replaceWith("discovery.top", {
this.router.replaceWith("discovery.top", {
queryParams: {
period,
},
});
} else if (url && (matches = url.match(/top\/(.*)$/))) {
if (this.site.periods.includes(matches[1])) {
this.replaceWith("discovery.top", {
this.router.replaceWith("discovery.top", {
queryParams: {
period: matches[1],
},

View File

@ -1,8 +1,11 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
afterModel() {
const params = this.paramsFor("editCategory");
this.replaceWith(`/c/${params.slug}/edit/general`);
this.router.replaceWith(`/c/${params.slug}/edit/general`);
},
});

View File

@ -1,8 +1,11 @@
import Category from "discourse/models/category";
import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
model(params) {
return Category.reloadCategoryWithPermissions(
params,
@ -13,7 +16,7 @@ export default DiscourseRoute.extend({
afterModel(model) {
if (!model.can_edit) {
this.replaceWith("/404");
this.router.replaceWith("/404");
return;
}
},

View File

@ -1,12 +1,15 @@
import DiscourseRoute from "discourse/routes/discourse";
import { defaultHomepage } from "discourse/lib/utilities";
import { next } from "@ember/runloop";
import { inject as service } from "@ember/service";
export default class ForgotPasswordRoute extends DiscourseRoute {
@service router;
async beforeModel() {
const { loginRequired } = this.controllerFor("application");
const e = await this.replaceWith(
const e = await this.router.replaceWith(
loginRequired ? "login" : `discovery.${defaultHomepage()}`
);

View File

@ -1,7 +1,9 @@
import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
showFooter: true,
titleToken() {
@ -10,7 +12,7 @@ export default DiscourseRoute.extend({
afterModel(group) {
if (group.get("automatic")) {
this.replaceWith("group.manage.interaction", group);
this.router.replaceWith("group.manage.interaction", group);
}
},
});

View File

@ -6,6 +6,7 @@ import StaticPage from "discourse/models/static-page";
export default class LoginRoute extends DiscourseRoute {
@service siteSettings;
@service router;
// `login-page` because `login` controller is the one for
// the login modal
@ -13,7 +14,7 @@ export default class LoginRoute extends DiscourseRoute {
beforeModel() {
if (!this.siteSettings.login_required) {
this.replaceWith(`/${defaultHomepage()}`).then((e) => {
this.router.replaceWith(`/${defaultHomepage()}`).then((e) => {
next(() => e.send("showLogin"));
});
}

View File

@ -2,6 +2,7 @@ import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { Promise } from "rsvp";
import { SEARCH_PRIORITIES } from "discourse/lib/constants";
import { inject as service } from "@ember/service";
let _newCategoryColor = "0088CC";
let _newCategoryTextColor = "FFFFFF";
@ -12,12 +13,13 @@ export function setNewCategoryDefaultColors(backgroundColor, textColor) {
}
export default DiscourseRoute.extend({
router: service(),
controllerName: "edit-category-tabs",
templateName: "edit-category-tabs",
beforeModel() {
if (!this.currentUser) {
this.replaceWith("/404");
this.router.replaceWith("/404");
return;
}
if (!this.currentUser.admin) {
@ -25,7 +27,7 @@ export default DiscourseRoute.extend({
!this.currentUser.moderator ||
this.siteSettings.moderators_manage_categories_and_groups === false
) {
this.replaceWith("/404");
this.router.replaceWith("/404");
}
}
},

View File

@ -7,6 +7,7 @@ import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
dialog: service(),
router: service(),
beforeModel(transition) {
const params = transition.to.queryParams;
@ -14,7 +15,7 @@ export default DiscourseRoute.extend({
const groupName = params.groupname || params.group_name;
if (this.currentUser) {
this.replaceWith("discovery.latest").then((e) => {
this.router.replaceWith("/latest").then((e) => {
if (params.username) {
e.send("createNewMessageViaParams", {
recipients: params.username,
@ -49,7 +50,7 @@ export default DiscourseRoute.extend({
});
} else {
cookie("destination_url", window.location.href);
this.replaceWith("login");
this.router.replaceWith("login");
}
},
});

View File

@ -2,8 +2,11 @@ import Category from "discourse/models/category";
import DiscourseRoute from "discourse/routes/discourse";
import cookie from "discourse/lib/cookie";
import { next } from "@ember/runloop";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
beforeModel(transition) {
if (this.currentUser) {
let category, categoryId;
@ -34,10 +37,9 @@ export default DiscourseRoute.extend({
}
if (category) {
let route = "discovery.category";
let params = { category, id: category.id };
this.replaceWith(route, params).then((e) => {
// Using URL-based transition to avoid bug with dynamic segments and refreshModel query params
// https://github.com/emberjs/ember.js/issues/16992
this.router.replaceWith(`/c/${category.id}`).then((e) => {
if (this.controllerFor("navigation/category").canCreateTopic) {
this._sendTransition(e, transition, categoryId);
}
@ -47,7 +49,7 @@ export default DiscourseRoute.extend({
transition.abort();
this.send("createNewTopicViaParams");
} else {
this.replaceWith("discovery.latest").then((e) => {
this.router.replaceWith("discovery.latest").then((e) => {
if (this.controllerFor("navigation/default").canCreateTopic) {
this._sendTransition(e, transition);
}
@ -57,7 +59,7 @@ export default DiscourseRoute.extend({
} else {
// User is not logged in
cookie("destination_url", window.location.href);
this.replaceWith("login");
this.router.replaceWith("login");
}
},

View File

@ -1,10 +1,13 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
// A base route that allows us to redirect when access is restricted
export default DiscourseRoute.extend({
router: service(),
afterModel() {
if (!this.modelFor("user").get("can_edit")) {
this.replaceWith("userActivity");
this.router.replaceWith("userActivity");
}
},
});

View File

@ -1,18 +1,21 @@
import DiscourseRoute from "discourse/routes/discourse";
import { next } from "@ember/runloop";
import { inject as service } from "@ember/service";
export default class SignupRoute extends DiscourseRoute {
@service router;
beforeModel() {
const { canSignUp } = this.controllerFor("application");
if (this.siteSettings.login_required) {
this.replaceWith("login").then((e) => {
this.router.replaceWith("login").then((e) => {
if (canSignUp) {
next(() => e.send("showCreateAccount"));
}
});
} else {
this.replaceWith("discovery.latest").then((e) => {
this.router.replaceWith("discovery.latest").then((e) => {
if (canSignUp) {
next(() => e.send("showCreateAccount"));
}

View File

@ -25,6 +25,7 @@ const ALL = "all";
export default DiscourseRoute.extend(FilterModeMixin, {
composer: service(),
router: service(),
navMode: "latest",
queryParams,
@ -99,7 +100,7 @@ export default DiscourseRoute.extend(FilterModeMixin, {
) {
// TODO: avoid throwing away preload data by redirecting on the server
PreloadStore.getAndRemove("topic_list");
return this.replaceWith(
return this.router.replaceWith(
"tags.showCategoryNone",
params.category_slug_path_with_id,
tagId

View File

@ -1,11 +1,14 @@
import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
model() {
let user = this.modelFor("user");
if (user.get("profile_hidden")) {
return this.replaceWith("user.profile-hidden");
return this.router.replaceWith("user.profile-hidden");
}
return user;

View File

@ -15,7 +15,7 @@ export default DiscourseRoute.extend({
// transition into a user's activity works. This makes the back button work on mobile
// where there is no user card as well as desktop where there is.
if (this.site.mobileView) {
this.replaceWith(destination);
this.router.replaceWith(destination);
} else {
this.router.transitionTo(destination);
}

View File

@ -1,7 +1,10 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
beforeModel() {
this.replaceWith("userInvited.show", "pending");
this.router.replaceWith("userInvited.show", "pending");
},
});

View File

@ -2,8 +2,11 @@ import DiscourseRoute from "discourse/routes/discourse";
import Invite from "discourse/models/invite";
import { action } from "@ember/object";
import I18n from "I18n";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
model(params) {
this.inviteFilter = params.filter;
return Invite.findInvitedBy(this.modelFor("user"), params.filter);
@ -11,7 +14,7 @@ export default DiscourseRoute.extend({
afterModel(model) {
if (!model.can_see_invite_details) {
this.replaceWith("userInvited.show", "redeemed");
this.router.replaceWith("userInvited.show", "redeemed");
}
this.controllerFor("user.invited").setProperties({
invitesCount: model.counts,

View File

@ -1,13 +1,15 @@
import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
showFooter: true,
model() {
const user = this.modelFor("user");
if (user.get("profile_hidden")) {
return this.replaceWith("user.profile-hidden");
return this.router.replaceWith("user.profile-hidden");
}
return user.summary();

View File

@ -2,11 +2,14 @@ import DiscourseRoute from "discourse/routes/discourse";
import User from "discourse/models/user";
import { action } from "@ember/object";
import { bind } from "discourse-common/utils/decorators";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
beforeModel() {
if (this.siteSettings.hide_user_profiles_from_public && !this.currentUser) {
this.replaceWith("discovery");
this.router.replaceWith("discovery");
}
},
@ -31,7 +34,7 @@ export default DiscourseRoute.extend({
.findDetails()
.then(() => user.findStaffInfo())
.then(() => user.trackStatus())
.catch(() => this.replaceWith("/404"));
.catch(() => this.router.replaceWith("/404"));
},
serialize(model) {

View File

@ -4,8 +4,10 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { Promise } from "rsvp";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
queryParams: {
period: { refreshModel: true },
order: { refreshModel: true },
@ -35,7 +37,7 @@ export default DiscourseRoute.extend({
beforeModel() {
if (this.siteSettings.hide_user_profiles_from_public && !this.currentUser) {
this.replaceWith("discovery");
this.router.replaceWith("discovery");
}
},

View File

@ -1,8 +1,11 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default DiscourseRoute.extend({
router: service(),
beforeModel() {
const appModel = this.modelFor("wizard");
this.replaceWith("wizard.step", appModel.start);
this.router.replaceWith("wizard.step", appModel.start);
},
});

View File

@ -1,9 +1,13 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class ChatBrowseIndexRoute extends DiscourseRoute {
@service router;
@service siteSettings;
afterModel() {
if (!this.siteSettings.chat_allow_archiving_channels) {
this.replaceWith("chat.browse");
this.router.replaceWith("chat.browse");
}
}
}

View File

@ -18,6 +18,6 @@ export default class ChatBrowseIndexRoute extends DiscourseRoute {
}
afterModel() {
this.replaceWith("chat.browse.open");
this.router.replaceWith("chat.browse.open");
}
}

View File

@ -1,9 +1,12 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class ChatChannelInfoAboutRoute extends DiscourseRoute {
@service router;
afterModel(model) {
if (model.isDirectMessageChannel) {
this.replaceWith("chat.channel.info.index");
this.router.replaceWith("chat.channel.info.index");
}
}
}

View File

@ -1,15 +1,18 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class ChatChannelInfoIndexRoute extends DiscourseRoute {
@service router;
afterModel(model) {
if (model.isDirectMessageChannel) {
if (model.isOpen && model.membershipsCount >= 1) {
this.replaceWith("chat.channel.info.members");
this.router.replaceWith("chat.channel.info.members");
} else {
this.replaceWith("chat.channel.info.settings");
this.router.replaceWith("chat.channel.info.settings");
}
} else {
this.replaceWith("chat.channel.info.about");
this.router.replaceWith("chat.channel.info.about");
}
}
}

View File

@ -1,13 +1,16 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class ChatChannelInfoMembersRoute extends DiscourseRoute {
@service router;
afterModel(model) {
if (!model.isOpen) {
return this.replaceWith("chat.channel.info.settings");
return this.router.replaceWith("chat.channel.info.settings");
}
if (model.membershipsCount < 1) {
return this.replaceWith("chat.channel.info");
return this.router.replaceWith("chat.channel.info");
}
}
}

View File

@ -1,9 +1,13 @@
import DiscourseRoute from "discourse/routes/discourse";
import { inject as service } from "@ember/service";
export default class ChatChannelInfoSettingsRoute extends DiscourseRoute {
@service router;
@service currentUser;
afterModel(model) {
if (!this.currentUser?.staff && !model.currentUserMembership?.following) {
this.replaceWith("chat.channel.info");
this.router.replaceWith("chat.channel.info");
}
}
}

View File

@ -17,7 +17,7 @@ export default class ChatMessageRoute extends DiscourseRoute {
params.messageId
);
})
.catch(() => this.replaceWith("/404"));
.catch(() => this.router.replaceWith("/404"));
}
beforeModel() {