AdminCustomizeContainer.js 8.2 KB

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