AdminAppContainer.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { Container } from 'unstated';
  2. /**
  3. * Service container for admin app setting page (AppSettings.jsx)
  4. * @extends {Container} unstated Container
  5. */
  6. export default class AdminAppContainer extends Container {
  7. constructor(appContainer) {
  8. super();
  9. this.appContainer = appContainer;
  10. this.dummyTitle = 0;
  11. this.dummyTitleForError = 1;
  12. this.state = {
  13. retrieveError: null,
  14. // set dummy value tile for using suspense
  15. title: this.dummyTitle,
  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. const response = await this.appContainer.apiv3.get('/app-settings/');
  67. const { appSettingsParams } = response.data;
  68. this.setState({
  69. title: appSettingsParams.title,
  70. confidential: appSettingsParams.confidential,
  71. globalLang: appSettingsParams.globalLang,
  72. fileUpload: appSettingsParams.fileUpload,
  73. siteUrl: appSettingsParams.siteUrl,
  74. envSiteUrl: appSettingsParams.envSiteUrl,
  75. isSetSiteUrl: !!appSettingsParams.siteUrl,
  76. fromAddress: appSettingsParams.fromAddress,
  77. smtpHost: appSettingsParams.smtpHost,
  78. smtpPort: appSettingsParams.smtpPort,
  79. smtpUser: appSettingsParams.smtpUser,
  80. smtpPassword: appSettingsParams.smtpPassword,
  81. region: appSettingsParams.region,
  82. customEndpoint: appSettingsParams.customEndpoint,
  83. bucket: appSettingsParams.bucket,
  84. accessKeyId: appSettingsParams.accessKeyId,
  85. secretAccessKey: appSettingsParams.secretAccessKey,
  86. isEnabledPlugins: appSettingsParams.isEnabledPlugins,
  87. });
  88. }
  89. /**
  90. * Change title
  91. */
  92. changeTitle(title) {
  93. this.setState({ title });
  94. }
  95. /**
  96. * Change confidential
  97. */
  98. changeConfidential(confidential) {
  99. this.setState({ confidential });
  100. }
  101. /**
  102. * Change globalLang
  103. */
  104. changeGlobalLang(globalLang) {
  105. this.setState({ globalLang });
  106. }
  107. /**
  108. * Change fileUpload
  109. */
  110. changeFileUpload(fileUpload) {
  111. this.setState({ fileUpload });
  112. }
  113. /**
  114. * Change site url
  115. */
  116. changeSiteUrl(siteUrl) {
  117. this.setState({ siteUrl });
  118. }
  119. /**
  120. * Change from address
  121. */
  122. changeFromAddress(fromAddress) {
  123. this.setState({ fromAddress });
  124. }
  125. /**
  126. * Change smtp host
  127. */
  128. changeSmtpHost(smtpHost) {
  129. this.setState({ smtpHost });
  130. }
  131. /**
  132. * Change smtp port
  133. */
  134. changeSmtpPort(smtpPort) {
  135. this.setState({ smtpPort });
  136. }
  137. /**
  138. * Change smtp user
  139. */
  140. changeSmtpUser(smtpUser) {
  141. this.setState({ smtpUser });
  142. }
  143. /**
  144. * Change smtp password
  145. */
  146. changeSmtpPassword(smtpPassword) {
  147. this.setState({ smtpPassword });
  148. }
  149. /**
  150. * Change region
  151. */
  152. changeRegion(region) {
  153. this.setState({ region });
  154. }
  155. /**
  156. * Change custom endpoint
  157. */
  158. changeCustomEndpoint(customEndpoint) {
  159. this.setState({ customEndpoint });
  160. }
  161. /**
  162. * Change bucket name
  163. */
  164. changeBucket(bucket) {
  165. this.setState({ bucket });
  166. }
  167. /**
  168. * Change access key id
  169. */
  170. changeAccessKeyId(accessKeyId) {
  171. this.setState({ accessKeyId });
  172. }
  173. /**
  174. * Change secret access key
  175. */
  176. changeSecretAccessKey(secretAccessKey) {
  177. this.setState({ secretAccessKey });
  178. }
  179. /**
  180. * Change secret key
  181. */
  182. changeIsEnabledPlugins(isEnabledPlugins) {
  183. this.setState({ isEnabledPlugins });
  184. }
  185. /**
  186. * Update app setting
  187. * @memberOf AdminAppContainer
  188. * @return {Array} Appearance
  189. */
  190. async updateAppSettingHandler() {
  191. const response = await this.appContainer.apiv3.put('/app-settings/app-setting', {
  192. title: this.state.title,
  193. confidential: this.state.confidential,
  194. globalLang: this.state.globalLang,
  195. fileUpload: this.state.fileUpload,
  196. });
  197. const { appSettingParams } = response.data;
  198. return appSettingParams;
  199. }
  200. /**
  201. * Update site url setting
  202. * @memberOf AdminAppContainer
  203. * @return {Array} Appearance
  204. */
  205. async updateSiteUrlSettingHandler() {
  206. const response = await this.appContainer.apiv3.put('/app-settings/site-url-setting', {
  207. siteUrl: this.state.siteUrl,
  208. });
  209. const { siteUrlSettingParams } = response.data;
  210. return siteUrlSettingParams;
  211. }
  212. /**
  213. * Update mail setting
  214. * @memberOf AdminAppContainer
  215. * @return {Array} Appearance
  216. */
  217. async updateMailSettingHandler() {
  218. const response = await this.appContainer.apiv3.put('/app-settings/mail-setting', {
  219. fromAddress: this.state.fromAddress,
  220. smtpHost: this.state.smtpHost,
  221. smtpPort: this.state.smtpPort,
  222. smtpUser: this.state.smtpUser,
  223. smtpPassword: this.state.smtpPassword,
  224. });
  225. const { mailSettingParams } = response.data;
  226. return mailSettingParams;
  227. }
  228. /**
  229. * Initialize mail setting
  230. * @memberOf AdminAppContainer
  231. * @return {Array} Appearance
  232. */
  233. async initializeMailSettingHandler() {
  234. const response = await this.appContainer.apiv3.delete('/app-settings/mail-setting', {});
  235. const {
  236. mailSettingParams,
  237. } = response.data;
  238. this.setState(mailSettingParams);
  239. return mailSettingParams;
  240. }
  241. /**
  242. * Update AWS setting
  243. * @memberOf AdminAppContainer
  244. * @return {Array} Appearance
  245. */
  246. async updateAwsSettingHandler() {
  247. const response = await this.appContainer.apiv3.put('/app-settings/aws-setting', {
  248. region: this.state.region,
  249. customEndpoint: this.state.customEndpoint,
  250. bucket: this.state.bucket,
  251. accessKeyId: this.state.accessKeyId,
  252. secretAccessKey: this.state.secretAccessKey,
  253. });
  254. const { awsSettingParams } = response.data;
  255. return awsSettingParams;
  256. }
  257. /**
  258. * Update plugin setting
  259. * @memberOf AdminAppContainer
  260. * @return {Array} Appearance
  261. */
  262. async updatePluginSettingHandler() {
  263. const response = await this.appContainer.apiv3.put('/app-settings/plugin-setting', {
  264. isEnabledPlugins: this.state.isEnabledPlugins,
  265. });
  266. const { pluginSettingParams } = response.data;
  267. return pluginSettingParams;
  268. }
  269. }