Skip to content

Commit

Permalink
refactor: Adding typing for display mode
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 11, 2023
1 parent 4241ff6 commit d0d3565
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
10 changes: 5 additions & 5 deletions pkg/crud/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ func (a App) updateCover(ctx context.Context, item absto.Item) error {
func updatePreferences(request provider.Request, oldPath, newPath string) {
paths := request.Preferences.LayoutPaths

for index, layoutPath := range paths {
if layoutPath != oldPath {
paths[index] = newPath
} else {
paths[index] = layoutPath
for path, display := range paths {
if path == oldPath {
delete(paths, path)

paths[newPath] = display
}
}
}
13 changes: 1 addition & 12 deletions pkg/fibr/fibr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (a App) parseRequest(r *http.Request) (provider.Request, error) {
Path: r.URL.Path,
CanEdit: false,
CanShare: false,
Display: parseDisplay(r),
Display: provider.ParseDisplay(r.URL.Query().Get("d")),
Preferences: parsePreferences(r),
}

Expand Down Expand Up @@ -90,17 +90,6 @@ func (a App) parseRequest(r *http.Request) (provider.Request, error) {
return request, nil
}

func parseDisplay(r *http.Request) string {
switch r.URL.Query().Get("d") {
case provider.ListDisplay:
return provider.ListDisplay
case provider.StoryDisplay:
return provider.StoryDisplay
default:
return provider.DefaultDisplay
}
}

func parsePreferences(r *http.Request) provider.Preferences {
var cookieValue string
if cookie, err := r.Cookie(provider.LayoutPathsCookieName); err == nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/fibr/fibr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ func TestParseRequest(t *testing.T) {
CanShare: true,
CanWebhook: true,
Preferences: provider.Preferences{
LayoutPaths: map[string]string{
"assets": "list",
"documents/monthly": "story",
LayoutPaths: map[string]provider.Display{
"assets": provider.ListDisplay,
"documents/monthly": provider.StoryDisplay,
},
},
},
Expand Down
43 changes: 31 additions & 12 deletions pkg/provider/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
absto "github.com/ViBiOh/absto/pkg/model"
)

type Display string

var (
// GridDisplay format
GridDisplay = "grid"
GridDisplay Display = "grid"
// ListDisplay format
ListDisplay = "list"
ListDisplay Display = "list"
// StoryDisplay format
StoryDisplay = "story"
StoryDisplay Display = "story"

// DefaultDisplay format
DefaultDisplay = GridDisplay
Expand All @@ -31,8 +33,24 @@ var (
}
)

func ParseDisplay(input string) Display {
switch input {
case string(GridDisplay):
return GridDisplay

case string(ListDisplay):
return ListDisplay

case string(StoryDisplay):
return StoryDisplay

default:
return DefaultDisplay
}
}

type Preferences struct {
LayoutPaths map[string]string
LayoutPaths map[string]Display
}

func ParsePreferences(value string) Preferences {
Expand All @@ -42,21 +60,21 @@ func ParsePreferences(value string) Preferences {
return output
}

output.LayoutPaths = make(map[string]string)
output.LayoutPaths = make(map[string]Display)

for _, part := range strings.Split(value, ",") {
parts := strings.SplitN(part, preferencesPathSeparator, 2)
if len(parts) == 2 {
output.LayoutPaths[parts[0]] = parts[1]
output.LayoutPaths[parts[0]] = ParseDisplay(parts[1])
}
}

return output
}

func (p Preferences) AddLayout(path, display string) Preferences {
func (p Preferences) AddLayout(path string, display Display) Preferences {
if p.LayoutPaths == nil {
p.LayoutPaths = map[string]string{
p.LayoutPaths = map[string]Display{
path: display,
}
return p
Expand All @@ -75,7 +93,7 @@ func (p Preferences) RemoveLayout(path string) Preferences {
type Request struct {
Path string
Item string
Display string
Display Display
Preferences Preferences
Share Share
CanEdit bool
Expand All @@ -90,7 +108,7 @@ func (r Request) String() string {
output.WriteString(strconv.FormatBool(r.CanEdit))
output.WriteString(r.Item)
output.WriteString(strconv.FormatBool(r.CanShare))
output.WriteString(r.Display)
output.WriteString(string(r.Display))
output.WriteString(strconv.FormatBool(r.CanWebhook))
output.WriteString(r.Share.String())

Expand Down Expand Up @@ -142,10 +160,11 @@ func (r Request) SubPath(name string) string {
return Join(r.Share.Path, r.Path, name)
}

func (r Request) LayoutPath(path string) string {
func (r Request) LayoutPath(path string) Display {
if layout, ok := r.Preferences.LayoutPaths[path]; ok {
return layout
}

return DefaultDisplay
}

Expand Down Expand Up @@ -199,7 +218,7 @@ func computeLayoutPaths(request Request) string {

builder.WriteString(key)
builder.WriteString("|")
builder.WriteString(value)
builder.WriteString(string(value))
}

return builder.String()
Expand Down
8 changes: 4 additions & 4 deletions pkg/provider/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestLayoutPath(t *testing.T) {
cases := map[string]struct {
instance Request
args args
want string
want Display
}{
"empty list": {
Request{},
Expand All @@ -243,9 +243,9 @@ func TestLayoutPath(t *testing.T) {
"story": {
Request{
Preferences: Preferences{
LayoutPaths: map[string]string{
"/sheets": "list",
"/reports": "story",
LayoutPaths: map[string]Display{
"/sheets": ListDisplay,
"/reports": StoryDisplay,
},
},
},
Expand Down

0 comments on commit d0d3565

Please sign in to comment.