import Component from "@glimmer/component"; import { cached } from "@glimmer/tracking"; import { inject as controller } from "@ember/controller"; import { action } from "@ember/object"; import { service } from "@ember/service"; import { isEmpty } from "@ember/utils"; import { eq } from "truth-helpers"; import BackButton from "discourse/components/back-button"; import Form from "discourse/components/form"; import { popupAjaxError } from "discourse/lib/ajax-error"; import { bind } from "discourse-common/utils/decorators"; import { i18n } from "discourse-i18n"; import AdminConfigAreaCard from "admin/components/admin-config-area-card"; import Permalink from "admin/models/permalink"; const TYPE_TO_FIELD_MAP = { topic: "topicId", post: "postId", category: "categoryId", tag: "tagName", user: "userId", external_url: "externalUrl", }; export default class AdminFlagsForm extends Component { @service router; @service store; @controller adminPermalinks; get isUpdate() { return this.args.permalink; } @cached get formData() { if (this.isUpdate) { let permalinkType; let permalinkValue; if (!isEmpty(this.args.permalink.topic_id)) { permalinkType = "topic"; permalinkValue = this.args.permalink.topic_id; } else if (!isEmpty(this.args.permalink.post_id)) { permalinkType = "post"; permalinkValue = this.args.permalink.post_id; } else if (!isEmpty(this.args.permalink.category_id)) { permalinkType = "category"; permalinkValue = this.args.permalink.category_id; } else if (!isEmpty(this.args.permalink.tag_name)) { permalinkType = "tag"; permalinkValue = this.args.permalink.tag_name; } else if (!isEmpty(this.args.permalink.external_url)) { permalinkType = "external_url"; permalinkValue = this.args.permalink.external_url; } else if (!isEmpty(this.args.permalink.user_id)) { permalinkType = "user"; permalinkValue = this.args.permalink.user_id; } return { url: this.args.permalink.url, [TYPE_TO_FIELD_MAP[permalinkType]]: permalinkValue, permalinkType, }; } else { return { permalinkType: "topic", }; } } get header() { return this.isUpdate ? "admin.permalink.form.edit_header" : "admin.permalink.form.add_header"; } @action async save(data) { this.isUpdate ? await this.update(data) : await this.create(data); } @bind async create(data) { try { const result = await this.store.createRecord("permalink").save({ url: data.url, permalink_type: data.permalinkType, permalink_type_value: this.valueForPermalinkType(data), }); this.adminPermalinks.model.unshift(Permalink.create(result.payload)); this.router.transitionTo("adminPermalinks"); } catch (error) { popupAjaxError(error); } } @bind async update(data) { try { const result = await this.store.update( "permalink", this.args.permalink.id, { url: data.url, permalink_type: data.permalinkType, permalink_type_value: this.valueForPermalinkType(data), } ); const index = this.adminPermalinks.model.findIndex( (permalink) => permalink.id === this.args.permalink.id ); this.adminPermalinks.model[index] = Permalink.create(result.payload); this.router.transitionTo("adminPermalinks"); } catch (error) { popupAjaxError(error); } } valueForPermalinkType(data) { return data[TYPE_TO_FIELD_MAP[data.permalinkType]]; } }