AdminCustomizeContainer.js 8.9 KB

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