Skip to content
Snippets Groups Projects
section-model.js 2.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis POYEN's avatar
    Alexis POYEN committed
    import * as Messages from "/services/messages/messages.js";
    
    let sectionModel;
    
    export function getSectionModel() {
      if (sectionModel == null) {
        sectionModel = new SectionModel();
      }
      return sectionModel;
    }
    
    class SectionModel {
      constructor() {}
    
      async getSection(id) {
        if (this.sections == null) await this.refreshSections();
        let sectionToGet;
        this.sections.forEach((section) => {
          if (section.ID == id) sectionToGet = section;
        });
        return sectionToGet;
      }
    
      async getSections() {
        if (this.sections == null) {
          try {
            const response = await fetch("/api/Section/", {
              method: "GET",
              headers: new Headers({
                "XSRF-Token": this.current_user.xsrftoken,
              }),
            });
            if (response.status !== 200) {
              throw new Error(
                `Sections could not be fetched (status ${response.status})`
              );
            }
            this.sections = await response.json();
          } catch (e) {
            Messages.Show("is-warning", e.message);
            console.error(e);
          }
        }
        return this.sections;
      }
    
    
    Alexis POYEN's avatar
    Alexis POYEN committed
      async saveSection(method, ID, AreaID, Name, MapID) {
    
    Alexis POYEN's avatar
    Alexis POYEN committed
        try {
          const response = await fetch("/api/Section/" + ID, {
            method: method,
            headers: new Headers({
              "XSRF-Token": this.current_user.xsrftoken,
            }),
            body: JSON.stringify({
              ID: ID,
              AreaID: AreaID,
              Name: Name,
    
    Alexis POYEN's avatar
    Alexis POYEN committed
              MapID: MapID,
    
    Alexis POYEN's avatar
    Alexis POYEN committed
            }),
          });
          if (response.status !== 200) {
            throw new Error(
              `Section could not be updated or created (status ${response.status})`
            );
          }
          this.refreshSections();
          return await response.json();
        } catch (e) {
          Messages.Show("is-warning", e.message);
          console.error(e);
          return;
        }
      }
    
      async deleteSections(ID) {
        try {
          const response = await fetch("/api/Section/" + ID, {
            method: "delete",
            headers: new Headers({
              "XSRF-Token": this.current_user.xsrftoken,
            }),
          });
          if (response.status !== 200) {
            throw new Error(
              `Section could not be deleted (status ${response.status})`
            );
          }
        } catch (e) {
          Messages.Show("is-warning", e.message);
          console.error(e);
        }
      }
    
      async refreshSections() {
        this.sections = null;
        await this.getSections();
      }
    }