orc-shared 1.6.0-dev.1 → 1.6.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ var _withErrorBoundary = _interopRequireDefault(require("../../hocs/withErrorBou
8
8
  var _FullPage = _interopRequireDefault(require("./FullPage"));
9
9
  var _SubPage = _interopRequireDefault(require("./SubPage"));
10
10
  var _withWaypointing = _interopRequireDefault(require("./withWaypointing"));
11
+ var _urlPattern = _interopRequireDefault(require("url-pattern"));
11
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
14
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -45,6 +46,7 @@ var Page = function Page(_ref) {
45
46
  var WrappedView = (0, _react.useMemo)(function () {
46
47
  return (0, _withErrorBoundary.default)(path)((0, _withWaypointing.default)(View, isVisible));
47
48
  }, [path, View, isVisible]);
49
+ var parentUrlPattern = new _urlPattern.default(path);
48
50
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactRouter.Switch, null, Object.entries(pages).map(function (_ref2) {
49
51
  var _ref3 = _slicedToArray(_ref2, 2),
50
52
  subpath = _ref3[0],
@@ -83,7 +85,8 @@ var Page = function Page(_ref) {
83
85
  render: function render(route) {
84
86
  return /*#__PURE__*/_react.default.createElement(_SubPage.default, _extends({
85
87
  root: path,
86
- config: config
88
+ config: config,
89
+ parentUrlPattern: parentUrlPattern
87
90
  }, route, {
88
91
  modulePrependPath: modulePrependPath
89
92
  }));
@@ -230,6 +230,8 @@ var SegmentPage = function SegmentPage(_ref4) {
230
230
  }));
231
231
  }
232
232
  if (config.subpages) {
233
+ var parentUrl = path + segpath;
234
+ var parentUrlPattern = new _urlPattern.default(parentUrl);
233
235
  subpages.push.apply(subpages, Object.entries(config.subpages).map(function (_ref9) {
234
236
  var _ref10 = _slicedToArray(_ref9, 2),
235
237
  subpath = _ref10[0],
@@ -241,7 +243,8 @@ var SegmentPage = function SegmentPage(_ref4) {
241
243
  render: function render(route) {
242
244
  return /*#__PURE__*/_react.default.createElement(_SubPage.default, _extends({
243
245
  root: path,
244
- config: config
246
+ config: config,
247
+ parentUrlPattern: parentUrlPattern
245
248
  }, route, {
246
249
  modulePrependPath: modulePrependPath
247
250
  }));
@@ -52,7 +52,8 @@ var SubPage = function SubPage(_ref) {
52
52
  location = _ref.location,
53
53
  history = _ref.history,
54
54
  root = _ref.root,
55
- modulePrependPath = _ref.modulePrependPath;
55
+ modulePrependPath = _ref.modulePrependPath,
56
+ parentUrlPattern = _ref.parentUrlPattern;
56
57
  var classes = useStyles();
57
58
  var dispatch = (0, _reactRedux.useDispatch)();
58
59
  var View = config.component,
@@ -60,15 +61,13 @@ var SubPage = function SubPage(_ref) {
60
61
  var pattern = new _urlPattern.default(root);
61
62
  var baseHref = pattern.stringify(match.params);
62
63
  var path = location.pathname;
63
- var basePathArr = path.split("/");
64
- basePathArr.pop();
65
- var basePath = basePathArr.join("/");
66
64
  var WrappedView = (0, _react.useMemo)(function () {
67
65
  return (0, _withErrorBoundary.default)(path)((0, _withWaypointing.default)(View));
68
66
  }, [path, View]);
69
67
  var closeSubPage = function closeSubPage() {
70
- history.push(basePath);
71
- dispatch((0, _navigation.mapHref)(basePath, basePath));
68
+ var parentHref = parentUrlPattern.stringify(match.params);
69
+ history.push(parentHref);
70
+ dispatch((0, _navigation.mapHref)(parentHref, parentHref));
72
71
  };
73
72
  var message = /*#__PURE__*/_react.default.createElement(WrappedView, _extends({
74
73
  match: match,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orc-shared",
3
- "version": "1.6.0-dev.1",
3
+ "version": "1.6.0-dev.2",
4
4
  "description": "Shared code for Orckestra applications",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
@@ -4,9 +4,12 @@ import withErrorBoundary from "../../hocs/withErrorBoundary";
4
4
  import FullPage from "./FullPage";
5
5
  import SubPage from "./SubPage";
6
6
  import withWaypointing from "./withWaypointing";
7
+ import UrlPattern from "url-pattern";
7
8
 
8
9
  const Page = ({ component: View, path, pages = {}, subpages = {}, modulePrependPath, isVisible = true }) => {
9
10
  const WrappedView = useMemo(() => withErrorBoundary(path)(withWaypointing(View, isVisible)), [path, View, isVisible]);
11
+ const parentUrlPattern = new UrlPattern(path);
12
+
10
13
  return (
11
14
  <React.Fragment>
12
15
  <Switch>
@@ -38,7 +41,15 @@ const Page = ({ component: View, path, pages = {}, subpages = {}, modulePrependP
38
41
  <Route
39
42
  key={subpath}
40
43
  path={path + subpath}
41
- render={route => <SubPage root={path} config={config} {...route} modulePrependPath={modulePrependPath} />}
44
+ render={route => (
45
+ <SubPage
46
+ root={path}
47
+ config={config}
48
+ parentUrlPattern={parentUrlPattern}
49
+ {...route}
50
+ modulePrependPath={modulePrependPath}
51
+ />
52
+ )}
42
53
  />
43
54
  ))}
44
55
  </Switch>
@@ -197,6 +197,9 @@ const SegmentPage = ({
197
197
  );
198
198
  }
199
199
  if (config.subpages) {
200
+ const parentUrl = path + segpath;
201
+ const parentUrlPattern = new UrlPattern(parentUrl);
202
+
200
203
  subpages.push(
201
204
  ...Object.entries(config.subpages).map(([subpath, config]) => {
202
205
  const pagePath = segpath + subpath;
@@ -204,7 +207,15 @@ const SegmentPage = ({
204
207
  <Route
205
208
  key={pagePath}
206
209
  path={path + pagePath}
207
- render={route => <SubPage root={path} config={config} {...route} modulePrependPath={modulePrependPath} />}
210
+ render={route => (
211
+ <SubPage
212
+ root={path}
213
+ config={config}
214
+ parentUrlPattern={parentUrlPattern}
215
+ {...route}
216
+ modulePrependPath={modulePrependPath}
217
+ />
218
+ )}
208
219
  />
209
220
  );
210
221
  }),
@@ -20,22 +20,19 @@ const useStyles = makeStyles(theme => ({
20
20
  },
21
21
  }));
22
22
 
23
- export const SubPage = ({ config, match, location, history, root, modulePrependPath }) => {
23
+ export const SubPage = ({ config, match, location, history, root, modulePrependPath, parentUrlPattern }) => {
24
24
  const classes = useStyles();
25
25
  const dispatch = useDispatch();
26
26
  let { component: View, ...props } = config;
27
27
  const pattern = new UrlPattern(root);
28
28
  const baseHref = pattern.stringify(match.params);
29
-
30
29
  const path = location.pathname;
31
30
 
32
- const basePathArr = path.split("/");
33
- basePathArr.pop();
34
- const basePath = basePathArr.join("/");
35
31
  const WrappedView = useMemo(() => withErrorBoundary(path)(withWaypointing(View)), [path, View]);
36
32
  const closeSubPage = () => {
37
- history.push(basePath);
38
- dispatch(mapHref(basePath, basePath));
33
+ const parentHref = parentUrlPattern.stringify(match.params);
34
+ history.push(parentHref);
35
+ dispatch(mapHref(parentHref, parentHref));
39
36
  };
40
37
 
41
38
  const message = (
@@ -13,6 +13,7 @@ import Button from "@material-ui/core/Button";
13
13
  import translations from "~/translations/en-US.json";
14
14
  import { TestWrapper, createMuiTheme } from "../../utils/testUtils";
15
15
  import sharedMessages from "../../sharedMessages";
16
+ import UrlPattern from "url-pattern";
16
17
 
17
18
  const InnerView = ({ theme, pathname, search, mapFrom, match, location, routeIsAligned, set }) => (
18
19
  <PropStruct
@@ -92,6 +93,7 @@ describe("SubPage", () => {
92
93
  config={{ component: InnerView, set: true, title: "Item Details" }}
93
94
  root="/foo"
94
95
  path="/foo/bar"
96
+ parentUrlPattern={new UrlPattern("/foo")}
95
97
  {...route}
96
98
  />
97
99
  )}
@@ -140,6 +142,7 @@ describe("SubPage", () => {
140
142
  config={{ component: InnerView, set: true, title: sharedMessages.confirmation }}
141
143
  root="/foo"
142
144
  path="/foo/bar"
145
+ parentUrlPattern={new UrlPattern("/foo")}
143
146
  {...route}
144
147
  />
145
148
  )}
@@ -164,6 +167,7 @@ describe("SubPage", () => {
164
167
  config={{ component: InnerView, set: true, title: "Item Details" }}
165
168
  root="/foo"
166
169
  path="/foo/bar"
170
+ parentUrlPattern={new UrlPattern("/foo")}
167
171
  {...route}
168
172
  />
169
173
  )}
@@ -180,6 +184,42 @@ describe("SubPage", () => {
180
184
  expect(history.push, "to have calls satisfying", [{ args: ["/foo"] }]);
181
185
  expect(dispatch, "to have calls satisfying", [{ args: [mapHref("/foo", "/foo")] }]);
182
186
  });
187
+ it("closing the dialog navigate to the parentUrlPattern with parameters", () => {
188
+ history = createMemoryHistory({ initialEntries: ["/foo/bar/123/456"] });
189
+ sinon.spy(history, "push");
190
+ history.push.named("history.push");
191
+ dispatch.resetHistory();
192
+
193
+ const component = (
194
+ <TestWrapper provider={{ store }} intlProvider={intlProvider} stylesProvider muiThemeProvider={{ theme }}>
195
+ <div>
196
+ <div id="outer" />
197
+ <Router history={history}>
198
+ <Route
199
+ path="/foo/bar/:parentId/:id"
200
+ render={route => (
201
+ <SubPage
202
+ config={{ component: InnerView, set: true, title: "Item Details" }}
203
+ root="/foo/bar/:parentId/:id"
204
+ path="/foo/bar/123/456"
205
+ parentUrlPattern={new UrlPattern("/foo/bar/:parentId")}
206
+ {...route}
207
+ />
208
+ )}
209
+ />
210
+ </Router>
211
+ </div>
212
+ </TestWrapper>
213
+ );
214
+ const mountedComponent = mount(component);
215
+
216
+ const closeButton = mountedComponent.find("button").at(0);
217
+
218
+ closeButton.invoke("onClick")();
219
+ expect(history.push, "to have calls satisfying", [{ args: ["/foo/bar/123"] }]);
220
+ expect(dispatch, "to have a call satisfying", { args: [mapHref("/foo/bar/123", "/foo/bar/123")] });
221
+ });
222
+
183
223
  it("renders action panel passed from props", () => {
184
224
  const actions = () => [{ label: sharedMessages.cancel }, { label: sharedMessages.applyChanges }];
185
225
 
@@ -200,6 +240,7 @@ describe("SubPage", () => {
200
240
  }}
201
241
  root="/foo"
202
242
  path="/foo/bar"
243
+ parentUrlPattern={new UrlPattern("/foo")}
203
244
  {...route}
204
245
  />
205
246
  )}
@@ -242,6 +283,7 @@ describe("SubPage", () => {
242
283
  }}
243
284
  root="/foo"
244
285
  path="/foo/bar"
286
+ parentUrlPattern={new UrlPattern("/foo")}
245
287
  {...route}
246
288
  />
247
289
  )}
@@ -286,6 +328,7 @@ describe("SubPage", () => {
286
328
  }}
287
329
  root="/foo"
288
330
  path="/foo/bar"
331
+ parentUrlPattern={new UrlPattern("/foo")}
289
332
  {...route}
290
333
  />
291
334
  )}
@@ -327,6 +370,7 @@ describe("SubPage", () => {
327
370
  }}
328
371
  root="/foo"
329
372
  path="/foo/bar"
373
+ parentUrlPattern={new UrlPattern("/foo")}
330
374
  {...route}
331
375
  />
332
376
  )}
@@ -368,6 +412,7 @@ describe("SubPage", () => {
368
412
  }}
369
413
  root="/foo"
370
414
  path="/foo/bar"
415
+ parentUrlPattern={new UrlPattern("/foo")}
371
416
  {...route}
372
417
  />
373
418
  )}
@@ -411,6 +456,7 @@ describe("SubPage", () => {
411
456
  }}
412
457
  root="/foo"
413
458
  path="/foo/bar"
459
+ parentUrlPattern={new UrlPattern("/foo")}
414
460
  {...route}
415
461
  />
416
462
  )}