AdminCustomizeContainer.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { isServer } from '@growi/core';
  2. import { Container } from 'unstated';
  3. import loggerFactory from '~/utils/logger';
  4. import { toastError } from '../util/apiNotification';
  5. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  6. // eslint-disable-next-line no-unused-vars
  7. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  8. /**
  9. * Service container for admin customize setting page (Customize.jsx)
  10. * @extends {Container} unstated Container
  11. */
  12. export default class AdminCustomizeContainer extends Container {
  13. constructor() {
  14. super();
  15. if (isServer()) {
  16. return;
  17. }
  18. this.state = {
  19. retrieveError: null,
  20. isEnabledTimeline: false,
  21. isEnabledAttachTitleHeader: false,
  22. pageLimitationS: null,
  23. pageLimitationM: null,
  24. pageLimitationL: null,
  25. pageLimitationXL: null,
  26. isEnabledStaleNotification: false,
  27. isAllReplyShown: false,
  28. isSearchScopeChildrenAsDefault: false,
  29. currentCustomizeTitle: '',
  30. currentCustomizeNoscript: '',
  31. currentCustomizeCss: '',
  32. currentCustomizeScript: '',
  33. };
  34. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  35. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  36. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  37. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  38. }
  39. /**
  40. * Workaround for the mangling in production build to break constructor.name
  41. */
  42. static getClassName() {
  43. return 'AdminCustomizeContainer';
  44. }
  45. /**
  46. * retrieve customize data
  47. */
  48. async retrieveCustomizeData() {
  49. try {
  50. const response = await apiv3Get('/customize-setting/');
  51. const { customizeParams } = response.data;
  52. this.setState({
  53. isEnabledTimeline: customizeParams.isEnabledTimeline,
  54. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  55. pageLimitationS: customizeParams.pageLimitationS,
  56. pageLimitationM: customizeParams.pageLimitationM,
  57. pageLimitationL: customizeParams.pageLimitationL,
  58. pageLimitationXL: customizeParams.pageLimitationXL,
  59. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  60. isAllReplyShown: customizeParams.isAllReplyShown,
  61. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  62. currentCustomizeTitle: customizeParams.customizeTitle,
  63. currentCustomizeNoscript: customizeParams.customizeNoscript,
  64. currentCustomizeCss: customizeParams.customizeCss,
  65. currentCustomizeScript: customizeParams.customizeScript,
  66. });
  67. }
  68. catch (err) {
  69. this.setState({ retrieveError: err });
  70. logger.error(err);
  71. throw new Error('Failed to fetch data');
  72. }
  73. }
  74. /**
  75. * Switch enabledTimeLine
  76. */
  77. switchEnableTimeline() {
  78. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  79. }
  80. /**
  81. * Switch enabledAttachTitleHeader
  82. */
  83. switchEnabledAttachTitleHeader() {
  84. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  85. }
  86. /**
  87. * S: Switch pageListLimitationS
  88. */
  89. switchPageListLimitationS(value) {
  90. this.setState({ pageLimitationS: value });
  91. }
  92. /**
  93. * M: Switch pageListLimitationM
  94. */
  95. switchPageListLimitationM(value) {
  96. this.setState({ pageLimitationM: value });
  97. }
  98. /**
  99. * L: Switch pageListLimitationL
  100. */
  101. switchPageListLimitationL(value) {
  102. this.setState({ pageLimitationL: value });
  103. }
  104. /**
  105. * XL: Switch pageListLimitationXL
  106. */
  107. switchPageListLimitationXL(value) {
  108. this.setState({ pageLimitationXL: value });
  109. }
  110. /**
  111. * Switch enabledStaleNotification
  112. */
  113. switchEnableStaleNotification() {
  114. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  115. }
  116. /**
  117. * Switch isAllReplyShown
  118. */
  119. switchIsAllReplyShown() {
  120. this.setState({ isAllReplyShown: !this.state.isAllReplyShown });
  121. }
  122. /**
  123. * Switch isSearchScopeChildrenAsDefault
  124. */
  125. switchIsSearchScopeChildrenAsDefault() {
  126. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  127. }
  128. /**
  129. * Change customize Title
  130. */
  131. changeCustomizeTitle(inputValue) {
  132. this.setState({ currentCustomizeTitle: inputValue });
  133. }
  134. /**
  135. * Change customize Html header
  136. */
  137. changeCustomizeNoscript(inputValue) {
  138. this.setState({ currentCustomizeNoscript: inputValue });
  139. }
  140. /**
  141. * Change customize css
  142. */
  143. changeCustomizeCss(inputValue) {
  144. this.setState({ currentCustomizeCss: inputValue });
  145. }
  146. /**
  147. * Change customize script
  148. */
  149. changeCustomizeScript(inpuValue) {
  150. this.setState({ currentCustomizeScript: inpuValue });
  151. }
  152. /**
  153. * Update function
  154. * @memberOf AdminCustomizeContainer
  155. */
  156. async updateCustomizeFunction() {
  157. try {
  158. const response = await apiv3Put('/customize-setting/function', {
  159. isEnabledTimeline: this.state.isEnabledTimeline,
  160. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  161. pageLimitationS: this.state.pageLimitationS,
  162. pageLimitationM: this.state.pageLimitationM,
  163. pageLimitationL: this.state.pageLimitationL,
  164. pageLimitationXL: this.state.pageLimitationXL,
  165. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  166. isAllReplyShown: this.state.isAllReplyShown,
  167. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  168. });
  169. const { customizedParams } = response.data;
  170. this.setState({
  171. isEnabledTimeline: customizedParams.isEnabledTimeline,
  172. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  173. pageLimitationS: customizedParams.pageLimitationS,
  174. pageLimitationM: customizedParams.pageLimitationM,
  175. pageLimitationL: customizedParams.pageLimitationL,
  176. pageLimitationXL: customizedParams.pageLimitationXL,
  177. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  178. isAllReplyShown: customizedParams.isAllReplyShown,
  179. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  180. });
  181. }
  182. catch (err) {
  183. logger.error(err);
  184. throw new Error('Failed to update data');
  185. }
  186. }
  187. /**
  188. * Update customTitle
  189. * @memberOf AdminCustomizeContainer
  190. */
  191. async updateCustomizeTitle() {
  192. try {
  193. const response = await apiv3Put('/customize-setting/customize-title', {
  194. customizeTitle: this.state.currentCustomizeTitle,
  195. });
  196. const { customizedParams } = response.data;
  197. this.setState({
  198. customizeTitle: customizedParams.customizeTitle,
  199. });
  200. }
  201. catch (err) {
  202. logger.error(err);
  203. throw new Error('Failed to update data');
  204. }
  205. }
  206. async updateCustomizeNoscript() {
  207. try {
  208. const response = await apiv3Put('/customize-setting/customize-noscript', {
  209. customizeNoscript: this.state.currentCustomizeNoscript,
  210. });
  211. const { customizedParams } = response.data;
  212. this.setState({
  213. currentCustomizeNoscript: customizedParams.customizeNoscript,
  214. });
  215. }
  216. catch (err) {
  217. logger.error(err);
  218. throw new Error('Failed to update data');
  219. }
  220. }
  221. /**
  222. * Update customCss
  223. * @memberOf AdminCustomizeContainer
  224. */
  225. async updateCustomizeCss() {
  226. try {
  227. const response = await apiv3Put('/customize-setting/customize-css', {
  228. customizeCss: this.state.currentCustomizeCss,
  229. });
  230. const { customizedParams } = response.data;
  231. this.setState({
  232. currentCustomizeCss: customizedParams.customizeCss,
  233. });
  234. }
  235. catch (err) {
  236. logger.error(err);
  237. throw new Error('Failed to update data');
  238. }
  239. }
  240. /**
  241. * Update customize script
  242. * @memberOf AdminCustomizeContainer
  243. * @return {string} Customize scripts
  244. */
  245. async updateCustomizeScript() {
  246. try {
  247. const response = await apiv3Put('/customize-setting/customize-script', {
  248. customizeScript: this.state.currentCustomizeScript,
  249. });
  250. const { customizedParams } = response.data;
  251. this.setState({
  252. currentCustomizeScript: customizedParams.customizeScript,
  253. });
  254. }
  255. catch (err) {
  256. logger.error(err);
  257. throw new Error('Failed to update data');
  258. }
  259. }
  260. }