Flex is built using React, so it's a single page application by default. While you can add, replace or remove components to the Flex UI, you may find that workers need an entirely separate page to effectively get their work done in the Flex UI. Flex allows you to create these views and route workers to them.
The Flex UI uses the react-router
, react-router-redux
and history
libraries for client-side routing.
Flex organizes views within the ViewCollection
dynamic component. You can manipulate Flex views in the same manner as a dynamic component.
1Flex.ViewCollection.Content.add(2<Flex.View name="my-custom-page" key="my-custom-page-key">3<div>My custom page</div>4</Flex.View>5);
This will add a new view that's available under a path that corresponds to the name
prop (i.e., https://localhost:3000/my-custom-page
in this example.)
If you require more control over mounting your view, refer to the Mounting a View to Multiple Routes section.
You can also create a link to your view in the side navigation bar:
1Flex.SideNav.Content.add(2<Flex.SideLink3showLabel={ true }4icon="DefaultAvatar"5isActive={true}6onClick={() => { Flex.Actions.invokeAction("HistoryPush", `/my-custom-page/`); } }7key="MyCustomPageSideLink"8>9My custom page10</Flex.SideLink>11);
The configuration interface for routing-related functionality in Flex has the following shape:
1router?: {2type: "browser" | "memory"; // either a web-browser address or a reference implementation3history?: HistoryBuildOptions; // build options for a `history` NPM package4isolate?: boolean; // isolate Flex routing from other routing, forces Flex to use memory router.5};
Support for browser and memory history that is configurable through the configuration object.
You may want your page's path to be separate from the name
, or to expose multiple paths that lead to the same View. The View component has a route
property that you can use to add an alternative route. The route
property has a shape similar to props of Route
component of react-router
package.
1Flex.ViewCollection.Content.add(2<Flex.View name="some-name" key="my-custom-page-key" route={{path: "/custom-route"}}>3<div>My custom page</div>4</Flex.View>5);
The custom page will be visible at the/custom-route
and /some-name
paths.
You can also use an array to add even more paths:
<View key="teams" name="teams" route={{ path: ["/supervisor", "/something"] }}>
The view will be visible at the /teams
/supervisor
and /something
paths.
If you are using routing libraries like react-router-redux
or connected-react-router
, you can sync history between your application and Flex by providing the history object that you are using for your Router as a parameter to the Flex store enhancer:
The router
application configuration options will have no effect when a custom history
object is provided.
1import { createStore, combineReducers, applyMiddleware, compose } from "redux";2import { createBrowserHistory } from "history";3import { Provider } from "react-redux";4import { ConnectedRouter } from "react-router-redux";5import { Switch, Route } from "react-router";6import { configuration } from "/appConfig";7import { myReducer } from "/myReducer";89const reducers = combineReducers({10flex: Flex.FlexReducer,11app: myReducer12});1314const history = createHistory();1516const middleware = applyMiddleware();1718const store = createStore(19reducers,20compose(21middleware,22Flex.applyFlexMiddleware(history)23)24);2526Flex27.Manager.create(configuration, store)28.then(manager => {29ReactDOM.render(30<Provider store={store}>31<ConnectedRouter history={history}>32<Switch>33<Route path="/hi" component={() => {34setTimeout(() => { history.push("/"); }, 5000);35return (36<div>Hi! I will redirect to Flex in 5 seconds flat!</div>37);38}}></Route>39<Route component={() => {40return (<Flex.ContextProvider manager={manager}>41<Flex.RootContainer />42</Flex.ContextProvider>);43}}></Route>44</Switch>45</ConnectedRouter>46</Provider>,47container48);49})
If you are using Flex alongside other applications, you may wish to prepend the Flex URL path with a prefix so that Flex routing won't interfere with other registered endpoints. For example, let's make our Flex path relative to /foobar
. To do so, apply the following configuration settings:
1window.appConfig = {2<...>3router: {4type: "browser",5history: {6basename: "/foobar"7}8}9<...>10};
The window.appConfig.router.history.basename
field tells Flex to make the Agent Desktop available under http://mysite.com/foobar/agent-desktop/
.
Implementing route prefixing creates several important side effects for other systems that interact with Flex.
Your server must take the prefix into account in its own routing. The server needs a catch-all fallback route matching the prefix (/foobar
in the above example). Otherwise, visiting http://mysite.com/foobar/agent-desktop/
will return a 404
. An application's webpack-dev-server
config that handles the route might look like the following:
1devServer: {2historyApiFallback: {3rewrites: [{ from: /^\/foobar/, to: '/foobar/index.html' }]4}5}
Additionally, the prefix is only for browser
routing and is not applicable to memory
. Finally, make sure to adjust paths to your assets files, like configuration or CSS!
Learn more in the history node package documentation.