AdminAppContainer.js 8.0 KB

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