You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the sake of this discussion I'll reference moneyphp to illustrate my dilemma. This is essentially an immutable value object used to make working with money simpler.
If we return a Money instance from a controller using return Inertia::render('Home', [ 'balance' => Money::ZAR(350) ]);, balance will be serialized as '{"amount":"350","currency":"ZAR"}'.
This works fine, except I now need to convert cents -> whole currency units in javascript. Ideally we would like to have a corresponding immutable money value object to work with in React.
Note: Our Money object in typescript is read only - so works fine with React's reactivity.
The current solution
To have a full end-to-end solution, we need to:
be able to automatically instantiate a Money object when receiving data from
the network.
work with Money objects in Forms.
correctly store and rehydrate Money objects in history state.
In order to achieve this we currently need to wrap the relevant inertia hooks:
/** * Wraps `usePage` to automatically insert a `Money` instance when `\Money\Money` is returned from a controller. */exportfunctionuseMoneyPage<TPagePropsextendsPageProps=PageProps>(): Page<TPageProps>{constpage=usePage<TPageProps>()// Recursively parse page props to ensure anything `Money` like gets instantiated as `Money`.constdeserializedProps=deserialize<TPageProps>(page.props)return{ ...page,props: deserializedProps}asPage<TPageProps>}
Similarly, we would need to wrap the useForm and useRemember hooks. However, these two hooks are currently very unwieldy to work with, as the object is deep cloned when adding it to the history state. This makes deserialization more tricky. Although this should be fixed when the cloneserializable is released, as the serialisation will use Money.toJSON().
Proposed improvements
Being able to pass custom replacer/reviver functions to cloneserializable would remove the need to wrap the useForm and useRemember hooks.
I'm not sure if this would necessarily fix how usePage props deserialization works, but I assume the same reviver function could be used for that too.
I am happy to try figure it out, and submit a PR. But thought I'd ask here first in case something similar is already in the works for v2 or if it's even likely to be included in Inertia.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The problem
There are various places that Inertia needs to serialize/deserialize data in order to send it over the network, or save it to history state.
In the latest version (v1.2.0 at the time of writing) Inertia does nothing to data being pushed to state. This has since been improved with the cloneserializable function, but this is unreleased.
For the sake of this discussion I'll reference moneyphp to illustrate my dilemma. This is essentially an immutable value object used to make working with money simpler.
If we return a
Money
instance from a controller usingreturn Inertia::render('Home', [ 'balance' => Money::ZAR(350) ]);
, balance will be serialized as'{"amount":"350","currency":"ZAR"}'
.This works fine, except I now need to convert cents -> whole currency units in javascript. Ideally we would like to have a corresponding immutable money value object to work with in React.
The current solution
To have a full end-to-end solution, we need to:
the network.
In order to achieve this we currently need to wrap the relevant inertia hooks:
Similarly, we would need to wrap the
useForm
anduseRemember
hooks. However, these two hooks are currently very unwieldy to work with, as the object is deep cloned when adding it to the history state. This makes deserialization more tricky. Although this should be fixed when thecloneserializable
is released, as the serialisation will useMoney.toJSON()
.Proposed improvements
Being able to pass custom replacer/reviver functions to
cloneserializable
would remove the need to wrap theuseForm
anduseRemember
hooks.I'm not sure if this would necessarily fix how
usePage
props deserialization works, but I assume the same reviver function could be used for that too.I am happy to try figure it out, and submit a PR. But thought I'd ask here first in case something similar is already in the works for v2 or if it's even likely to be included in Inertia.
Beta Was this translation helpful? Give feedback.
All reactions