AdminAppContainer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { toastError } from '../util/apiNotification';
  4. const logger = loggerFactory('growi:appSettings');
  5. /**
  6. * Service container for admin app setting page (AppSettings.jsx)
  7. * @extends {Container} unstated Container
  8. */
  9. export default class AdminAppContainer extends Container {
  10. constructor(appContainer) {
  11. super();
  12. this.appContainer = appContainer;
  13. this.state = {
  14. retrieveError: null,
  15. title: '',
  16. confidential: '',
  17. globalLang: '',
  18. fileUpload: '',
  19. siteUrl: '',
  20. envSiteUrl: '',
  21. isSetSiteUrl: true,
  22. fromAddress: '',
  23. smtpHost: '',
  24. smtpPort: '',
  25. smtpUser: '',
  26. smtpPassword: '',
  27. region: '',
  28. customEndpoint: '',
  29. bucket: '',
  30. accessKeyId: '',
  31. secretKey: '',
  32. };
  33. this.changeTitle = this.changeTitle.bind(this);
  34. this.changeConfidential = this.changeConfidential.bind(this);
  35. this.changeGlobalLang = this.changeGlobalLang.bind(this);
  36. this.changeFileUpload = this.changeFileUpload.bind(this);
  37. this.changeSiteUrl = this.changeSiteUrl.bind(this);
  38. this.changeFromAddress = this.changeFromAddress.bind(this);
  39. this.changeSmtpHost = this.changeSmtpHost.bind(this);
  40. this.changeSmtpPort = this.changeSmtpPort.bind(this);
  41. this.changeSmtpUser = this.changeSmtpUser.bind(this);
  42. this.changeSmtpPassword = this.changeSmtpPassword.bind(this);
  43. this.changeRegion = this.changeRegion.bind(this);
  44. this.changeCustomEndpoint = this.changeCustomEndpoint.bind(this);
  45. this.changeBucket = this.changeBucket.bind(this);
  46. this.changeAccessKeyId = this.changeAccessKeyId.bind(this);
  47. this.changeSecretKey = this.changeSecretKey.bind(this);
  48. this.updateAppSettingHandler = this.updateAppSettingHandler.bind(this);
  49. this.updateSiteUrlSettingHandler = this.updateSiteUrlSettingHandler.bind(this);
  50. this.updateMailSettingHandler = this.updateMailSettingHandler.bind(this);
  51. this.updateAwsSettingHandler = this.updateAwsSettingHandler.bind(this);
  52. }
  53. /**
  54. * Workaround for the mangling in production build to break constructor.name
  55. */
  56. static getClassName() {
  57. return 'AdminAppContainer';
  58. }
  59. /**
  60. * retrieve app sttings data
  61. */
  62. async retrieveAppSettingsData() {
  63. try {
  64. const response = await this.appContainer.apiv3.get('/app-settings/');
  65. const { appSettingsParams } = response.data;
  66. this.setState({
  67. title: appSettingsParams.title,
  68. confidential: appSettingsParams.confidential,
  69. globalLang: appSettingsParams.globalLang,
  70. fileUpload: appSettingsParams.fileUpload,
  71. siteUrl: appSettingsParams.siteUrl,
  72. envSiteUrl: appSettingsParams.envSiteUrl,
  73. isSetSiteUrl: !!appSettingsParams.siteUrl,
  74. fromAddress: appSettingsParams.fromAddress,
  75. smtpHost: appSettingsParams.smtpHost,
  76. smtpPort: appSettingsParams.smtpPort,
  77. smtpUser: appSettingsParams.smtpUser,
  78. smtpPassword: appSettingsParams.smtpPassword,
  79. region: appSettingsParams.region,
  80. customEndpoint: appSettingsParams.customEndpoint,
  81. bucket: appSettingsParams.bucket,
  82. accessKeyId: appSettingsParams.accessKeyId,
  83. secretKey: appSettingsParams.secretKey,
  84. });
  85. }
  86. catch (err) {
  87. logger.error(err);
  88. toastError(new Error('Failed to fetch data'));
  89. }
  90. }
  91. /**
  92. * Change title
  93. */
  94. changeTitle(title) {
  95. this.setState({ title });
  96. }
  97. /**
  98. * Change confidential
  99. */
  100. changeConfidential(confidential) {
  101. this.setState({ confidential });
  102. }
  103. /**
  104. * Change globalLang
  105. */
  106. changeGlobalLang(globalLang) {
  107. this.setState({ globalLang });
  108. }
  109. /**
  110. * Change fileUpload
  111. */
  112. changeFileUpload(fileUpload) {
  113. this.setState({ fileUpload });
  114. }
  115. /**
  116. * Change site url
  117. */
  118. changeSiteUrl(siteUrl) {
  119. this.setState({ siteUrl });
  120. }
  121. /**
  122. * Change from address
  123. */
  124. changeFromAddress(fromAddress) {
  125. this.setState({ fromAddress });
  126. }
  127. /**
  128. * Change smtp host
  129. */
  130. changeSmtpHost(smtpHost) {
  131. this.setState({ smtpHost });
  132. }
  133. /**
  134. * Change smtp port
  135. */
  136. changeSmtpPort(smtpPort) {
  137. this.setState({ smtpPort });
  138. }
  139. /**
  140. * Change smtp user
  141. */
  142. changeSmtpUser(smtpUser) {
  143. this.setState({ smtpUser });
  144. }
  145. /**
  146. * Change smtp password
  147. */
  148. changeSmtpPassword(smtpPassword) {
  149. this.setState({ smtpPassword });
  150. }
  151. /**
  152. * Change region
  153. */
  154. changeRegion(region) {
  155. this.setState({ region });
  156. }
  157. /**
  158. * Change custom endpoint
  159. */
  160. changeCustomEndpoint(customEndpoint) {
  161. this.setState({ customEndpoint });
  162. }
  163. /**
  164. * Change bucket name
  165. */
  166. changeBucket(bucket) {
  167. this.setState({ bucket });
  168. }
  169. /**
  170. * Change access key id
  171. */
  172. changeAccessKeyId(accessKeyId) {
  173. this.setState({ accessKeyId });
  174. }
  175. /**
  176. * Change secret key
  177. */
  178. changeSecretKey(secretKey) {
  179. this.setState({ secretKey });
  180. }
  181. /**
  182. * Update app setting
  183. * @memberOf AdminAppContainer
  184. * @return {Array} Appearance
  185. */
  186. async updateAppSettingHandler() {
  187. const response = await this.appContainer.apiv3.put('/app-settings/app-setting', {
  188. title: this.state.title,
  189. confidential: this.state.confidential,
  190. globalLang: this.state.globalLang,
  191. fileUpload: this.state.fileUpload,
  192. });
  193. const { appSettingParams } = response.data;
  194. return appSettingParams;
  195. }
  196. /**
  197. * Update site url setting
  198. * @memberOf AdminAppContainer
  199. * @return {Array} Appearance
  200. */
  201. async updateSiteUrlSettingHandler() {
  202. const response = await this.appContainer.apiv3.put('/app-settings/site-url-setting', {
  203. siteUrl: this.state.siteUrl,
  204. });
  205. const { siteUrlSettingParams } = response.data;
  206. return siteUrlSettingParams;
  207. }
  208. /**
  209. * Update mail setting
  210. * @memberOf AdminAppContainer
  211. * @return {Array} Appearance
  212. */
  213. async updateMailSettingHandler() {
  214. const response = await this.appContainer.apiv3.put('/app-settings/mail-setting', {
  215. fromAddress: this.state.fromAddress,
  216. smtpHost: this.state.smtpHost,
  217. smtpPort: this.state.smtpPort,
  218. smtpUser: this.state.smtpUser,
  219. smtpPassword: this.state.smtpPassword,
  220. });
  221. const { mailSettingParams } = response.data;
  222. return mailSettingParams;
  223. }
  224. /**
  225. * Update AWS setting
  226. * @memberOf AdminAppContainer
  227. * @return {Array} Appearance
  228. */
  229. async updateAwsSettingHandler() {
  230. const response = await this.appContainer.apiv3.put('/app-settings/aws-setting', {
  231. region: this.state.region,
  232. customEndpoint: this.state.customEndpoint,
  233. bucket: this.state.bucket,
  234. accessKeyId: this.state.accessKeyId,
  235. secretKey: this.state.secretKey,
  236. });
  237. const { awsSettingParams } = response.data;
  238. return awsSettingParams;
  239. }
  240. }