AdminCustomizeContainer.js 9.4 KB

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