Skip to content

Commit

Permalink
refactor: clean code.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Nov 27, 2024
1 parent 7c7c604 commit 7a50599
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
50 changes: 39 additions & 11 deletions src/Data/Object/Component.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Data.Object.Component exposing
, decodeComponentItem
, decodeList
, encodeComponentItem
, expandComponentItems
, expandProcessItems
, findById
, idFromString
, idToString
Expand Down Expand Up @@ -82,17 +84,30 @@ componentItemToString components processes componentItem =
)


processItemToString : List Process -> ProcessItem -> Result String String
processItemToString processes processItem =
processItem.processId
|> Process.findById processes
|> Result.map
(\process ->
String.fromFloat (amountToFloat processItem.amount)
++ process.unit
++ " "
++ process.displayName
)
expandComponentItems :
{ a | components : List Component, processes : List Process }
-> List ComponentItem
-> Result String (List ( Quantity, Component, List ( Amount, Process ) ))
expandComponentItems { components, processes } =
List.map
(\componentItem ->
components
|> findById componentItem.id
|> Result.andThen
(\component ->
component.processes
|> expandProcessItems processes
|> Result.map (\processItems -> ( componentItem.quantity, component, processItems ))
)
)
>> RE.combine


expandProcessItems : List Process -> List ProcessItem -> Result String (List ( Amount, Process ))
expandProcessItems processes =
List.map (\{ amount, processId } -> ( amount, processId ))
>> List.map (RE.combineMapSecond (Process.findById processes))
>> RE.combine


decode : Decoder Component
Expand Down Expand Up @@ -147,6 +162,19 @@ idToString (Id uuid) =
Uuid.toString uuid


processItemToString : List Process -> ProcessItem -> Result String String
processItemToString processes processItem =
processItem.processId
|> Process.findById processes
|> Result.map
(\process ->
String.fromFloat (amountToFloat processItem.amount)
++ process.unit
++ " "
++ process.displayName
)


quantityFromInt : Int -> Quantity
quantityFromInt int =
Quantity int
Expand Down
29 changes: 1 addition & 28 deletions src/Data/Object/Simulator.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module Data.Object.Simulator exposing
, availableComponents
, compute
, emptyResults
, expandProcessItems
, expandProcesses
, extractImpacts
, extractItems
, extractMass
Expand All @@ -14,7 +12,7 @@ module Data.Object.Simulator exposing
import Data.Impact as Impact exposing (Impacts, noStepsImpacts)
import Data.Impact.Definition as Definition
import Data.Object.Component as Component exposing (Component, ComponentItem, ProcessItem)
import Data.Object.Process as Process exposing (Process)
import Data.Object.Process as Process
import Data.Object.Query exposing (Query)
import Mass exposing (Mass)
import Quantity
Expand Down Expand Up @@ -113,31 +111,6 @@ emptyResults =
}


expandProcessItems : Db -> Query -> Result String (List ( Component.Quantity, Component, List ( Component.Amount, Process ) ))
expandProcessItems db =
.components
>> List.map
(\componentItem ->
db.object.components
|> Component.findById componentItem.id
|> Result.andThen
(\component ->
component.processes
|> expandProcesses db
|> Result.map (\processes -> ( componentItem.quantity, component, processes ))
)
)
>> RE.combine


expandProcesses : Db -> List ProcessItem -> Result String (List ( Component.Amount, Process ))
expandProcesses db processes =
processes
|> List.map (\{ amount, processId } -> ( amount, processId ))
|> List.map (RE.combineMapSecond (Process.findById db.object.processes))
|> RE.combine


extractImpacts : Results -> Impacts
extractImpacts (Results { impacts }) =
impacts
Expand Down
5 changes: 2 additions & 3 deletions src/Page/Explore/ObjectComponents.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Page.Explore.ObjectComponents exposing (table)

import Data.Dataset as Dataset
import Data.Object.Component as ObjectComponent
import Data.Object.Simulator as Simulator
import Data.Scope exposing (Scope)
import Html exposing (..)
import Html.Attributes exposing (..)
Expand Down Expand Up @@ -39,7 +38,7 @@ table db { detailed, scope } =
, toValue =
Table.StringValue <|
\{ processes } ->
case Simulator.expandProcesses db processes of
case ObjectComponent.expandProcessItems db.object.processes processes of
Err _ ->
""

Expand All @@ -55,7 +54,7 @@ table db { detailed, scope } =
|> String.join ", "
, toCell =
\{ processes } ->
case Simulator.expandProcesses db processes of
case ObjectComponent.expandProcessItems db.object.processes processes of
Err err ->
Alert.simple
{ close = Nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Page/Object.elm
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ componentListView db { detailedComponents, impact, results } query =
div [ class "card-body" ] [ text "Aucun élément." ]

else
case Simulator.expandProcessItems db query of
case Component.expandComponentItems db.object query.components of
Err error ->
Alert.simple
{ close = Nothing
Expand Down

0 comments on commit 7a50599

Please sign in to comment.