DEV: Make admin experimental sidebar config more forgiving (#24236)
Followup to b53449eac9
,
it was too easy to add broken routes which would break
configuration for the whole site, so now we validate ember
routes on save.
This commit is contained in:
parent
a86833fe91
commit
3fe8cc811c
|
@ -14,7 +14,17 @@
|
||||||
or a
|
or a
|
||||||
<code>href</code>, if you want to link to a specific page but don't know the
|
<code>href</code>, if you want to link to a specific page but don't know the
|
||||||
Ember route. You can also use the Ember Inspector extension to find route
|
Ember route. You can also use the Ember Inspector extension to find route
|
||||||
names, for example for plugin routes which are not auto-generated here.</p>
|
names, for example for plugin routes which are not auto-generated here.
|
||||||
|
<br /><br />
|
||||||
|
<code>routeModels</code>
|
||||||
|
is an array of values that correspond to parts of the route; for example the
|
||||||
|
topic route may be
|
||||||
|
<code>/t/:id</code>, so to get a link to topic with ID 123 you would do
|
||||||
|
<code>routeModels: [123]</code>.</p>
|
||||||
|
|
||||||
|
<p>All configuration options for a section and its links looks like this:</p>
|
||||||
|
|
||||||
|
<pre><code>{{this.exampleJson}}</code></pre>
|
||||||
|
|
||||||
<DButton
|
<DButton
|
||||||
@action={{this.resetToDefault}}
|
@action={{this.resetToDefault}}
|
||||||
|
|
|
@ -13,12 +13,36 @@ import { ADMIN_PANEL } from "discourse/services/sidebar-state";
|
||||||
export default class AdminConfigAreaSidebarExperiment extends Component {
|
export default class AdminConfigAreaSidebarExperiment extends Component {
|
||||||
@service adminSidebarExperimentStateManager;
|
@service adminSidebarExperimentStateManager;
|
||||||
@service toasts;
|
@service toasts;
|
||||||
|
@service router;
|
||||||
@tracked editedNavConfig;
|
@tracked editedNavConfig;
|
||||||
|
|
||||||
|
validRouteNames = new Set();
|
||||||
|
|
||||||
get defaultAdminNav() {
|
get defaultAdminNav() {
|
||||||
return JSON.stringify(ADMIN_NAV_MAP, null, 2);
|
return JSON.stringify(ADMIN_NAV_MAP, null, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get exampleJson() {
|
||||||
|
return JSON.stringify(
|
||||||
|
{
|
||||||
|
name: "section-name",
|
||||||
|
text: "Section Name",
|
||||||
|
links: [
|
||||||
|
{
|
||||||
|
name: "admin-revamp",
|
||||||
|
route: "admin-revamp",
|
||||||
|
routeModels: [123],
|
||||||
|
text: "Revamp",
|
||||||
|
href: "https://forum.site.com/t/123",
|
||||||
|
icon: "rocket",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
loadDefaultNavConfig() {
|
loadDefaultNavConfig() {
|
||||||
const savedConfig = this.adminSidebarExperimentStateManager.navConfig;
|
const savedConfig = this.adminSidebarExperimentStateManager.navConfig;
|
||||||
|
@ -48,6 +72,40 @@ export default class AdminConfigAreaSidebarExperiment extends Component {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let invalidRoutes = [];
|
||||||
|
config.forEach((section) => {
|
||||||
|
section.links.forEach((link) => {
|
||||||
|
if (!link.route) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.validRouteNames.has(link.route)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.router._router._routerMicrolib.recognizer.handlersFor(
|
||||||
|
link.route
|
||||||
|
);
|
||||||
|
this.validRouteNames.add(link.route);
|
||||||
|
} catch {
|
||||||
|
invalidRoutes.push(link.route);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (invalidRoutes.length) {
|
||||||
|
this.toasts.error({
|
||||||
|
duration: 3000,
|
||||||
|
data: {
|
||||||
|
message: `There was an error with one or more of the routes provided: ${invalidRoutes.join(
|
||||||
|
", "
|
||||||
|
)}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.#saveConfig(config);
|
this.#saveConfig(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue