AdminAppContainer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 sesAccessKeyId
  174. */
  175. changeSesAccessKeyId(sesAccessKeyId) {
  176. this.setState({ sesAccessKeyId });
  177. }
  178. /**
  179. * Change sesSecretAccessKey
  180. */
  181. changeSesSecretAccessKey(sesSecretAccessKey) {
  182. this.setState({ sesSecretAccessKey });
  183. }
  184. /**
  185. * Change s3Region
  186. */
  187. changeS3Region(s3Region) {
  188. this.setState({ s3Region });
  189. }
  190. /**
  191. * Change s3CustomEndpoint
  192. */
  193. changeS3CustomEndpoint(s3CustomEndpoint) {
  194. this.setState({ s3CustomEndpoint });
  195. }
  196. /**
  197. * Change fileUploadType
  198. */
  199. changeFileUploadType(fileUploadType) {
  200. this.setState({ fileUploadType });
  201. }
  202. /**
  203. * Change region
  204. */
  205. changeS3Bucket(s3Bucket) {
  206. this.setState({ s3Bucket });
  207. }
  208. /**
  209. * Change access key id
  210. */
  211. changeS3AccessKeyId(s3AccessKeyId) {
  212. this.setState({ s3AccessKeyId });
  213. }
  214. /**
  215. * Change secret access key
  216. */
  217. changeS3SecretAccessKey(s3SecretAccessKey) {
  218. this.setState({ s3SecretAccessKey });
  219. }
  220. /**
  221. * Change s3ReferenceFileWithRelayMode
  222. */
  223. changeS3ReferenceFileWithRelayMode(s3ReferenceFileWithRelayMode) {
  224. this.setState({ s3ReferenceFileWithRelayMode });
  225. }
  226. /**
  227. * Change gcsApiKeyJsonPath
  228. */
  229. changeGcsApiKeyJsonPath(gcsApiKeyJsonPath) {
  230. this.setState({ gcsApiKeyJsonPath });
  231. }
  232. /**
  233. * Change gcsBucket
  234. */
  235. changeGcsBucket(gcsBucket) {
  236. this.setState({ gcsBucket });
  237. }
  238. /**
  239. * Change gcsUploadNamespace
  240. */
  241. changeGcsUploadNamespace(gcsUploadNamespace) {
  242. this.setState({ gcsUploadNamespace });
  243. }
  244. /**
  245. * Change gcsReferenceFileWithRelayMode
  246. */
  247. changeGcsReferenceFileWithRelayMode(gcsReferenceFileWithRelayMode) {
  248. this.setState({ gcsReferenceFileWithRelayMode });
  249. }
  250. /**
  251. * Change secret key
  252. */
  253. changeIsEnabledPlugins(isEnabledPlugins) {
  254. this.setState({ isEnabledPlugins });
  255. }
  256. /**
  257. * Update app setting
  258. * @memberOf AdminAppContainer
  259. * @return {Array} Appearance
  260. */
  261. async updateAppSettingHandler() {
  262. const response = await this.appContainer.apiv3.put('/app-settings/app-setting', {
  263. title: this.state.title,
  264. confidential: this.state.confidential,
  265. globalLang: this.state.globalLang,
  266. fileUpload: this.state.fileUpload,
  267. });
  268. const { appSettingParams } = response.data;
  269. return appSettingParams;
  270. }
  271. /**
  272. * Update site url setting
  273. * @memberOf AdminAppContainer
  274. * @return {Array} Appearance
  275. */
  276. async updateSiteUrlSettingHandler() {
  277. const response = await this.appContainer.apiv3.put('/app-settings/site-url-setting', {
  278. siteUrl: this.state.siteUrl,
  279. });
  280. const { siteUrlSettingParams } = response.data;
  281. return siteUrlSettingParams;
  282. }
  283. /**
  284. * Update mail setting
  285. * @memberOf AdminAppContainer
  286. * @return {Array} Appearance
  287. */
  288. updateMailSettingHandler() {
  289. if (this.state.transmissionMethod === 'smtp') {
  290. return this.updateSmtpSetting();
  291. }
  292. return this.updateSesSetting();
  293. }
  294. /**
  295. * Update smtp setting
  296. * @memberOf AdminAppContainer
  297. * @return {Array} Appearance
  298. */
  299. async updateSmtpSetting() {
  300. const response = await this.appContainer.apiv3.put('/app-settings/smtp-setting', {
  301. fromAddress: this.state.fromAddress,
  302. transmissionMethod: this.state.transmissionMethod,
  303. smtpHost: this.state.smtpHost,
  304. smtpPort: this.state.smtpPort,
  305. smtpUser: this.state.smtpUser,
  306. smtpPassword: this.state.smtpPassword,
  307. });
  308. const { mailSettingParams } = response.data;
  309. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  310. return mailSettingParams;
  311. }
  312. /**
  313. * Update ses setting
  314. * @memberOf AdminAppContainer
  315. * @return {Array} Appearance
  316. */
  317. async updateSesSetting() {
  318. const response = await this.appContainer.apiv3.put('/app-settings/ses-setting', {
  319. fromAddress: this.state.fromAddress,
  320. transmissionMethod: this.state.transmissionMethod,
  321. sesAccessKeyId: this.state.sesAccessKeyId,
  322. sesSecretAccessKey: this.state.sesSecretAccessKey,
  323. });
  324. const { mailSettingParams } = response.data;
  325. this.setState({ isMailerSetup: mailSettingParams.isMailerSetup });
  326. return mailSettingParams;
  327. }
  328. /**
  329. * send test e-mail
  330. * @memberOf AdminAppContainer
  331. */
  332. async sendTestEmail() {
  333. return this.appContainer.apiv3.post('/app-settings/smtp-test');
  334. }
  335. /**
  336. * Update updateFileUploadSettingHandler
  337. * @memberOf AdminAppContainer
  338. */
  339. async updateFileUploadSettingHandler() {
  340. const { fileUploadType } = this.state;
  341. const requestParams = {
  342. fileUploadType,
  343. };
  344. if (fileUploadType === 'gcs') {
  345. requestParams.gcsApiKeyJsonPath = this.state.gcsApiKeyJsonPath;
  346. requestParams.gcsBucket = this.state.gcsBucket;
  347. requestParams.gcsUploadNamespace = this.state.gcsUploadNamespace;
  348. requestParams.gcsReferenceFileWithRelayMode = this.state.gcsReferenceFileWithRelayMode;
  349. }
  350. if (fileUploadType === 'aws') {
  351. requestParams.s3Region = this.state.s3Region;
  352. requestParams.s3CustomEndpoint = this.state.s3CustomEndpoint;
  353. requestParams.s3Bucket = this.state.s3Bucket;
  354. requestParams.s3AccessKeyId = this.state.s3AccessKeyId;
  355. requestParams.s3SecretAccessKey = this.state.s3SecretAccessKey;
  356. requestParams.s3ReferenceFileWithRelayMode = this.state.s3ReferenceFileWithRelayMode;
  357. }
  358. const response = await this.appContainer.apiv3.put('/app-settings/file-upload-setting', requestParams);
  359. const { responseParams } = response.data;
  360. return this.setState(responseParams);
  361. }
  362. /**
  363. * Update plugin setting
  364. * @memberOf AdminAppContainer
  365. * @return {Array} Appearance
  366. */
  367. async updatePluginSettingHandler() {
  368. const response = await this.appContainer.apiv3.put('/app-settings/plugin-setting', {
  369. isEnabledPlugins: this.state.isEnabledPlugins,
  370. });
  371. const { pluginSettingParams } = response.data;
  372. return pluginSettingParams;
  373. }
  374. }