This repository was archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow-router-react-helpers.js
76 lines (64 loc) · 1.75 KB
/
flow-router-react-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import PropTypes from 'prop-types';
import { mount } from 'react-mounter';
import { Meteor } from 'meteor/meteor';
import { FlowRouter } from 'meteor/kadira:flow-router';
export const name = 'flow-router-react-helpers';
export const render = (app) => {
React.Children.map(app.props.children, (layout) => {
React.Children.map(layout.props.children, (element) => {
const {
path,
name,
title,
authenticated,
onEnter,
onExit,
} = element.props;
FlowRouter.route(path, {
name,
triggersEnter: [() => {
if (title) {
document.title = title;
}
if (authenticated) {
if (!Meteor.loggingIn() && !Meteor.user()) {
FlowRouter.go('/login');
}
}
if (onEnter) {
onEnter();
}
}],
triggersExit: [() => {
if (onExit) {
onExit();
}
}],
action: (params, queryParams) => {
mount(layout.props.component, {
children: <element.props.component params={params} queryParams={queryParams} />,
});
},
});
});
});
};
export const Route = ({ children }) => (
{ children }
);
Route.defaultProps = {
children: null,
};
Route.propTypes = {
children: PropTypes.node,
};
export const pathFor = (path, params) => {
const query = params && params.query ? FlowRouter._qs.parse(params.query) : {};
return FlowRouter.path(path, params, query);
};
export const urlFor = (path, params) => Meteor.absoluteUrl(pathFor(path, params));
export const currentRoute = (routeName) => {
FlowRouter.watchPathChange();
return FlowRouter.getRouteName() === routeName;
};