aboutsummaryrefslogtreecommitdiffstats
path: root/web_src/js/features/admin/config.js
blob: f5d8fae8facf331ece65fd105699bf9c64cb1f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import $ from 'jquery';
import {showTemporaryTooltip} from '../../modules/tippy.js';

const {appSubUrl, csrfToken, pageData} = window.config;

export function initAdminConfigs() {
  const isAdminConfigPage = pageData?.adminConfigPage;
  if (!isAdminConfigPage) return;

  $("input[type='checkbox']").on('change', (e) => {
    const $this = $(e.currentTarget);
    $.ajax({
      url: `${appSubUrl}/admin/config`,
      type: 'POST',
      data: {
        _csrf: csrfToken,
        key: $this.attr('name'),
        value: $this.is(':checked'),
        version: $this.attr('version'),
      }
    }).done((resp) => {
      if (resp) {
        if (resp.redirect) {
          window.location.href = resp.redirect;
        } else if (resp.version) {
          $this.attr('version', resp.version);
        } else if (resp.err) {
          showTemporaryTooltip(e.currentTarget, resp.err);
          $this.prop('checked', !$this.is(':checked'));
        }
      }
    });

    e.preventDefault();
    return false;
  });
}