Skip to contentSkip to navigationSkip to topbar
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 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.

1
Flex.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:

1
Flex.SideNav.Content.add(
2
<Flex.SideLink
3
showLabel={ true }
4
icon="DefaultAvatar"
5
isActive={true}
6
onClick={() => { Flex.Actions.invokeAction("HistoryPush", `/my-custom-page/`); } }
7
key="MyCustomPageSideLink"
8
>
9
My custom page
10
</Flex.SideLink>
11
);

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

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

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).

1
Flex.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.


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.

1
import { createStore, combineReducers, applyMiddleware, compose } from "redux";
2
import { createBrowserHistory } from "history";
3
import { Provider } from "react-redux";
4
import { ConnectedRouter } from "react-router-redux";
5
import { Switch, Route } from "react-router";
6
import { configuration } from "/appConfig";
7
import { myReducer } from "/myReducer";
8
9
const reducers = combineReducers({
10
flex: Flex.FlexReducer,
11
app: myReducer
12
});
13
14
const history = createHistory();
15
16
const middleware = applyMiddleware();
17
18
const store = createStore(
19
reducers,
20
compose(
21
middleware,
22
Flex.applyFlexMiddleware(history)
23
)
24
);
25
26
Flex
27
.Manager.create(configuration, store)
28
.then(manager => {
29
ReactDOM.render(
30
<Provider store={store}>
31
<ConnectedRouter history={history}>
32
<Switch>
33
<Route path="/hi" component={() => {
34
setTimeout(() => { history.push("/"); }, 5000);
35
return (
36
<div>Hi! I will redirect to Flex in 5 seconds flat!</div>
37
);
38
}}></Route>
39
<Route component={() => {
40
return (<Flex.ContextProvider manager={manager}>
41
<Flex.RootContainer />
42
</Flex.ContextProvider>);
43
}}></Route>
44
</Switch>
45
</ConnectedRouter>
46
</Provider>,
47
container
48
);
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:

1
window.appConfig = {
2
<...>
3
router: {
4
type: "browser",
5
history: {
6
basename: "/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/.

(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:

1
devServer: {
2
historyApiFallback: {
3
rewrites: [{ 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(link takes you to an external page).

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.