Skip to content

Commit

Permalink
feat: update component quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
vjousse committed Nov 18, 2024
1 parent 1c19e84 commit 06c8d2c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 98 deletions.
32 changes: 22 additions & 10 deletions src/Data/Object/Query.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module Data.Object.Query exposing
, defaultItem
, encode
, parseBase64Query
, quantity
, quantityToInt
, removeItem
, toString
-- , updateItem
, updateItem
)

import Base64
Expand Down Expand Up @@ -59,6 +60,11 @@ amountToFloat (Amount float) =
float


quantity : Int -> Quantity
quantity int =
Quantity int


quantityToInt : Quantity -> Int
quantityToInt (Quantity int) =
int
Expand Down Expand Up @@ -140,15 +146,21 @@ removeItem name ({ items } as query) =
{ query | items = items |> List.filter (.name >> (/=) name) }



--
--
-- updateItem : Item -> Query -> Query
-- updateItem newItem query =
-- -- FIX: implement it for components
-- query
--
--
updateItem : Item -> Query -> Query
updateItem newItem query =
let
items =
query.items
|> List.map
(\item ->
if item.name == newItem.name then
newItem

else
item
)
in
{ query | items = items }


toString : List Process -> Query -> Result String String
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Object/Simulator.elm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Results
availableComponents : Db -> Query -> List Item
availableComponents { object } query =
let
-- For now, consider that components are unique by name, we should
-- FIX: For now, consider that components are unique by name, we should
-- replace it with ids later on
usedNames =
List.map .name query.items
Expand Down
106 changes: 19 additions & 87 deletions src/Page/Object.elm
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ type Msg
| SwitchImpactsTab ImpactTabs.Tab
| ToggleComparedSimulation Bookmark Bool
| UpdateBookmarkName String



-- | UpdateItem Query.Item
| UpdateItem Query.Item


init : Scope -> Definition.Trigram -> Maybe Query -> Session -> ( Model, Session, Cmd Msg )
Expand Down Expand Up @@ -271,7 +268,6 @@ update ({ navKey } as session) msg model =
( OnAutocompleteSelect, _ ) ->
( model, session, Cmd.none )

-- FIX: select component
( OnAutocompleteSelectComponent, AddComponentModal autocompleteState ) ->
( model, session, Cmd.none )
|> selectComponent query autocompleteState
Expand Down Expand Up @@ -385,12 +381,9 @@ update ({ navKey } as session) msg model =
( UpdateBookmarkName newName, _ ) ->
( { model | bookmarkName = newName }, session, Cmd.none )



-- FIX: add it back for components
-- ( UpdateItem item, _ ) ->
-- ( model, session, Cmd.none )
-- |> updateQuery (Query.updateItem item query)
( UpdateItem item, _ ) ->
( model, session, Cmd.none )
|> updateQuery (Query.updateItem item query)


commandsForNoModal : Modal -> Cmd Msg
Expand Down Expand Up @@ -562,7 +555,7 @@ itemListView db selectedImpact results query =


itemView : Definition -> ( Query.Quantity, String, List ( Query.Amount, Process ) ) -> Results -> Html Msg
itemView selectedImpact ( quantity, name, _ ) itemResults =
itemView selectedImpact ( quantity, name, processes ) itemResults =
tr []
[ td [ class "ps-3 align-middle" ]
[ div [ class "input-group", style "min-width" "180px" ]
Expand All @@ -571,19 +564,20 @@ itemView selectedImpact ( quantity, name, _ ) itemResults =
, class "form-control text-end"
, quantity |> Query.quantityToInt |> String.fromInt |> value
, step "1"

-- FIX: add it back for components
-- , onInput <|
-- \str ->
-- case String.toFloat str of
-- Just float ->
-- UpdateItem
-- { amount = Query.amount float
-- , processId = process.id
-- }
--
-- Nothing ->
-- NoOp
, onInput <|
\str ->
case String.toInt str of
-- FIX: don't update components based on their name
-- swith to components ids as soon as they are implemented
Just int ->
UpdateItem
{ name = name
, quantity = Query.quantity int
, processes = processes |> List.map (\( amount, process ) -> { amount = amount, processId = process.id })
}

Nothing ->
NoOp
]
[]
]
Expand All @@ -597,74 +591,12 @@ itemView selectedImpact ( quantity, name, _ ) itemResults =
|> Format.formatImpact selectedImpact
]
, td [ class "pe-3 align-middle text-nowrap" ]
-- FIX: add it back for components
[ button [ class "btn btn-outline-secondary", onClick (RemoveComponent name) ]
[ Icon.trash ]
]
]



-- processView : Definition -> ( Query.Amount, Process ) -> Results -> Html Msg
-- processView selectedImpact ( amount, process ) itemResults =
-- tr []
-- [ td [ class "ps-3 align-middle" ]
-- [ div [ class "input-group", style "min-width" "180px" ]
-- [ input
-- [ type_ "number"
-- , class "form-control text-end"
-- , amount |> Query.amountToFloat |> String.fromFloat |> value
-- , step <|
-- case process.unit of
-- "kg" ->
-- "0.01"
--
-- "m3" ->
-- "0.00001"
--
-- _ ->
-- "1"
--
-- -- FIX: add it back for components
-- -- , onInput <|
-- -- \str ->
-- -- case String.toFloat str of
-- -- Just float ->
-- -- UpdateItem
-- -- { amount = Query.amount float
-- -- , processId = process.id
-- -- }
-- --
-- -- Nothing ->
-- -- NoOp
-- ]
-- []
-- , span
-- [ class "input-group-text justify-content-center fs-8"
-- , style "width" "38px"
-- ]
-- [ text process.unit ]
-- ]
-- ]
-- , td [ class "align-middle text-truncate w-100" ]
-- [ text process.displayName ]
-- , td [ class "align-middle text-end" ]
-- [ Format.density process ]
-- , td [ class "text-end align-middle text-nowrap" ]
-- [ Format.kg <| Simulator.extractMass itemResults ]
-- , td [ class "text-end align-middle text-nowrap" ]
-- [ Simulator.extractImpacts itemResults
-- |> Format.formatImpact selectedImpact
-- ]
-- , td [ class "pe-3 align-middle text-nowrap" ]
-- -- FIX: add it back for components
-- -- [ button [ class "btn btn-outline-secondary", onClick (RemoveItem process.id) ]
-- [ button [ class "btn btn-outline-secondary" ]
-- [ Icon.trash ]
-- ]
-- ]


view : Session -> Model -> ( String, List (Html Msg) )
view session model =
( "Simulateur"
Expand Down

0 comments on commit 06c8d2c

Please sign in to comment.