Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/category-creation-position #814

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/elm/Page/Community/Settings/Shop/Categories.elm
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,32 @@ update msg model loggedIn =

EditingNewCategory { parent } ->
parent

position =
case Community.getField loggedIn.selectedCommunity .shopCategories of
RemoteData.Success ( _, categories ) ->
case parentId of
Nothing ->
categories
|> List.map (Tree.label >> .position)
|> List.maximum
|> Maybe.map ((+) 1)
|> Maybe.withDefault 0

Just validParentId ->
categories
|> Utils.Tree.findZipperInForest (\category -> category.id == validParentId)
|> Maybe.andThen
(Tree.Zipper.tree
>> Tree.children
>> List.map (Tree.label >> .position)
>> List.maximum
>> Maybe.map ((+) 1)
)
|> Maybe.withDefault 0

_ ->
0
in
{ model
| newCategoryState =
Expand All @@ -323,6 +349,7 @@ update msg model loggedIn =
, slug = slug
, image = image
, parentId = parentId
, position = position
}
Shop.Category.selectionSet
)
Expand Down
11 changes: 7 additions & 4 deletions src/elm/Shop/Category.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type alias Model =
, metaTitle : Maybe String
, metaDescription : Maybe String
, metaKeywords : List String
, position : Int
}


Expand All @@ -54,10 +55,11 @@ create :
, slug : Slug
, image : Maybe String
, parentId : Maybe Id
, position : Int
}
-> SelectionSet decodesTo Cambiatus.Object.Category
-> SelectionSet (Maybe decodesTo) RootMutation
create { icon, name, slug, description, image, parentId } =
create { icon, name, slug, description, image, parentId, position } =
Cambiatus.Mutation.category
(\optionals ->
{ optionals
Expand All @@ -70,7 +72,7 @@ create { icon, name, slug, description, image, parentId } =
, name = OptionalArgument.Present name
, description = OptionalArgument.Present (Markdown.toRawString description)
, imageUri = OptionalArgument.fromMaybe image
, position = OptionalArgument.Present 0
, position = OptionalArgument.Present position
}
)

Expand Down Expand Up @@ -181,6 +183,7 @@ selectionSet =
>> Maybe.withDefault []
)
)
|> SelectionSet.with Cambiatus.Object.Category.position



Expand Down Expand Up @@ -212,7 +215,7 @@ treesSelectionSet categoriesSelectionSet =
Nothing
)
children
|> List.sortBy (Tree.label >> .name)
|> List.sortBy (Tree.label >> .position)
)
in
categoriesSelectionSet selectionSet
Expand All @@ -227,7 +230,7 @@ treesSelectionSet categoriesSelectionSet =
Nothing
)
allCategories
|> List.sortBy (Tree.label >> .name)
|> List.sortBy (Tree.label >> .position)
)


Expand Down