AdminAppContainer.js 7.1 KB

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