diff --git a/pages/inventory/inventory.go b/pages/inventory/inventory.go index 473698ed..c5e11673 100644 --- a/pages/inventory/inventory.go +++ b/pages/inventory/inventory.go @@ -14,22 +14,17 @@ import ( // Get inventory page. func Get(ctx *aero.Context) string { user := utils.GetUser(ctx) + viewUser := user if user == nil { return ctx.Error(http.StatusUnauthorized, "Not logged in", nil) } - inventory, err := arn.GetInventory(user.ID) + inventory, err := arn.GetInventory(viewUser.ID) if err != nil { return ctx.Error(http.StatusInternalServerError, "Error fetching inventory data", err) } - // TEST - inventory.AddItem("anime-support-ticket", 35) - inventory.AddItem("pro-account-24", 20) - inventory.AddItem("anime-support-ticket", 15) - inventory.AddItem("pro-account-24", 10) - - return ctx.HTML(components.Inventory(inventory)) + return ctx.HTML(components.Inventory(inventory, viewUser)) } diff --git a/pages/inventory/inventory.pixy b/pages/inventory/inventory.pixy index 11326778..ccdb7a63 100644 --- a/pages/inventory/inventory.pixy +++ b/pages/inventory/inventory.pixy @@ -1,11 +1,11 @@ -component Inventory(inventory *arn.Inventory) +component Inventory(inventory *arn.Inventory, viewUser *arn.User) ShopTabs - .inventory + .inventory(data-api="/api/inventory/" + viewUser.ID) for index, slot := range inventory.Slots if slot.ItemID == "" .inventory-slot.mountable(draggable="false", data-index=index) else - .inventory-slot.mountable(title=slot.Item().Name, draggable="true", data-index=index) + .inventory-slot.mountable(title=slot.Item().Name, draggable="true", data-index=index, data-item-id=slot.ItemID, data-consumable=slot.Item().Consumable) Icon(slot.Item().Icon) if slot.Quantity > 1 .inventory-slot-quantity= slot.Quantity \ No newline at end of file diff --git a/patches/add-inventories/add-inventories.go b/patches/add-inventories/add-inventories.go index e1463128..c9c591a3 100644 --- a/patches/add-inventories/add-inventories.go +++ b/patches/add-inventories/add-inventories.go @@ -16,15 +16,20 @@ func main() { // Iterate over the stream for user := range allUsers { - exists, err := arn.DB.Exists("Inventory", user.ID) + // exists, err := arn.DB.Exists("Inventory", user.ID) - if err != nil || exists { - continue - } + // if err != nil || exists { + // continue + // } fmt.Println(user.Nick) inventory := arn.NewInventory(user.ID) + + // TEST + inventory.AddItem("anime-support-ticket", 50) + inventory.AddItem("pro-account-24", 30) + err = arn.DB.Set("Inventory", inventory.UserID, inventory) if err != nil { diff --git a/scripts/Actions.ts b/scripts/Actions.ts index aa1e925a..e7deb1fc 100644 --- a/scripts/Actions.ts +++ b/scripts/Actions.ts @@ -302,6 +302,25 @@ export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElemen .then(() => arn.loading(false)) } +// Use item +// export function useItem(arn: AnimeNotifier, button: HTMLElement) { +// let slotIndex = "" +// let parent = button + +// while(parent = parent.parentElement) { +// if(parent.dataset.index !== undefined) { +// slotIndex = parent.dataset.index +// break +// } +// } + +// let apiEndpoint = arn.findAPIEndpoint(button) + +// arn.post(apiEndpoint + "/use/" + slotIndex, "") +// .then(() => arn.reloadContent()) +// .catch(err => arn.statusMessage.showError(err)) +// } + // Chrome extension installation export function installExtension(arn: AnimeNotifier, button: HTMLElement) { let browser: any = window["chrome"] diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 4a94eb7d..c61b33dc 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -248,11 +248,32 @@ export class AnimeNotifier { dragAndDrop() { for(let element of findAll("inventory-slot")) { + // Skip elements that have their event listeners attached already + if(element["listeners-attached"]) { + continue + } + + // If the slot has an item and is therefore draggable if(element.draggable) { element.addEventListener("dragstart", e => { let element = e.target as HTMLElement e.dataTransfer.setData("text", element.dataset.index) }, false) + + element.addEventListener("dblclick", e => { + let itemName = element.title + + if(element.dataset.consumable !== "true") { + return this.statusMessage.showError(itemName + " is not a consumable item.") + } + + let apiEndpoint = this.findAPIEndpoint(element) + + this.post(apiEndpoint + "/use/" + element.dataset.index, "") + .then(() => this.reloadContent()) + .then(() => this.statusMessage.showInfo(`You used ${itemName}.`)) + .catch(err => this.statusMessage.showError(err)) + }, false) } element.addEventListener("dragenter", e => { @@ -283,12 +304,22 @@ export class AnimeNotifier { return } + // Swap in database + let apiEndpoint = this.findAPIEndpoint(inventory) + + this.post(apiEndpoint + "/swap/" + fromIndex + "/" + toIndex, "") + .catch(err => this.statusMessage.showError(err)) + + // Swap in UI toElement.classList.remove("drag-enter") swapElements(fromElement, toElement) fromElement.dataset.index = toIndex toElement.dataset.index = fromIndex }, false) + + // Prevent re-attaching the same listeners + element["listeners-attached"] = true } } @@ -660,6 +691,10 @@ export class AnimeNotifier { } findAPIEndpoint(element: HTMLElement) { + if(element.dataset.api !== undefined) { + return element.dataset.api + } + let apiObject: HTMLElement let parent = element