Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Create Custom Views and Routes


Flex is built using React, so it's a single page application by default. While you can easily 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.


Add, Replace, or Remove a View

add-replace-or-remove-a-view page anchor

Flex organizes views within the ViewCollection dynamic component. You can manipulate Flex views in the same manner as a dynamic component.


_10
Flex.ViewCollection.Content.add(
_10
<Flex.View name="my-custom-page" key="my-custom-page-key">
_10
<div>My custom page</div>
_10
</Flex.View>
_10
);

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:


_11
Flex.SideNav.Content.add(
_11
<Flex.SideLink
_11
showLabel={ true }
_11
icon="DefaultAvatar"
_11
isActive={true}
_11
onClick={() => { Flex.Actions.invokeAction("HistoryPush", `/my-custom-page/`); } }
_11
key="MyCustomPageSideLink"
_11
>
_11
My custom page
_11
</Flex.SideLink>
_11
);


The configuration interface for routing-related functionality in Flex has the following shape:


_10
router?: {
_10
type: "browser" | "memory"; // either a web-browser address or a reference implementation
_10
history?: HistoryBuildOptions; // build options for a `history` NPM package
_10
isolate?: boolean; // isolate Flex routing from other routing, forces Flex to use memory router.
_10
};


Actions for Navigation and Routing

actions-for-navigation-and-routing page anchor

You can use actions to navigate to different "locations" in the Flex UI. Actions work similarly to the HTML5 History API(link takes you to an external page). Flex actions include:

ActionPayloadDescription
NavigateToView{ viewName: string }Navigates the user to a view defined by the provided view name
HistoryPushURL path as a String or an Object matching the shape of the history location ({ pathname: string, search: string, hash: string, state: { [key: string]: any } })Adds a history entry, leading to view change or redirect
HistoryReplaceURL path as a String or an Object matching the shape of the history location ({ pathname: string, search: string, hash: string, state: { [key: string]: any } })Replaces current history entry, leading to view change or redirect
HistoryGoIntegerGoes to a specific history entry, identified by its relative position to the current page. The current page is index 0.
HistoryGoBackNoneGoes back to the previous history entry
HistoryGoForwardNoneGoes forward to the next history entry

Browser and In-Memory History

browser-and-in-memory-history page anchor

Support for browser and memory history that is configurable through the configuration object.


Mounting a view to multiple routes

mounting-a-view-to-multiple-routes page anchor

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(link takes you to an external page).


_10
Flex.ViewCollection.Content.add(
_10
<Flex.View name="some-name" key="my-custom-page-key" route={{path: "/custom-route"}}>
_10
<div>My custom page</div>
_10
</Flex.View>
_10
);

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:


_10
<View key="teams" name="teams" route={{ path: ["/supervisor", "/something"] }}>

The view will be visible at the /teams /supervisor and /something paths.


Coordinating Flex UI with your application's routing

coordinating-flex-ui-with-your-applications-routing page anchor

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:

(information)

Info

The router application configuration options will have no effect when a custom history object is provided.


_49
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
_49
import { createBrowserHistory } from "history";
_49
import { Provider } from "react-redux";
_49
import { ConnectedRouter } from "react-router-redux";
_49
import { Switch, Route } from "react-router";
_49
import { configuration } from "/appConfig";
_49
import { myReducer } from "/myReducer";
_49
_49
const reducers = combineReducers({
_49
flex: Flex.FlexReducer,
_49
app: myReducer
_49
});
_49
_49
const history = createHistory();
_49
_49
const middleware = applyMiddleware();
_49
_49
const store = createStore(
_49
reducers,
_49
compose(
_49
middleware,
_49
Flex.applyFlexMiddleware(history)
_49
)
_49
);
_49
_49
Flex
_49
.Manager.create(configuration, store)
_49
.then(manager => {
_49
ReactDOM.render(
_49
<Provider store={store}>
_49
<ConnectedRouter history={history}>
_49
<Switch>
_49
<Route path="/hi" component={() => {
_49
setTimeout(() => { history.push("/"); }, 5000);
_49
return (
_49
<div>Hi! I will redirect to Flex in 5 seconds flat!</div>
_49
);
_49
}}></Route>
_49
<Route component={() => {
_49
return (<Flex.ContextProvider manager={manager}>
_49
<Flex.RootContainer />
_49
</Flex.ContextProvider>);
_49
}}></Route>
_49
</Switch>
_49
</ConnectedRouter>
_49
</Provider>,
_49
container
_49
);
_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:


_10
window.appConfig = {
_10
<...>
_10
router: {
_10
type: "browser",
_10
history: {
_10
basename: "/foobar"
_10
}
_10
}
_10
<...>
_10
};

The window.appConfig.router.history.basename field tells Flex to make the Agent Desktop available under http://mysite.com/foobar/agent-desktop/.

(warning)

Warning

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:


_10
devServer: {
_10
historyApiFallback: {
_10
rewrites: [{ from: /^\/foobar/, to: '/foobar/index.html' }]
_10
}
_10
}

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(link takes you to an external page).


Rate this page: