AdminAppContainer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. fileUploadType: '',
  32. envFileUploadType: '',
  33. isFixedFileUploadByEnvVar: false,
  34. gcsUseOnlyEnvVars: false,
  35. gcsApiKeyJsonPath: '',
  36. envGcsApiKeyJsonPath: '',
  37. gcsBucket: '',
  38. envGcsBucket: '',
  39. gcsUploadNamespace: '',
  40. envGcsUploadNamespace: '',
  41. gcsReferenceFileWithRelayMode: false,
  42. s3Region: '',
  43. s3CustomEndpoint: '',
  44. s3Bucket: '',
  45. s3AccessKeyId: '',
  46. s3SecretAccessKey: '',
  47. s3ReferenceFileWithRelayMode: false,
  48. isEnabledPlugins: true,
  49. };
  50. }
  51. /**
  52. * Workaround for the mangling in production build to break constructor.name
  53. */
  54. static getClassName() {
  55. return 'AdminAppContainer';
  56. }
  57. /**
  58. * retrieve app sttings data
  59. */
  60. async retrieveAppSettingsData() {
  61. const response = await this.appContainer.apiv3.get('/app-settings/');
  62. const { appSettingsParams } = response.data;
  63. this.setState({
  64. title: appSettingsParams.title,
  65. confidential: appSettingsParams.confidential,
  66. globalLang: appSettingsParams.globalLang,
  67. fileUpload: appSettingsParams.fileUpload,
  68. siteUrl: appSettingsParams.siteUrl,
  69. envSiteUrl: appSettingsParams.envSiteUrl,
  70. isSetSiteUrl: !!appSettingsParams.siteUrl,
  71. isMailerSetup: appSettingsParams.isMailerSetup,
  72. fromAddress: appSettingsParams.fromAddress,
  73. transmissionMethod: appSettingsParams.transmissionMethod,
  74. smtpHost: appSettingsParams.smtpHost,
  75. smtpPort: appSettingsParams.smtpPort,
  76. smtpUser: appSettingsParams.smtpUser,
  77. smtpPassword: appSettingsParams.smtpPassword,
  78. sesAccessKeyId: appSettingsParams.sesAccessKeyId,
  79. sesSecretAccessKey: appSettingsParams.sesSecretAccessKey,
  80. fileUploadType: appSettingsParams.fileUploadType,
  81. envFileUploadType: appSettingsParams.envFileUploadType,
  82. useOnlyEnvVarForFileUploadType: appSettingsParams.useOnlyEnvVarForFileUploadType,
  83. s3Region: appSettingsParams.s3Region,
  84. s3CustomEndpoint: appSettingsParams.s3CustomEndpoint,
  85. s3Bucket: appSettingsParams.s3Bucket,
  86. s3AccessKeyId: appSettingsParams.s3AccessKeyId,
  87. s3SecretAccessKey: appSettingsParams.s3SecretAccessKey,
  88. s3ReferenceFileWithRelayMode: appSettingsParams.s3ReferenceFileWithRelayMode,
  89. gcsUseOnlyEnvVars: appSettingsParams.gcsUseOnlyEnvVars,
  90. gcsApiKeyJsonPath: appSettingsParams.gcsApiKeyJsonPath,
  91. gcsBucket: appSettingsParams.gcsBucket,
  92. gcsUploadNamespace: appSettingsParams.gcsUploadNamespace,
  93. gcsReferenceFileWithRelayMode: appSettingsParams.gcsReferenceFileWithRelayMode,
  94. envGcsApiKeyJsonPath: appSettingsParams.envGcsApiKeyJsonPath,
  95. envGcsBucket: appSettingsParams.envGcsBucket,
  96. envGcsUploadNamespace: appSettingsParams.envGcsUploadNamespace,
  97. isEnabledPlugins: appSettingsParams.isEnabledPlugins,
  98. });
  99. // if useOnlyEnvVarForFileUploadType is true, get fileUploadType from only env var and make the forms fixed.
  100. // and if env var 'FILE_UPLOAD' is null, envFileUploadType is 'aws' that is default value of 'FILE_UPLOAD'.
  101. if (appSettingsParams.useOnlyEnvVarForFileUploadType) {
  102. this.setState({ fileUploadType: appSettingsParams.envFileUploadType });
  103. this.setState({ isFixedFileUploadByEnvVar: true });
  104. }
  105. }
  106. /**
  107. * Change title
  108. */
  109. changeTitle(title) {
  110. this.setState({ title });
  111. }
  112. /**
  113. * Change confidential
  114. */
  115. changeConfidential(confidential) {
  116. this.setState({ confidential });
  117. }
  118. /**
  119. * Change globalLang
  120. */
  121. changeGlobalLang(globalLang) {
  122. this.setState({ globalLang });
  123. }
  124. /**
  125. * Change fileUpload
  126. */
  127. changeFileUpload(fileUpload) {
  128. this.setState({ fileUpload });
  129. }
  130. /**
  131. * Change site url
  132. */
  133. changeSiteUrl(siteUrl) {
  134. this.setState({ siteUrl });
  135. }
  136. /**
  137. * Change from address
  138. */
  139. changeFromAddress(fromAddress) {
  140. this.setState({ fromAddress });
  141. }
  142. /**
  143. * Change from transmission method
  144. */
  145. changeTransmissionMethod(transmissionMethod) {
  146. this.setState({ transmissionMethod });
  147. }
  148. /**
  149. * Change smtp host
  150. */
  151. changeSmtpHost(smtpHost) {
  152. this.setState({ smtpHost });
  153. }
  154. /**
  155. * Change smtp port
  156. */
  157. changeSmtpPort(smtpPort) {
  158. this.setState({ smtpPort });
  159. }
  160. /**
  161. * Change smtp user
  162. */
  163. changeSmtpUser(smtpUser) {
  164. this.setState({ smtpUser });
  165. }
  166. /**
  167. * Change smtp password
  168. */
  169. changeSmtpPassword(smtpPassword) {
  170. this.setState({ smtpPassword });
  171. }
  172. /**
  173. * Change s3Region
  174. */
  175. changeS3Region(s3Region) {
  176. this.setState({ s3Region });
  177. }
  178. /**
  179. * Change s3CustomEndpoint
  180. */
  181. changeS3CustomEndpoint(s3CustomEndpoint) {
  182. this.setState({ s3CustomEndpoint });
  183. }
  184. /**
  185. * Change fileUploadType
  186. */
  187. changeFileUploadType(fileUploadType) {
  188. this.setState({ fileUploadType });
  189. }
  190. /**
  191. * Change region
  192. */
  193. changeS3Bucket(s3Bucket) {
  194. this.setState({ s3Bucket });
  195. }
  196. /**
  197. * Change access key id
  198. */
  199. changeS3AccessKeyId(s3AccessKeyId) {
  200. this.setState({ s3AccessKeyId });
  201. }
  202. /**
  203. * Change secret access key
  204. */
  205. changeS3SecretAccessKey(s3SecretAccessKey) {
  206. this.setState({ s3SecretAccessKey });
  207. }
  208. /**
  209. * Change s3ReferenceFileWithRelayMode
  210. */
  211. changeS3ReferenceFileWithRelayMode(s3ReferenceFileWithRelayMode) {
  212. this.setState({ s3ReferenceFileWithRelayMode });
  213. }
  214. /**
  215. * Change gcsApiKeyJsonPath
  216. */
  217. changeGcsApiKeyJsonPath(gcsApiKeyJsonPath) {
  218. this.setState({ gcsApiKeyJsonPath });
  219. }
  220. /**
  221. * Change gcsBucket
  222. */
  223. changeGcsBucket(gcsBucket) {
  224. this.setState({ gcsBucket });
  225. }
  226. /**
  227. * Change gcsUploadNamespace
  228. */
  229. changeGcsUploadNamespace(gcsUploadNamespace) {
  230. this.setState({ gcsUploadNamespace });
  231. }
  232. /**
  233. * Change gcsReferenceFileWithRelayMode
  234. */
  235. changeGcsReferenceFileWithRelayMode(gcsReferenceFileWithRelayMode) {
  236. this.setState({ gcsReferenceFileWithRelayMode });
  237. }
  238. /**
  239. * Change secret key
  240. */
  241. changeIsEnabledPlugins(isEnabledPlugins) {
  242. this.setState({ isEnabledPlugins });
  243. }
  244. /**
  245. * Update app setting
  246. * @memberOf AdminAppContainer
  247. * @return {Array} Appearance
  248. */
  249. async updateAppSettingHandler() {
  250. const response = await this.appContainer.apiv3.put('/app-settings/app-setting', {
  251. title: this.state.title,
  252. confidential: this.state.confidential,
  253. globalLang: this.state.globalLang,
  254. fileUpload: this.state.fileUpload,
  255. });
  256. const { appSettingParams } = response.data;
  257. return appSettingParams;
  258. }
  259. /**
  260. * Update site url setting
  261. * @memberOf AdminAppContainer
  262. * @return {Array} Appearance
  263. */
  264. async updateSiteUrlSettingHandler() {
  265. const response = await this.appContainer.apiv3.put('/app-settings/site-url-setting', {
  266. siteUrl: this.state.siteUrl,
  267. });
  268. const { siteUrlSettingParams } = response.data;
  269. return siteUrlSettingParams;
  270. }
  271. /**
  272. * Update mail setting
  273. * @memberOf AdminAppContainer
  274. * @return {Array} Appearance
  275. */
  276. updateMailSettingHandler() {
  277. if (this.state.transmissionMethod === 'smtp') {
  278. return this.updateSmtpSetting();
  279. }
  280. return this.updateSesSetting();
  281. }
  282. /**
  283. * Update smtp setting
  284. * @memberOf AdminAppContainer
  285. * @return {Array} Appearance
  286. */
  287. async updateSmtpSetting() {
  288. const response = await this.appContainer.apiv3.put('/app-settings/smtp-setting', {
  289. fromAddress: this.state.fromAddress,
  290. transmissionMethod: this.state.transmissionMethod,
  291. smtpHost: this.state.smtpHost,
  292. smtpPort: this.state.smtpPort,
  293. smtpUser: this.state.smtpUser,
  294. smtpPassword: this.state.smtpPassword,
  295. });
  296. const { mailSettingParams } = response.data;
  297. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  298. return mailSettingParams;
  299. }
  300. /**
  301. * Update ses setting
  302. * @memberOf AdminAppContainer
  303. * @return {Array} Appearance
  304. */
  305. async updateSesSetting() {
  306. const response = await this.appContainer.apiv3.put('/app-settings/ses-setting', {
  307. fromAddress: this.state.fromAddress,
  308. transmissionMethod: this.state.transmissionMethod,
  309. sesAccessKeyId: this.state.sesAccessKeyId,
  310. sesSecretAccessKey: this.state.sesSecretAccessKey,
  311. });
  312. const { mailSettingParams } = response.data;
  313. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  314. return mailSettingParams;
  315. }
  316. /**
  317. * send test e-mail
  318. * @memberOf AdminAppContainer
  319. */
  320. async sendTestEmail() {
  321. return this.appContainer.apiv3.post('/app-settings/smtp-test');
  322. }
  323. /**
  324. * Update updateFileUploadSettingHandler
  325. * @memberOf AdminAppContainer
  326. */
  327. async updateFileUploadSettingHandler() {
  328. const { fileUploadType } = this.state;
  329. const requestParams = {
  330. fileUploadType,
  331. };
  332. if (fileUploadType === 'gcs') {
  333. requestParams.gcsApiKeyJsonPath = this.state.gcsApiKeyJsonPath;
  334. requestParams.gcsBucket = this.state.gcsBucket;
  335. requestParams.gcsUploadNamespace = this.state.gcsUploadNamespace;
  336. requestParams.gcsReferenceFileWithRelayMode = this.state.gcsReferenceFileWithRelayMode;
  337. }
  338. if (fileUploadType === 'aws') {
  339. requestParams.s3Region = this.state.s3Region;
  340. requestParams.s3CustomEndpoint = this.state.s3CustomEndpoint;
  341. requestParams.s3Bucket = this.state.s3Bucket;
  342. requestParams.s3AccessKeyId = this.state.s3AccessKeyId;
  343. requestParams.s3SecretAccessKey = this.state.s3SecretAccessKey;
  344. requestParams.s3ReferenceFileWithRelayMode = this.state.s3ReferenceFileWithRelayMode;
  345. }
  346. const response = await this.appContainer.apiv3.put('/app-settings/file-upload-setting', requestParams);
  347. const { responseParams } = response.data;
  348. return this.setState(responseParams);
  349. }
  350. /**
  351. * Update plugin setting
  352. * @memberOf AdminAppContainer
  353. * @return {Array} Appearance
  354. */
  355. async updatePluginSettingHandler() {
  356. const response = await this.appContainer.apiv3.put('/app-settings/plugin-setting', {
  357. isEnabledPlugins: this.state.isEnabledPlugins,
  358. });
  359. const { pluginSettingParams } = response.data;
  360. return pluginSettingParams;
  361. }
  362. }