AdminAppContainer.js 7.8 KB

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