Merge pull request #698 from RickCarlino/router_bugs

Router Fixes
pull/700/head
Rick Carlino 2018-03-05 16:29:41 -06:00 committed by GitHub
commit a0fab7b45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 35 deletions

View File

@ -5,13 +5,15 @@ import {
designerRoutes,
maybeReplaceDesignerModules
} from "../route_config";
import { noop } from "lodash";
import { RouterState, RedirectFunction } from "react-router";
async function makeSureTheyAreRoutes(input: typeof topLevelRoutes.childRoutes) {
const cb = jest.fn();
await Promise.all(input.map(route => route.getComponent(undefined, cb)));
const all = (input || []);
await Promise.all(all.map(route => (route.getComponent || noop)({} as RouterState, cb)));
expect(cb).toHaveBeenCalled();
expect(cb).toHaveBeenCalledTimes(input.length);
expect(cb).toHaveBeenCalledTimes(all.length);
cb.mock.calls.map(x => expect(!!x[1]).toBeTruthy());
}

View File

@ -1,6 +1,6 @@
import { App } from "./app";
import { crashPage } from "./crash_page";
import { RouterState, RedirectFunction } from "react-router";
import { RouterState, RedirectFunction, PlainRoute } from "react-router";
/** These methods are a way to determine how to load certain modules
* based on the device (mobile or desktop) for optimization/css purposes.
@ -12,69 +12,110 @@ export function maybeReplaceDesignerModules(next: RouterState,
}
}
function page(path: string, getter: () => Promise<React.ReactType>) {
/** Create a react router config for a specific route. */
function page<T>(path: string,
getter: () => Promise<T>,
key: keyof T): PlainRoute {
return {
path,
getComponent(_: void, cb: Function) {
const ok = (component: React.ReactType) => cb(undefined, component);
getComponent(_, cb): void {
const ok = (mod: T) => cb(undefined, mod[key] as any);
const no = (e: object) => cb(undefined, crashPage(e));
return getter().then(ok, no);
/** Whatever you do, make sure this function stays void or you will get a
* bunch of silent errors. - RC*/
getter().then(ok, no);
}
};
}
const controlsRoute =
page("app/controls", async () => (await import("./controls/controls")).Controls);
export const designerRoutes = {
const controlsRoute: PlainRoute =
page("app/controls",
() => import("./controls/controls"), "Controls");
export const designerRoutes: PlainRoute = {
path: "app/designer",
onEnter: maybeReplaceDesignerModules,
getComponent(_discard: void, cb: Function) {
getComponent(_, cb) {
import("./farm_designer/index")
.then(module => cb(undefined, module.FarmDesigner))
.catch((e: object) => cb(undefined, crashPage(e)));
},
childRoutes: [
page("plants",
async () => (await import("./farm_designer/plants/plant_inventory")).Plants),
() => import("./farm_designer/plants/plant_inventory"),
"Plants"),
page("plants/crop_search",
async () => (await import("./farm_designer/plants/crop_catalog")).CropCatalog),
() => import("./farm_designer/plants/crop_catalog"),
"CropCatalog"),
page("plants/crop_search/:crop",
async () => (await import("./farm_designer/plants/crop_info")).CropInfo),
() => import("./farm_designer/plants/crop_info"),
"CropInfo"),
page("plants/crop_search/:crop/add",
async () => (await import("./farm_designer/plants/add_plant")).AddPlant),
() => import("./farm_designer/plants/add_plant"),
"AddPlant"),
page("plants/select",
async () => (await import("./farm_designer/plants/select_plants")).SelectPlants),
page("plants/move_to", async () => (await import("./farm_designer/plants/move_to")).MoveTo),
page("plants/create_point",
async () => (await import("./farm_designer/plants/create_points")).CreatePoints),
() => import("./farm_designer/plants/select_plants"),
"SelectPlants"),
page("plants/move_to",
() => import("./farm_designer/plants/move_to"),
"MoveTo"),
page("plants",
() => import("./farm_designer/plants/create_points"),
"CreatePoints"),
page("plants/:plant_id",
async () => (await import("./farm_designer/plants/plant_info")).PlantInfo),
() => import("./farm_designer/plants/plant_info"),
"PlantInfo"),
page("plants/:plant_id/edit",
async () => (await import("./farm_designer/plants/edit_plant_info")).EditPlantInfo),
() => import("./farm_designer/plants/edit_plant_info"),
"EditPlantInfo"),
page("farm_events",
async () => (await import("./farm_designer/farm_events/farm_events")).FarmEvents),
() => import("./farm_designer/farm_events/farm_events"),
"FarmEvents"),
page("farm_events/add",
async () => (await import("./farm_designer/farm_events/add_farm_event")).AddFarmEvent),
() => import("./farm_designer/farm_events/add_farm_event"),
"AddFarmEvent"),
page("farm_events/:farm_event_id",
async () => (await import("./farm_designer/farm_events/edit_farm_event")).EditFarmEvent),
() => import("./farm_designer/farm_events/edit_farm_event"),
"EditFarmEvent"),
]
};
export const topLevelRoutes = {
export const topLevelRoutes: PlainRoute = {
component: App,
indexRoute: controlsRoute,
childRoutes: [
page("app/account", async () => (await import("./account/index")).Account),
page("app/account",
() => import("./account/index"),
"Account"),
controlsRoute,
page("app/device", async () => (await import("./devices/devices")).Devices),
page("app/farmware", async () => (await import("./farmware/index")).FarmwarePage),
page("app/regimens", async () => (await import("./regimens/index")).Regimens),
page("app/regimens/:regimen", async () => (await import("./regimens/index")).Regimens),
page("app/sequences", async () => (await import("./sequences/sequences")).Sequences),
page("app/sequences/:sequence", async () => (await import("./sequences/sequences")).Sequences),
page("app/tools", async () => (await import("./tools/index")).Tools),
page("app/logs", async () => (await import("./logs/index")).Logs),
page("*", async () => (await import("./404")).FourOhFour),
page("app/device",
() => import("./devices/devices"),
"Devices"),
page("app/farmware",
() => import("./farmware/index"),
"FarmwarePage"),
designerRoutes,
page("app/regimens",
() => import("./regimens/index"),
"Regimens"),
page("app/regimens/:regimen",
() => import("./regimens/index"),
"Regimens"),
page("app/sequences",
() => import("./sequences/sequences"),
"Sequences"),
page("app/sequences/:sequence",
() => import("./sequences/sequences"),
"Sequences"),
page("app/tools",
() => import("./tools/index"),
"Tools"),
page("app/logs",
() => import("./logs/index"),
"Logs"),
page("*",
() => import("./404"),
"FourOhFour"),
]
};