SaveThemeButton.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 I18n from 'i18n!theme_editor'
  19. import React from 'react'
  20. import PropTypes from 'prop-types'
  21. import $ from 'jquery'
  22. import customTypes from './PropTypes'
  23. import Modal from 'jsx/shared/modal'
  24. import ModalContent from 'jsx/shared/modal-content'
  25. import ModalButtons from 'jsx/shared/modal-buttons'
  26. export default React.createClass({
  27. displayName: 'SaveThemeButton',
  28. propTypes: {
  29. accountID: PropTypes.string.isRequired,
  30. brandConfigMd5: customTypes.md5,
  31. sharedBrandConfigBeingEdited: customTypes.sharedBrandConfig.isRequired,
  32. onSave: PropTypes.func.isRequired
  33. },
  34. getInitialState() {
  35. return {
  36. newThemeName: '',
  37. modalIsOpen: false
  38. }
  39. },
  40. save () {
  41. const shouldUpdate = !!(this.props.sharedBrandConfigBeingEdited &&
  42. this.props.sharedBrandConfigBeingEdited.id)
  43. const params = {brand_config_md5: this.props.brandConfigMd5}
  44. let url, method
  45. if (shouldUpdate) {
  46. url = `/api/v1/accounts/${this.props.accountID}/shared_brand_configs/${this.props.sharedBrandConfigBeingEdited.id}`
  47. method = 'PUT'
  48. } else {
  49. if (!this.state.newThemeName) {
  50. this.setState({modalIsOpen: true})
  51. return
  52. }
  53. params.name = this.state.newThemeName
  54. url = `/api/v1/accounts/${this.props.accountID}/shared_brand_configs`
  55. method = 'POST'
  56. }
  57. return $.ajaxJSON(url, method, {shared_brand_config: params}, (updatedSharedConfig) => {
  58. this.setState({modalIsOpen: false})
  59. this.props.onSave(updatedSharedConfig)
  60. })
  61. },
  62. render() {
  63. let disable = false
  64. let disableMessage
  65. if (this.props.userNeedsToPreviewFirst) {
  66. disable = true
  67. disableMessage = I18n.t('You need to "Preview Changes" before saving')
  68. } else if (this.props.sharedBrandConfigBeingEdited &&
  69. this.props.sharedBrandConfigBeingEdited.brand_config_md5 === this.props.brandConfigMd5) {
  70. disable = true
  71. disableMessage = I18n.t('There are no unsaved changes')
  72. } else if (!this.props.brandConfigMd5) {
  73. disable = true
  74. }
  75. return (
  76. <div
  77. className="pull-left"
  78. data-tooltip="left"
  79. title={disableMessage}
  80. >
  81. <button
  82. type="button"
  83. className="Button Button--primary"
  84. disabled={disable}
  85. onClick={this.save}
  86. >
  87. {I18n.t('Save theme')}
  88. </button>
  89. <Modal
  90. title={I18n.t('Theme Name')}
  91. onSubmit={this.save}
  92. isOpen={this.state.modalIsOpen}
  93. >
  94. <ModalContent>
  95. <div className="ic-Form-control">
  96. <label
  97. htmlFor="new_theme_theme_name"
  98. className="ic-Label"
  99. >
  100. {I18n.t('Theme Name')}
  101. </label>
  102. <input
  103. type="text"
  104. id="new_theme_theme_name"
  105. className="ic-Input"
  106. placeholder={I18n.t('Pick a name to save this theme as')}
  107. onChange={(e) => this.setState({newThemeName: e.target.value})}
  108. />
  109. </div>
  110. </ModalContent>
  111. <ModalButtons>
  112. <button
  113. type='button'
  114. className='Button'
  115. onClick={() => this.setState({modalIsOpen:false})}
  116. >
  117. {I18n.t('Cancel')}
  118. </button>
  119. <button
  120. type='submit'
  121. disabled={!this.state.newThemeName}
  122. className="Button Button--primary"
  123. >
  124. {I18n.t('Save theme')}
  125. </button>
  126. </ModalButtons>
  127. </Modal>
  128. </div>
  129. );
  130. }
  131. })