Untitled

 avatar
unknown
plain_text
2 years ago
5.2 kB
5
Indexable
    submitHandler(event) {
        return __awaiter(this, void 0, void 0, function* () {
            event.preventDefault();
            const userInput = this.gatherUserInput();
            if (Array.isArray(userInput)) {
                let validator = new validators_js_1.Post();
                [validator.title, validator.amount] = userInput;
                console.log(validator.title);
                console.log(validator.amount);
                if (yield validator.validate()) {
                    // first validate the input
                    if (this.nameInputElement instanceof HTMLInputElement) {
                        console.log("input name");
                        // in case of inserting a new resource or existing resource
                        if (exports.data.addResource(validator.title.trim(), validator.amount) === resource_js_1.Result.Add) {
                            this.addOptionBorrow(validator.title); // adds option to the borrow select options
                        }
                    }
                    else {
                        // in case of borrowing a resource
                        console.log("select input");
                        exports.data.UpdateExistingItemOrBorrowItem = new resource_js_1.Resource(validator.title.trim(), -Math.abs(validator.amount)
                        // makes negative to reduce resource amount
                        );
                        console.log("borrow resource");
                        this.amountHandler();
                        //remove empty resources
                    }
                }
                this.clearInputs();
                console.log(exports.data.getResources);
            }
            else {
                console.log(userInput);
            }
        });
    }




class ResourceStorage {
    constructor() {
        this.listeners = [];
        this.resources = [];
        this.addListener(this.removesEmptyResources);
    }
    removesEmptyResources() {
        console.log(this);
        console.log(this.resources);
        const relevantResources = this.resources.slice().filter((r) => {
            if (r.getResourceAmount > 0) {
                return true;
            }
            else
                return false;
        });
        this.setResources = relevantResources;
    }



 addResource(title, amount) {
        // create instance of resource
        const nr = new Resource(title, amount
        // ProjectStatus.Active
        );
        // first checks if input isn't for the first item of resource storage array or if it's of an existing item
        //because there are 2 options wether we want to add a new item or update existing item
        if (Array.isArray(this.getResources) &&
            this.getResources.length > 0 &&
            this.getResources.some(function (r) {
                return r.getResourceName === title;
            })) {
            // update item
            this.UpdateExistingItemOrBorrowItem = nr;
            return Result.Update;
        }
        else if (this.resources.push(nr)) {
            console.log("push new item");
            // add a new item to resources array
            this.executeListeners();
            return Result.Add;
        }
        return null;
    }


set UpdateExistingItemOrBorrowItem(r) {
        //a function to update existing item amount
        const i = this.resources.findIndex((checked) => checked.getResourceName === r.getResourceName);
        // find existing item to be able to update
        if (Array.isArray(this.getResources)) {
            // stores previous amount to add new amount and for log porpouses
            const previousAmount = this.getResources[i].getResourceAmount;
            try {
                const sum = r.getResourceAmount + previousAmount;
                if (sum < 0) {
                    alert("Cannot take more than " + previousAmount + " from this resource");
                    console.log("Cannot take more than " + previousAmount + " from this resource");
                    return;
                }
                alert(`update given resource: ${r.getResourceName} with given amount: ${sum}`);
                console.log(`update given resource: ${r.getResourceName} with given amount: ${sum}`);
            }
            catch (err) {
                // throw new Error("Update failed: updated amount is not in range (1 - "+Number.MAX_SAFE_INTEGER+")");
                alert("Update failed: updated amount is not in range (1 - " +
                    Number.MAX_SAFE_INTEGER +
                    ")");
                console.log("Update failed: updated amount is not in range (1 - " +
                    Number.MAX_SAFE_INTEGER +
                    ")");
                console.log(err);
                return;
            }
            this.getResources[i].updateResourceAmount = r.getResourceAmount;
            this.executeListeners();
            return;
        }
        else {
            throw new Error("Update failed: there isn't any item in the storage");
        }
    }
}