RaspberryPiFoundation / editor-ui

Code Editor web component
https://editor-static.raspberrypi.org
Apache License 2.0
37 stars 8 forks source link

Fix initial value of `user` in `WebComponentLoader` #1021

Closed floehopper closed 1 month ago

floehopper commented 1 month ago

Previously, even if the authKey property was provided and a logged-in user was stored in localStorage, on first render the value of user was null. This was because the localStorageUserMiddleware which dispatches setUser to store the user in Redux had not yet been triggered.

This meant that previously if we tried to use the web component to view a project that the logged-in user owned, the request to editor-api to load the project would respond with a 403 Forbidden and the project would not load.

I'm fairly (but not completely) sure that the assertions in the "with user set in local storage" group of examples in src/containers/WebComponentLoader.test.js were incorrect, i.e. if the authKey is set and a user is present in localStorage we ought to load the project based on the fact the user is logged in. I have fixed these assertions.

The null value of the user property in the call to useProjectPersistence in the "When no user is in state with no user in local storage" group of examples in src/containers/WebComponentLoader.test.js is due to the return value for localStorage.getItem(authKey) and not the fallback in the new ternary expression in WebComponentLoader. I think this is fine.

I'm not sure whether my fix is definitely correct/idiomatic - maybe I should be using a useState hook for the localStorageUser in combination with a useEffect hook...?

Also stepping back a bit, it's not entirely clear to me that dispatching setUser in localStorageUserMiddleware is definitely the most sensible place to do that.

github-actions[bot] commented 1 month ago
github-actions[bot] commented 1 month ago