Skip to content

Commit

Permalink
add/delete card feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mebitek committed May 26, 2023
1 parent 1c6ce97 commit 427c33b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ___

# planned features

- [ ] add/remove cards
- [x] add/remove cards
- [ ] add/edit/delete stacks
- [ ] add/edit/delete boards
- [ ] manage comments
Expand Down
38 changes: 38 additions & 0 deletions deck_http/deck_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ func GetStacks(boardId int, configuration utils.Configuration) ([]deck_structs.S
return stacks, nil
}

func AddCard(boardId int, stackId int, jsonBody string, configuration utils.Configuration) (deck_structs.Card, error) {
body := []byte(jsonBody)

call, err := httpCall(body, http.MethodPost,
fmt.Sprintf("%s/index.php/apps/deck/api/v1.0/boards/%d/stacks/%d/cards", configuration.Url, boardId, stackId),
configuration.User, configuration.Password)
if err != nil {
return deck_structs.Card{}, err

}
decoder := json.NewDecoder(call.Body)
var card deck_structs.Card

err = decoder.Decode(&card)
if err != nil {
panic(err)
}
return card, nil
}

func UpdateCard(boardId int, stackId int, cardId int, jsonBody string, configuration utils.Configuration) (deck_structs.Card, error) {
body := []byte(jsonBody)

Expand All @@ -93,6 +113,24 @@ func UpdateCard(boardId int, stackId int, cardId int, jsonBody string, configura
}
return card, nil
}
func DeleteCard(boardId int, stackId int, cardId int, configuration utils.Configuration) (deck_structs.Card, error) {

call, err := httpCall(nil, http.MethodDelete,
fmt.Sprintf("%s/index.php/apps/deck/api/v1.0/boards/%d/stacks/%d/cards/%d", configuration.Url, boardId, stackId, cardId),
configuration.User, configuration.Password)
if err != nil {
return deck_structs.Card{}, err

}
decoder := json.NewDecoder(call.Body)
var card deck_structs.Card

err = decoder.Decode(&card)
if err != nil {
panic(err)
}
return card, nil
}

func GetBoardDetail(boardId int, configuration utils.Configuration) (deck_structs.Board, error) {
call, err := httpCall(nil, http.MethodGet,
Expand Down
1 change: 1 addition & 0 deletions deck_structs/deck_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Card struct {
Labels []Label
StackId int
Order int
Type string
}

type Label struct {
Expand Down
88 changes: 87 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
Expand Down Expand Up @@ -119,6 +120,34 @@ func main() {
} else if event.Rune() == 115 {
// s -> switch board
go buildFullFlex(boardList)

} else if event.Rune() == 97 {
// a -> add card
actualList := app.GetFocus().(*tview.List)

addForm, card := buildAddForm()

//TODO add due Date input field
addForm.AddButton("Save", func() {
addCard(*actualList, *card)

})

buildFullFlex(addForm)

} else if event.Rune() == 100 {
// d -> delete card
actualList := app.GetFocus().(*tview.List)
var _, stack, _ = getActualStack(*actualList)

var currentItemIndex = actualList.GetCurrentItem()
mainText, _ := actualList.GetItemText(currentItemIndex)
cardId := getCardId(mainText)

go deck_http.DeleteCard(currentBoard.Id, stack.Id, cardId, configuration)

actualList.RemoveItem(currentItemIndex)

} else if event.Rune() == 63 {
// ? deck_help menu
buildHelp(mainFlex, deck_help.HelpMain)
Expand Down Expand Up @@ -236,7 +265,7 @@ func main() {

buildFullFlex(editTagsFlex)
} else if event.Rune() == 63 {
// ? deck_help menu
// ? -> deck_help menu
buildHelp(detailText, deck_help.HelpView)
}
return event
Expand Down Expand Up @@ -271,6 +300,15 @@ func main() {

}

func getActualStack(actualList tview.List) (int, deck_structs.Stack, error) {
for i, s := range stacks {
if s.Title == strings.TrimSpace(actualList.GetTitle()) {
return i, s, nil
}
}
return 0, deck_structs.Stack{}, errors.New("not found")
}

func getNextFocus(index int) tview.Primitive {
if index == len(primitivesIndexMap) {
index = 0
Expand Down Expand Up @@ -508,7 +546,55 @@ func editCard() {
if err != nil {
footerBar.SetText(fmt.Sprintf("Error updating card: %s", err.Error()))
}
}

func addCard(actualList tview.List, card deck_structs.Card) {
var stackIndex, stack, _ = getActualStack(actualList)

jsonBody := fmt.Sprintf(`{"title":"%s", "description": "%s", "type": "plain", "order": 0}`, card.Title, card.Description)
var newCard deck_structs.Card
var err error
newCard, err = deck_http.AddCard(currentBoard.Id, stack.Id, jsonBody, configuration)
if err != nil {
footerBar.SetText(fmt.Sprintf("Error crating new card: %s", err.Error()))
}

actualList.InsertItem(0, fmt.Sprintf("[%s]#%d[white] - %s ", configuration.Color, newCard.Id, newCard.Title), "", rune(0), nil)
cardsMap[newCard.Id] = newCard
detailText.Clear()
editableCard = newCard
stacks[stackIndex].Cards = append(stacks[stackIndex].Cards[:1], stacks[stackIndex].Cards[0:]...)
stacks[stackIndex].Cards[0] = newCard
detailText.SetTitle(fmt.Sprintf(" %s ", newCard.Title))
detailText.SetText(formatDescription(newCard.Description))
buildFullFlex(detailText)
}

func buildAddForm() (*tview.Form, *deck_structs.Card) {
addForm := tview.NewForm()
card := deck_structs.Card{}
addForm.SetTitle(" Add Card ")
addForm.SetBorder(true)
addForm.SetBorderColor(utils.GetColor(configuration.Color))
addForm.SetButtonBackgroundColor(utils.GetColor(configuration.Color))
addForm.SetFieldBackgroundColor(tcell.ColorWhite)
addForm.SetFieldTextColor(tcell.ColorBlack)
addForm.SetLabelColor(utils.GetColor(configuration.Color))
addForm.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEsc {
buildFullFlex(mainFlex)
return nil
}
return event
})
addForm.AddInputField("Title", "", 20, nil, func(title string) {
card.Title = title
})
addForm.AddTextArea("Description", "", 60, 10, 300, func(description string) {
card.Description = description
})

return addForm, &card
}

func updateStacks() {
Expand Down

0 comments on commit 427c33b

Please sign in to comment.