import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { on } from "@ember/modifier"; import { action } from "@ember/object"; import didInsert from "@ember/render-modifiers/modifiers/did-insert"; import { service } from "@ember/service"; import { and, not } from "truth-helpers"; import CookText from "discourse/components/cook-text"; import DToggleSwitch from "discourse/components/d-toggle-switch"; import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; import dIcon from "discourse-common/helpers/d-icon"; import i18n from "discourse-common/helpers/i18n"; import { bind } from "discourse-common/utils/decorators"; import I18n from "discourse-i18n"; import DTooltip from "float-kit/components/d-tooltip"; export default class DiscourseNewFeatureItem extends Component { @service siteSettings; @service toasts; @tracked experimentEnabled; @tracked toggleExperimentDisabled = false; @bind initEnabled() { this.experimentEnabled = this.siteSettings[this.args.item.experiment_setting]; } @action async toggleExperiment() { if (this.toggleExperimentDisabled) { this.toasts.error({ duration: 3000, data: { message: I18n.t( "admin.dashboard.new_features.experiment_toggled_too_fast" ), }, }); return; } this.experimentEnabled = !this.experimentEnabled; this.toggleExperimentDisabled = true; setTimeout(() => { this.toggleExperimentDisabled = false; }, 5000); try { await ajax("/admin/toggle-feature", { type: "POST", data: { setting_name: this.args.item.experiment_setting, }, }); this.toasts.success({ duration: 3000, data: { message: this.experimentEnabled ? I18n.t("admin.dashboard.new_features.experiment_enabled") : I18n.t("admin.dashboard.new_features.experiment_disabled"), }, }); } catch (error) { this.experimentEnabled = !this.experimentEnabled; return popupAjaxError(error); } } }