Hi, I’m curious what’s the best way to model my data structure so it’s impossible to run into issues (make impossible states impossible) and is also performant.
I have an SVG with points { x: Int, y: Int }
on it. User can click and drag those points around. I need to x
and y
position as user drags it’s mouse. Currently, it’s implemented as a list of points and selected point index.
Here’s the full working example https://ellie-app.com/3krKBgDJRKRa1
When user clicks I simply save index of that clicked point
SelectPoint selectedIndex ->
{ model | maybeSelectedIndex = Just selectedIndex }
When user releases mouse btn, I just set it to Nothing
.
The update function is getting quite complex to my taste
MovePoint mouseMoveData ->
let
updatedPoints =
case model.maybeSelectedIndex of
Just selectedPointIndex ->
model.points
|> List.indexedMap
(\index point ->
if index == selectedPointIndex then
{ x = mouseMoveData.offsetX
, y = mouseMoveData.offsetY
}
else
point
)
Nothing ->
model.points
in
{ model | points = updatedPoints }
Is there a better data structure/way to model this?
Thank you