You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

157 lines
3.6 KiB

package assets
import (
"fmt"
"git.brettb.xyz/goinv/client/internal/ui/dialogs"
"git.brettb.xyz/goinv/client/internal/ui/style"
)
func (a *Assets) runCommand(cmd string) {
switch cmd {
case "create asset":
a.createDialog.Display()
case "edit asset":
a.cedit()
case "delete asset":
a.cdelete()
case "refresh":
a.crefresh()
}
}
func (a *Assets) cNotImplemented() {
a.displayError("not implemented", fmt.Errorf("this command has not been implemented"))
}
// Confirm deletion
func (a *Assets) cdelete() {
selectedItem := a.getSelectedItem()
// Empty table
if selectedItem == nil {
a.displayError("DELETE ASSET ERROR", fmt.Errorf("no assets to delete"))
return
}
title := "delete asset"
a.confirmDialog.SetTitle(title)
a.confirmData = status_CONFIRM_DELETE_ASSET
bgColor := style.GetColorHex(style.DialogSubBoxBorderColor)
fgColor := style.GetColorHex(style.DialogFgColor)
assetName := fmt.Sprintf("[%s:%s:b]ASSET NAME:[:-:-] %s", fgColor, bgColor, selectedItem.item)
assetQuantity := fmt.Sprintf(" [%s:%s:b]QUANTITY:[:-:-] %s", fgColor, bgColor, selectedItem.quantity)
confirmMsg := fmt.Sprintf("%s\n%s\nAre you sure you want to delete the selected asset ?", assetName, assetQuantity)
a.confirmDialog.SetText(confirmMsg)
a.confirmDialog.Display()
}
func (a *Assets) cedit() {
selectedItem := a.getSelectedItem()
if selectedItem == nil {
a.displayError("DELETE ASSET ERROR", fmt.Errorf("no assets to edit"))
return
}
asset, err := a.client.RetrieveAssetByID(selectedItem.id)
if err != nil {
a.displayError("DELETE ASSET ERROR", fmt.Errorf("unable to retrieve asset from server"))
return
}
a.editDialog.SetAsset(asset)
a.editDialog.Display()
}
func (a *Assets) edit() {
selectedItem := a.getSelectedItem()
createReq := a.editDialog.EditAssetOptions()
if createReq.Name == "" {
a.displayError("ASSET EDIT ERROR", fmt.Errorf("asset name cannot be empty"))
return
}
_, err := a.client.UpdateAsset(selectedItem.id, createReq)
if err != nil {
a.displayError("ASSET EDIT ERROR", err)
return
}
a.crefresh()
}
func (a *Assets) delete() {
selectedItem := a.getSelectedItem()
a.progressDialog.SetTitle(fmt.Sprintf("deleting asset %s", selectedItem.id))
a.progressDialog.Display()
del := func() {
_, err := a.client.DeleteAssetByID(selectedItem.id)
a.progressDialog.Hide()
if err != nil {
a.displayError("DELETE ASSET ERROR", err)
return
}
// display success message
a.messageDialog.SetTitle(fmt.Sprintf("deleting asset %s", selectedItem.id))
a.messageDialog.SetText(dialogs.MessageGeneric, "Success!", fmt.Sprintf("Asset %s successfully deleted.", selectedItem.id))
a.messageDialog.Display()
a.UpdateAssetData()
}
del()
}
func (a *Assets) crefresh() {
a.progressDialog.SetTitle("refreshing assets")
a.progressDialog.Display()
ref := func() {
a.UpdateShelfData()
a.UpdateAssetData()
a.progressDialog.Hide()
if !a.errorDialog.IsDisplay() {
a.messageDialog.SetTitle("asset refresh")
a.messageDialog.SetText(dialogs.MessageGeneric, "Refreshed!", "Successfully refreshed page.")
a.messageDialog.Display()
}
}
ref()
}
func (a *Assets) create() {
createReq := a.createDialog.CreateAssetOptions()
if createReq.Name == "" {
a.displayError("ASSET CREATE ERROR", fmt.Errorf("asset name cannot be empty"))
return
}
_, err := a.client.CreateAsset(createReq)
if err != nil {
a.displayError("ASSET CREATE ERROR", err)
return
}
a.crefresh()
a.assetTable.ScrollToEnd()
a.assetTable.Select(a.assetTable.GetRowCount(), 0)
}
func (a *Assets) displayError(title string, err error) {
a.errorDialog.SetTitle(title)
a.errorDialog.SetText(fmt.Sprintf("%v", err))
a.errorDialog.Display()
}