Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
3
Indexable
Never
 addResource(title: string, amount: number) {
    const newResource = new Resource(
      title,
      amount
      // ProjectStatus.Active
    );
    if (this.resources.push(newResource)) console.log("push");
    for (const listenerFn of this.listeners) {
      listenerFn(this.resources.slice());
      console.log(listenerFn);
    }
    return true;
  }
  get getResources() {
    if (this.resources) return this.resources;
    else return "null";
  }
  set setResources(array: Resource[]) {
    this.resources = array;
  }
  get getResourcesLength(): number {
    return this.resources.length;
  }
  uniqByReduce(array: Resource[]) {
    this.setResources = array.reduce((acc: Resource[], cur: Resource) => { // EXCEPTION this.resources is undefined
      if (!acc.includes(cur)) {
        acc.push(cur);
      } else if (
        acc.slice().filter(function (r) {
          return r.getResourceName === cur.getResourceName;
        }).length > 0) {
          alert(`update given resource: ${cur.getResourceName} with given amount: ${cur.getResourceAmount}`);
          console.log(`update given resource: ${cur.getResourceName} with given amount: ${cur.getResourceAmount}`);
        // checks wether checked resource included in the array and just update it's amount
        const i = acc.findIndex(
          (r) => r.getResourceName === cur.getResourceName
        );
        if (i > -1) {
          acc[i] = cur;
        }
      }
      return acc;
    }, []);
  }
}

 public async submitHandler(event: Event) {
    event.preventDefault();
    const userInput = this.gatherUserInput();
    if (Array.isArray(userInput)) {
      let validator = new Post();
      [validator.title,validator.amount] = userInput;
      console.log(validator.title);
      console.log(validator.amount);
      if(await validator.validate())
      {
       if(data.addResource(validator.title,validator.amount))
       console.log("added resource");
      }
      this.clearInputs();
      console.log(data.getResources);
    }
    else
    {
      console.log(userInput);
    }
  }