AdminAppContainer.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. secretAccessKey: '',
  32. isEnabledPlugins: true,
  33. };
  34. this.changeTitle = this.changeTitle.bind(this);
  35. this.changeConfidential = this.changeConfidential.bind(this);
  36. this.changeGlobalLang = this.changeGlobalLang.bind(this);
  37. this.changeFileUpload = this.changeFileUpload.bind(this);
  38. this.changeSiteUrl = this.changeSiteUrl.bind(this);
  39. this.changeFromAddress = this.changeFromAddress.bind(this);
  40. this.changeSmtpHost = this.changeSmtpHost.bind(this);
  41. this.changeSmtpPort = this.changeSmtpPort.bind(this);
  42. this.changeSmtpUser = this.changeSmtpUser.bind(this);
  43. this.changeSmtpPassword = this.changeSmtpPassword.bind(this);
  44. this.changeRegion = this.changeRegion.bind(this);
  45. this.changeCustomEndpoint = this.changeCustomEndpoint.bind(this);
  46. this.changeBucket = this.changeBucket.bind(this);
  47. this.changeAccessKeyId = this.changeAccessKeyId.bind(this);
  48. this.changeSecretAccessKey = this.changeSecretAccessKey.bind(this);
  49. this.changeIsEnabledPlugins = this.changeIsEnabledPlugins.bind(this);
  50. this.updateAppSettingHandler = this.updateAppSettingHandler.bind(this);
  51. this.updateSiteUrlSettingHandler = this.updateSiteUrlSettingHandler.bind(this);
  52. this.updateMailSettingHandler = this.updateMailSettingHandler.bind(this);
  53. this.updateAwsSettingHandler = this.updateAwsSettingHandler.bind(this);
  54. this.updatePluginSettingHandler = this.updatePluginSettingHandler.bind(this);
  55. }
  56. /**
  57. * Workaround for the mangling in production build to break constructor.name
  58. */
  59. static getClassName() {
  60. return 'AdminAppContainer';
  61. }
  62. /**
  63. * retrieve app sttings data
  64. */
  65. async retrieveAppSettingsData() {
  66. try {
  67. const response = await this.appContainer.apiv3.get('/app-settings/');
  68. const { appSettingsParams } = response.data;
  69. this.setState({
  70. title: appSettingsParams.title,
  71. confidential: appSettingsParams.confidential,
  72. globalLang: appSettingsParams.globalLang,
  73. fileUpload: appSettingsParams.fileUpload,
  74. siteUrl: appSettingsParams.siteUrl,
  75. envSiteUrl: appSettingsParams.envSiteUrl,
  76. isSetSiteUrl: !!appSettingsParams.siteUrl,
  77. fromAddress: appSettingsParams.fromAddress,
  78. smtpHost: appSettingsParams.smtpHost,
  79. smtpPort: appSettingsParams.smtpPort,
  80. smtpUser: appSettingsParams.smtpUser,
  81. smtpPassword: appSettingsParams.smtpPassword,
  82. region: appSettingsParams.region,
  83. customEndpoint: appSettingsParams.customEndpoint,
  84. bucket: appSettingsParams.bucket,
  85. accessKeyId: appSettingsParams.accessKeyId,
  86. secretAccessKey: appSettingsParams.secretAccessKey,
  87. isEnabledPlugins: appSettingsParams.isEnabledPlugins,
  88. });
  89. }
  90. catch (err) {
  91. logger.error(err);
  92. toastError(new Error('Failed to fetch data'));
  93. }
  94. }
  95. /**
  96. * Change title
  97. */
  98. changeTitle(title) {
  99. this.setState({ title });
  100. }
  101. /**
  102. * Change confidential
  103. */
  104. changeConfidential(confidential) {
  105. this.setState({ confidential });
  106. }
  107. /**
  108. * Change globalLang
  109. */
  110. changeGlobalLang(globalLang) {
  111. this.setState({ globalLang });
  112. }
  113. /**
  114. * Change fileUpload
  115. */
  116. changeFileUpload(fileUpload) {
  117. this.setState({ fileUpload });
  118. }
  119. /**
  120. * Change site url
  121. */
  122. changeSiteUrl(siteUrl) {
  123. this.setState({ siteUrl });
  124. }
  125. /**
  126. * Change from address
  127. */
  128. changeFromAddress(fromAddress) {
  129. this.setState({ fromAddress });
  130. }
  131. /**
  132. * Change smtp host
  133. */
  134. changeSmtpHost(smtpHost) {
  135. this.setState({ smtpHost });
  136. }
  137. /**
  138. * Change smtp port
  139. */
  140. changeSmtpPort(smtpPort) {
  141. this.setState({ smtpPort });
  142. }
  143. /**
  144. * Change smtp user
  145. */
  146. changeSmtpUser(smtpUser) {
  147. this.setState({ smtpUser });
  148. }
  149. /**
  150. * Change smtp password
  151. */
  152. changeSmtpPassword(smtpPassword) {
  153. this.setState({ smtpPassword });
  154. }
  155. /**
  156. * Change region
  157. */
  158. changeRegion(region) {
  159. this.setState({ region });
  160. }
  161. /**
  162. * Change custom endpoint
  163. */
  164. changeCustomEndpoint(customEndpoint) {
  165. this.setState({ customEndpoint });
  166. }
  167. /**
  168. * Change bucket name
  169. */
  170. changeBucket(bucket) {
  171. this.setState({ bucket });
  172. }
  173. /**
  174. * Change access key id
  175. */
  176. changeAccessKeyId(accessKeyId) {
  177. this.setState({ accessKeyId });
  178. }
  179. /**
  180. * Change secret access key
  181. */
  182. changeSecretAccessKey(secretAccessKey) {
  183. this.setState({ secretAccessKey });
  184. }
  185. /**
  186. * Change secret key
  187. */
  188. changeIsEnabledPlugins(isEnabledPlugins) {
  189. this.setState({ isEnabledPlugins });
  190. }
  191. /**
  192. * Update app setting
  193. * @memberOf AdminAppContainer
  194. * @return {Array} Appearance
  195. */
  196. async updateAppSettingHandler() {
  197. const response = await this.appContainer.apiv3.put('/app-settings/app-setting', {
  198. title: this.state.title,
  199. confidential: this.state.confidential,
  200. globalLang: this.state.globalLang,
  201. fileUpload: this.state.fileUpload,
  202. });
  203. const { appSettingParams } = response.data;
  204. return appSettingParams;
  205. }
  206. /**
  207. * Update site url setting
  208. * @memberOf AdminAppContainer
  209. * @return {Array} Appearance
  210. */
  211. async updateSiteUrlSettingHandler() {
  212. const response = await this.appContainer.apiv3.put('/app-settings/site-url-setting', {
  213. siteUrl: this.state.siteUrl,
  214. });
  215. const { siteUrlSettingParams } = response.data;
  216. return siteUrlSettingParams;
  217. }
  218. /**
  219. * Update mail setting
  220. * @memberOf AdminAppContainer
  221. * @return {Array} Appearance
  222. */
  223. async updateMailSettingHandler() {
  224. const response = await this.appContainer.apiv3.put('/app-settings/mail-setting', {
  225. fromAddress: this.state.fromAddress,
  226. smtpHost: this.state.smtpHost,
  227. smtpPort: this.state.smtpPort,
  228. smtpUser: this.state.smtpUser,
  229. smtpPassword: this.state.smtpPassword,
  230. });
  231. const { mailSettingParams } = response.data;
  232. return mailSettingParams;
  233. }
  234. /**
  235. * Update AWS setting
  236. * @memberOf AdminAppContainer
  237. * @return {Array} Appearance
  238. */
  239. async updateAwsSettingHandler() {
  240. const response = await this.appContainer.apiv3.put('/app-settings/aws-setting', {
  241. region: this.state.region,
  242. customEndpoint: this.state.customEndpoint,
  243. bucket: this.state.bucket,
  244. accessKeyId: this.state.accessKeyId,
  245. secretAccessKey: this.state.secretAccessKey,
  246. });
  247. const { awsSettingParams } = response.data;
  248. return awsSettingParams;
  249. }
  250. /**
  251. * Update plugin setting
  252. * @memberOf AdminAppContainer
  253. * @return {Array} Appearance
  254. */
  255. async updatePluginSettingHandler() {
  256. const response = await this.appContainer.apiv3.put('/app-settings/plugin-setting', {
  257. isEnabledPlugins: this.state.isEnabledPlugins,
  258. });
  259. const { pluginSettingParams } = response.data;
  260. return pluginSettingParams;
  261. }
  262. }