initializeNewUserTutorials.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2017 - 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 axios from 'axios'
  21. import NewUserTutorialToggleButton from './NewUserTutorialToggleButton'
  22. import TutorialTray from './trays/TutorialTray'
  23. import getProperTray from './utils/getProperTray'
  24. import createTutorialStore from './utils/createTutorialStore'
  25. import splitAssetString from 'compiled/str/splitAssetString'
  26. const initializeNewUserTutorials = () => {
  27. if (window.ENV.NEW_USER_TUTORIALS &&
  28. window.ENV.NEW_USER_TUTORIALS.is_enabled &&
  29. (window.ENV.context_asset_string && (splitAssetString(window.ENV.context_asset_string)[0] === 'courses'))) {
  30. const API_URL = '/api/v1/users/self/new_user_tutorial_statuses';
  31. axios.get(API_URL)
  32. .then((response) => {
  33. let onPageToggleButton;
  34. const trayObj = getProperTray();
  35. const collapsedStatus = response.data.new_user_tutorial_statuses.collapsed[trayObj.pageName];
  36. const store = createTutorialStore({
  37. isCollapsed: collapsedStatus
  38. });
  39. store.addChangeListener(() => {
  40. axios.put(`${API_URL}/${trayObj.pageName}`, {
  41. collapsed: store.getState().isCollapsed
  42. });
  43. });
  44. const getReturnFocus = () => onPageToggleButton;
  45. const renderTray = () => {
  46. const Tray = trayObj.component;
  47. ReactDOM.render(
  48. <TutorialTray
  49. store={store}
  50. returnFocusToFunc={getReturnFocus}
  51. label={trayObj.label}
  52. >
  53. <Tray />
  54. </TutorialTray>
  55. , document.querySelector('.NewUserTutorialTray__Container')
  56. );
  57. }
  58. ReactDOM.render(
  59. <NewUserTutorialToggleButton
  60. ref={(c) => { onPageToggleButton = c; }}
  61. store={store}
  62. />
  63. , document.querySelector('.TutorialToggleHolder'),
  64. () => renderTray()
  65. );
  66. });
  67. }
  68. };
  69. export default initializeNewUserTutorials