router.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import ReactDOM from 'react-dom'
  20. import page from 'page'
  21. import qs from 'qs'
  22. import redux from 'redux'
  23. import CollaborationsApp from 'jsx/collaborations/CollaborationsApp'
  24. import CollaborationsToolLaunch from 'jsx/collaborations/CollaborationsToolLaunch'
  25. import actions from 'jsx/collaborations/actions/collaborationsActions'
  26. import store from 'jsx/collaborations/store/store'
  27. import splitAssetString from 'compiled/str/splitAssetString'
  28. $(window).on('externalContentReady', (e, data) => store.dispatch(actions.externalContentReady(e, data)));
  29. let unsubscribe
  30. /**
  31. * Route Handlers
  32. */
  33. function renderShowCollaborations (ctx) {
  34. store.dispatch(actions.getLTICollaborators(ctx.params.context, ctx.params.contextId));
  35. store.dispatch(actions.getCollaborations(`/api/v1/${ctx.params.context}/${ctx.params.contextId}/collaborations`, true));
  36. let view = () => {
  37. let state = store.getState();
  38. ReactDOM.render(<CollaborationsApp applicationState={state} actions={actions} />, document.getElementById('content'));
  39. };
  40. unsubscribe = store.subscribe(view);
  41. view();
  42. }
  43. function renderLaunchTool (ctx) {
  44. let view = () => {
  45. ReactDOM.render(<CollaborationsToolLaunch launchUrl={ctx.path.replace('/lti_collaborations', '')} />, document.getElementById('content'))
  46. }
  47. view()
  48. }
  49. /**
  50. * Middlewares
  51. */
  52. function parseQueryString (ctx, next) {
  53. ctx.query = qs.parse(ctx.querystring);
  54. next();
  55. }
  56. /**
  57. * Route Configuration
  58. */
  59. page('*', parseQueryString); // Middleware to parse querystring to object
  60. page('/:context(courses|groups)/:contextId/lti_collaborations', renderShowCollaborations);
  61. page.exit('/:context(courses|groups)/:contextId/lti_collaborations', (ctx, next) => {
  62. unsubscribe()
  63. next()
  64. })
  65. page('/:context(courses|groups)/:contextId/lti_collaborations/external_tools*', renderLaunchTool);
  66. export default {
  67. start () {
  68. page.start();
  69. }
  70. };