In Elm Pages update `Shared.Model` from within the `Page.X.update`

In Elm Pages, is there a way to update Shared.Model from within the update function of a page?

My use case is following. I’m implementing a PKCE flow to obtain access token from GitLab. The token is received inside a Page.Index.Msg where it’s also immediately used for the page specific HTTP request. But it also needs to be stored in Shared.Model so other pages can use it.

I guess I need to somehow pass a SharedMsg from a page module to the Shared module, but I’m not sure how. Or maybe there is a different way?

Alternatively I thought about moving the whole PKCE logic to the Shared module. But then I need to somehow notify the current page once the token is obtained, so it can initiate the request.

Probably I can implement it through a pair of ports - an outgoing port in Page.Index and an incoming port in Shared. The token is already sent through a port to be stored in localStorage so it wouldn’t be so much more code. But that would require encoding and decoding the same value and generally seems clunky.

The question seems similar to Modifying "parent state" from child page in an elm-spa-example-like architecture but I couldn’t apply any of the suggestions there to my case.

Maybe @dillonkearns can help?

Hello @lazurski, I think if you use Page.buildWithSharedState in Page.Index that will give you what you’re looking for. You can run elm-pages docs to see all the signatures for that code (since the Page.elm module is generated). Hope that helps!

1 Like

Yes, I believe so. Thank you! Elm Pages are really great.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.