account page: fixed platform reordering bugs
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
type accountSubrouter struct {
|
||||
log *logging.Logger
|
||||
accts *accounts.Store
|
||||
pub *sse.UpdateNotificationPublisher
|
||||
}
|
||||
|
||||
func Routes(
|
||||
@@ -30,6 +32,7 @@ func Routes(
|
||||
as := &accountSubrouter{
|
||||
log: logger,
|
||||
accts: accts,
|
||||
pub: pub,
|
||||
}
|
||||
|
||||
r.POST("", response.Handler(as.createAccount))
|
||||
@@ -187,9 +190,13 @@ func getOrderIndexForSyncGroupListingDraftFromPath(c *gin.Context) (int, error)
|
||||
return orderIndex, nil
|
||||
}
|
||||
|
||||
// PUT /:acctID/platforms/:platform/order-index"
|
||||
// this endpoint is called when dragging a platform tab in the accounts page.
|
||||
func (s *accountSubrouter) setOrderOfPlatformOnAccountPage(c *gin.Context) (response.Response, error) {
|
||||
acctID := auth.GetIdentity(c).Account.AccountID
|
||||
|
||||
// validate parameters
|
||||
|
||||
var orderIndex int
|
||||
if v, ok := c.GetPostForm("order-index"); !ok {
|
||||
return nil, response.BadRequest().
|
||||
@@ -216,9 +223,34 @@ func (s *accountSubrouter) setOrderOfPlatformOnAccountPage(c *gin.Context) (resp
|
||||
platform = p
|
||||
}
|
||||
|
||||
if err := s.accts.SetOrderOfPlatformOnAccountPage(c, acctID, platform, orderIndex); err != nil {
|
||||
// update the order
|
||||
|
||||
platforms, prevIndex, err := s.accts.SetOrderOfPlatformOnAccountPage(c, acctID, platform, orderIndex)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to save record: %w", err)
|
||||
}
|
||||
|
||||
return response.Status(200), nil
|
||||
// emit events on all platforms updated, so the tabs can all refresh (including their logic)
|
||||
|
||||
platformEvents := make([]string, max(orderIndex, prevIndex)-min(orderIndex, prevIndex))
|
||||
if orderIndex > prevIndex {
|
||||
for i := range orderIndex - prevIndex {
|
||||
p := platforms[prevIndex+i]
|
||||
platformEvents[i] = fmt.Sprintf("accounts_%d_platforms_%s_order-index", acctID, lowerSnakeCase(p))
|
||||
}
|
||||
} else if orderIndex < prevIndex {
|
||||
for i := range prevIndex - orderIndex {
|
||||
p := platforms[orderIndex+1+i]
|
||||
platformEvents[i] = fmt.Sprintf("accounts_%d_platforms_%s_order-index", acctID, lowerSnakeCase(p))
|
||||
}
|
||||
}
|
||||
if err := s.pub.Push(c, acctID, platformEvents...); err != nil {
|
||||
s.log.Errorf("failed to publish platform order-index events: %v", err)
|
||||
}
|
||||
|
||||
return response.StatusNoContent(), nil
|
||||
}
|
||||
|
||||
func lowerSnakeCase(s accounts.Platform) string {
|
||||
return strings.ToLower(strings.Join(strings.Split(string(s), " "), "_"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user