import { createItem, getAllItems, getItem, updateItem } from "../../api/client"; import moment from "moment"; const PATH = "news"; const DATE_DB = "DD.MM.YYYY"; const DATE_UI = "YYYY-MM-DD"; export default class FormModel { constructor() { this.element = {}; this.tegs = []; } async getTegs() { this.tegs = []; this.tegs = await getAllItems("tegs"); } async get(id) { if (!id) { throw new Error("Element id is not defined!"); } this.element = await getItem(PATH, id); } async create() { if (!this.element || Object.keys(this.element).length === 0) { throw new Error("Item is null or empty!"); } this.element = await createItem(PATH, this.element); } async update() { if (!this.element) { throw new Error("Item is null or empty!"); } this.element = await updateItem(PATH, this.element.id, this.element); } getValue(attribute) { if (attribute !== "bdate") { return this.element[attribute] || ""; } return moment(this.element[attribute], DATE_DB).format(DATE_UI); } setValue(attribute, value) { if (attribute !== "bdate") { this.element[attribute] = value; } else { this.element[attribute] = moment(value, DATE_UI).format(DATE_DB); } } }