Skip to content

Commit

Permalink
Merge branch 'main' of github.com:NitroDevs/FShopOnWeb
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed Nov 6, 2022
2 parents 35a0ba1 + a5a0fef commit bfadaa9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
28 changes: 28 additions & 0 deletions src/Microsoft.eShopWeb.Web/Basket/Basket.Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,31 @@ module BasketDomain =
{ Id = 1
Items = List.mapi mapCatalogItem catalogItems
BuyerId = None }

let addItemToBasket basket (catalogItem: CatalogItem) =
// let id = form.TryGetString "id" |> Option.map int
let item: BasketItem option =
basket.Items |> List.tryFind (fun i -> i.CatalogItemId = catalogItem.Id)

match item with
| None ->
let items =
basket.Items
|> List.append [ (mapCatalogItem (basket.Items.Length + 1) catalogItem) ]

{ basket with Items = items }
| Some item ->
let quantity = item.Quantity + 1

let items =
basket.Items
|> List.map (fun i ->
if i.CatalogItemId = catalogItem.Id then
{ i with Quantity = quantity }
else
i)

{ basket with Items = items }

// TODO:
// let getCatalogItemById =
2 changes: 1 addition & 1 deletion src/Microsoft.eShopWeb.Web/Home/CatalogGrid.Component.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module CatalogGridComponent =
div
[ class' "esh-catalog-item col-md-4" ]
[ form
[ method "post"; action "/Basket" ]
[ method "post"; action "/basket" ]
[ img [ src item.PictureUri; class' "esh-catalog-thumbnail" ]
input [ class' "esh-catalog-button"; type' "submit"; value "[ ADD TO BASKET ]" ]
div [ class' "esh-catalog-name" ] [ span [] [ raw item.Name ] ]
Expand Down
48 changes: 33 additions & 15 deletions src/Microsoft.eShopWeb.Web/Home/CatalogPager.Component.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ module CatalogPagerComponent =
type Props = { CurrentPage: int; ItemsCount: int }

module private Template =
type State =
{ PagingText: string
PreviousClass: string
NextClass: string }

let pagingText props =
let calculateState props =
let currentPage = 1
let pageSize = 10
let totalPages = (props.ItemsCount / pageSize) + 1

let totalPages =
match (props.ItemsCount / pageSize) with
| 0 -> 1
| _ -> (props.ItemsCount / pageSize)

let morePagesAvailable = props.ItemsCount > pageSize
let hasPreviousPage = currentPage > 1
let hasNextPage = currentPage < totalPages
Expand All @@ -23,9 +32,27 @@ module CatalogPagerComponent =
| false -> props.ItemsCount

let currentItemIndex = (pageSize * (currentPage - 1)) + 1
sprintf "Showing %d of %d products - Page %d - %d" currentItemIndex totalItemsOnPage currentPage totalPages

let previousClass =
match hasPreviousPage with
| true -> "esh-pager-item-left esh-pager-item--navigable esh-pager-item"
| false -> "esh-pager-item-left esh-pager-item--navigable esh-pager-item is-disabled"

let nextClass =
match hasNextPage with
| true -> "esh-pager-item-right esh-pager-item--navigable esh-pager-item"
| false -> "esh-pager-item-right esh-pager-item--navigable esh-pager-item is-disabled"

let pagingText =
sprintf "Showing %d of %d products - Page %d - %d" currentItemIndex totalItemsOnPage currentPage totalPages

{ PagingText = pagingText
NextClass = nextClass
PreviousClass = previousClass }

let cmpt props =
let state = Template.calculateState props

div
[ class' "esh-pager" ]
[ div
Expand All @@ -36,17 +63,8 @@ module CatalogPagerComponent =
[]
[ div
[ class' "col-md-2 col-xs-12" ]
[ a
[ class' "esh-pager-item-left esh-pager-item--navigable esh-pager-item is-disabled"
href "/?pageId=-1" ]
[ raw "Previous" ] ]
div
[ class' "col-md-8 col-xs-12" ]
[ span [ class' "esh-pager-item" ] [ raw (Template.pagingText props) ] ]
[ a [ class' state.PreviousClass; href "/?pageId=-1" ] [ raw "Previous" ] ]
div [ class' "col-md-8 col-xs-12" ] [ span [ class' "esh-pager-item" ] [ raw state.PagingText ] ]
div
[ class' "col-md-2 col-xs-12" ]
[ a
[ class' "esh-pager-item-right esh-pager-item--navigable esh-pager-item is-disabled"
id "Next"
href "/?pageId=1" ]
[ raw "Next" ] ] ] ] ] ]
[ a [ class' state.NextClass; id "Next"; href "/?pageId=1" ] [ raw "Next" ] ] ] ] ] ]
2 changes: 1 addition & 1 deletion src/Microsoft.eShopWeb.Web/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Program =
endpoints
[ get "/" HomePage.handler

get "/basket" BasketPage.handler
post "/basket" BasketPage.handler

//get "/catalogItems/{id:guid}" (Request.mapRoute getCatalogItemByIdFromRoute responseHandler)
]
Expand Down

0 comments on commit bfadaa9

Please sign in to comment.