AdminCustomizeContainer.js 9.4 KB

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