@molecule/app-react 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -3
- package/dist/hooks/useProfileForm.d.ts +70 -0
- package/dist/hooks/useProfileForm.d.ts.map +1 -0
- package/dist/hooks/useProfileForm.js +188 -0
- package/dist/hooks/useProfileForm.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +45 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-22T08:55:49.342Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-react
|
|
@@ -15,12 +15,38 @@ core interfaces (auth, i18n, theme, routing, state, http, storage, logger,
|
|
|
15
15
|
chat, workspace, editor, preview), so framework-agnostic providers plug
|
|
16
16
|
into React idioms.
|
|
17
17
|
|
|
18
|
+
**Editable account/profile forms:** `useProfileForm` composes the bonded form
|
|
19
|
+
and auth clients. It hydrates asynchronous user data without erasing dirty
|
|
20
|
+
fields, snapshots controlled values on native form submission, submits only
|
|
21
|
+
changed fields through an app-supplied adapter, and persists/publishes the
|
|
22
|
+
server's saved user. Use its `onSaved` callback to update other query caches
|
|
23
|
+
from that response before navigating. Configure the endpoint, method and JSON
|
|
24
|
+
envelope once in `AuthClientConfig`; no page-local session store is needed.
|
|
25
|
+
|
|
18
26
|
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
19
27
|
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
20
28
|
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
21
29
|
|
|
22
30
|
## Quick Start
|
|
23
31
|
|
|
32
|
+
```tsx
|
|
33
|
+
const profile = useProfileForm<User, { name: string; email: string }>({
|
|
34
|
+
defaultValues: { name: '', email: '' },
|
|
35
|
+
getIdentity: (user) => user.id,
|
|
36
|
+
toValues: (user) => ({ name: user.name ?? '', email: user.email ?? '' }),
|
|
37
|
+
toPatch: (changes) => changes,
|
|
38
|
+
onSaved: async (user) => {
|
|
39
|
+
// Set/invalidate the app's profile query keys using the saved response.
|
|
40
|
+
// Navigate only after those writes complete.
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
<form {...profile.formProps}>
|
|
44
|
+
<input {...profile.register('name')} />
|
|
45
|
+
<input {...profile.register('email', { email: true })} type="email" />
|
|
46
|
+
<button type="submit" disabled={!profile.ready || profile.isSubmitting}>Save</button>
|
|
47
|
+
</form>
|
|
48
|
+
```
|
|
49
|
+
|
|
24
50
|
```tsx
|
|
25
51
|
import { MoleculeProvider, useAuth, useTheme, useTranslation } from '@molecule/app-react'
|
|
26
52
|
import { provider as stateProvider } from '@molecule/app-state-zustand'
|
|
@@ -215,6 +241,9 @@ interface AuthClient<T = UserProfile, TRegister extends RegisterExtraShape<TRegi
|
|
|
215
241
|
confirmPasswordReset(data: PasswordResetConfirm): Promise<void>;
|
|
216
242
|
/**
|
|
217
243
|
* Updates the current user's profile.
|
|
244
|
+
* Returns the saved response. If the session is explicitly replaced or ended
|
|
245
|
+
* while the request is pending, that response does not replace the current
|
|
246
|
+
* session's user or persisted cache.
|
|
218
247
|
*/
|
|
219
248
|
updateProfile(data: Partial<T>): Promise<T>;
|
|
220
249
|
/**
|
|
@@ -281,8 +310,6 @@ interface AuthClient<T = UserProfile, TRegister extends RegisterExtraShape<TRegi
|
|
|
281
310
|
* HTTP client bonded to that provider sends first-paint requests unauthenticated.
|
|
282
311
|
*
|
|
283
312
|
* A provider whose restore sends a request THROUGH the gated HTTP client must send that
|
|
284
|
-
* request with `skipAuthGate: true` (`RequestConfig` in `@molecule/app-http`): the gate
|
|
285
|
-
* waits on this promise, so holding the restore's own request makes the gate wait on
|
|
286
313
|
// … truncated — the full declaration is in node_modules/@molecule/app-react/dist
|
|
287
314
|
```
|
|
288
315
|
|
|
@@ -940,6 +967,71 @@ interface UsePlatformResult {
|
|
|
940
967
|
}
|
|
941
968
|
```
|
|
942
969
|
|
|
970
|
+
#### `UseProfileFormOptions`
|
|
971
|
+
|
|
972
|
+
Configuration for a profile form; transport stays in the auth bond.
|
|
973
|
+
|
|
974
|
+
```typescript
|
|
975
|
+
interface UseProfileFormOptions<TUser, TValues extends Record<string, unknown>> extends Omit<
|
|
976
|
+
UseFormOptions<TValues>,
|
|
977
|
+
'defaultValues'
|
|
978
|
+
> {
|
|
979
|
+
/** Complete controlled-field defaults, including blank write-only fields. */
|
|
980
|
+
defaultValues: TValues
|
|
981
|
+
/** Stable account identity; changing accounts discards the previous account's edits. */
|
|
982
|
+
getIdentity: (user: TUser) => string
|
|
983
|
+
/** Maps the current/saved user into controlled values, e.g. nullable text to ''. */
|
|
984
|
+
toValues: (user: TUser) => TValues
|
|
985
|
+
/**
|
|
986
|
+
* Maps ONLY changed fields into an API patch. Omit blank optional credentials
|
|
987
|
+
* here; preserve undefined/absent fields rather than turning them into null.
|
|
988
|
+
* The second argument is the complete value snapshot taken at submit time.
|
|
989
|
+
*/
|
|
990
|
+
toPatch: (changes: Partial<TValues>, values: Readonly<TValues>) => Partial<TUser>
|
|
991
|
+
/**
|
|
992
|
+
* Propagates the server's saved user to other caches, then navigates if needed.
|
|
993
|
+
* Awaited before submission finishes. The auth client already persists and
|
|
994
|
+
* publishes the saved user; never seed caches from the submitted patch.
|
|
995
|
+
*/
|
|
996
|
+
onSaved?: (user: TUser) => void | Promise<void>
|
|
997
|
+
}
|
|
998
|
+
```
|
|
999
|
+
|
|
1000
|
+
#### `UseProfileFormResult`
|
|
1001
|
+
|
|
1002
|
+
Controlled profile form state and native form bindings.
|
|
1003
|
+
|
|
1004
|
+
```typescript
|
|
1005
|
+
interface UseProfileFormResult<TUser, TValues extends Record<string, unknown>> extends Omit<
|
|
1006
|
+
UseFormResult<TValues>,
|
|
1007
|
+
'register' | 'handleSubmit' | 'reset'
|
|
1008
|
+
> {
|
|
1009
|
+
/** The shared auth user's current profile. */
|
|
1010
|
+
user: TUser | null
|
|
1011
|
+
/** A user has been loaded; disable submission until this is true. */
|
|
1012
|
+
ready: boolean
|
|
1013
|
+
/**
|
|
1014
|
+
* The latest save/propagation failure. Rejected saves retain edits; a failed
|
|
1015
|
+
* onSaved callback can be retried by submit without repeating the mutation.
|
|
1016
|
+
*/
|
|
1017
|
+
error: unknown
|
|
1018
|
+
/** Native input/textarea bindings with the field's concrete value type. */
|
|
1019
|
+
register: <K extends keyof TValues>(
|
|
1020
|
+
name: K,
|
|
1021
|
+
options?: Parameters<UseFormResult<TValues>['register']>[1],
|
|
1022
|
+
) => Omit<FieldRegistration, 'value'> & { value: TValues[K] }
|
|
1023
|
+
/** Bind to the native form, with a type="submit" button inside it. */
|
|
1024
|
+
formProps: { onSubmit: (event: React.FormEvent<HTMLFormElement>) => void }
|
|
1025
|
+
/**
|
|
1026
|
+
* Validates and saves one submit-time snapshot. Concurrent submits share the
|
|
1027
|
+
* same promise. Resolves undefined on validation, save or propagation failure.
|
|
1028
|
+
*/
|
|
1029
|
+
submit: () => Promise<TUser | undefined>
|
|
1030
|
+
/** Discards edits and restores the newest loaded/saved profile baseline. */
|
|
1031
|
+
reset: () => void
|
|
1032
|
+
}
|
|
1033
|
+
```
|
|
1034
|
+
|
|
943
1035
|
#### `UsePromiseState`
|
|
944
1036
|
|
|
945
1037
|
Extended promise state with actions.
|
|
@@ -2239,6 +2331,27 @@ function usePreview(): UsePreviewResult
|
|
|
2239
2331
|
|
|
2240
2332
|
**Returns:** Preview state and controls: state (url, isLoading, device, error, isConnected), setUrl, refresh, setDevice, and openExternal.
|
|
2241
2333
|
|
|
2334
|
+
#### `useProfileForm(options)`
|
|
2335
|
+
|
|
2336
|
+
Owns load → edit → submit → publish for the signed-in user's profile.
|
|
2337
|
+
|
|
2338
|
+
Same-account background updates refresh untouched fields but preserve dirty
|
|
2339
|
+
controls. Submission snapshots the registered values before asynchronous
|
|
2340
|
+
validation, sends only changed fields through `toPatch`, and uses the auth
|
|
2341
|
+
client's configured updateProfile transport. The saved server response becomes
|
|
2342
|
+
the clean baseline; edits made while saving remain dirty. No request is made
|
|
2343
|
+
for a patch with no fields. A failed request retains the edits and shared user.
|
|
2344
|
+
|
|
2345
|
+
```typescript
|
|
2346
|
+
function useProfileForm(
|
|
2347
|
+
options: UseProfileFormOptions<TUser, TValues>,
|
|
2348
|
+
): UseProfileFormResult<TUser, TValues>
|
|
2349
|
+
```
|
|
2350
|
+
|
|
2351
|
+
- `options` — Controlled defaults, identity and wire adapters, optional cache/navigation callback.
|
|
2352
|
+
|
|
2353
|
+
**Returns:** Form state, typed control bindings, native submit binding and save error.
|
|
2354
|
+
|
|
2242
2355
|
#### `usePromise(asyncFn)`
|
|
2243
2356
|
|
|
2244
2357
|
Hook that wraps an async function with state tracking.
|
|
@@ -3028,6 +3141,23 @@ Peer dependencies:
|
|
|
3028
3141
|
- `@molecule/app-version`
|
|
3029
3142
|
- `react`
|
|
3030
3143
|
|
|
3144
|
+
- `useProfileForm` needs `AuthProvider` (or `MoleculeProvider auth={client}`)
|
|
3145
|
+
and uses the bonded forms provider, just like `useForm`. `defaultValues`
|
|
3146
|
+
and `toValues` must cover every registered control; map nullable text to
|
|
3147
|
+
`''` there. Add optional write-only controls such as `password: ''` to both,
|
|
3148
|
+
then omit a blank password in `toPatch`: `({ password, ...changes }) =>
|
|
3149
|
+
({ ...changes, ...(password?.trim() ? { password } : {}) })`. `changes`
|
|
3150
|
+
contains only edited fields, so do not fill omitted fields with null or
|
|
3151
|
+
copy every property of the auth user into the request. `reset()` discards
|
|
3152
|
+
edits to the newest baseline. A changed `getIdentity(user)` clears the old
|
|
3153
|
+
account's edits. Field comparisons are shallow, matching form providers.
|
|
3154
|
+
A submission whose account or auth client changes, or whose form unmounts,
|
|
3155
|
+
during asynchronous validation is discarded before any mutation or saved
|
|
3156
|
+
callback. Same-account background hydration does not discard the submit.
|
|
3157
|
+
The hook captures save failures in `error`; render that error with the
|
|
3158
|
+
app's normal API-error presentation. Use native `<form>` submit semantics,
|
|
3159
|
+
and pass the typed `register()` props through custom controls to their
|
|
3160
|
+
actual input/textarea elements.
|
|
3031
3161
|
- **Every hook throws when its provider is not mounted.** `MoleculeProvider` wires ONLY the
|
|
3032
3162
|
services you pass as props — it is a convenience wrapper, not a default registry. The map:
|
|
3033
3163
|
`useAuth`→`auth`, `useTranslation`/`useT`→`i18n`, `useTheme`→`theme`, `useRouter`→`router`,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile editing over the app's bonded form and auth clients.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { FieldRegistration } from '@molecule/app-forms';
|
|
7
|
+
import { type UseFormOptions, type UseFormResult } from './useForm.js';
|
|
8
|
+
/** Configuration for a profile form; transport stays in the auth bond. */
|
|
9
|
+
export interface UseProfileFormOptions<TUser, TValues extends Record<string, unknown>> extends Omit<UseFormOptions<TValues>, 'defaultValues'> {
|
|
10
|
+
/** Complete controlled-field defaults, including blank write-only fields. */
|
|
11
|
+
defaultValues: TValues;
|
|
12
|
+
/** Stable account identity; changing accounts discards the previous account's edits. */
|
|
13
|
+
getIdentity: (user: TUser) => string;
|
|
14
|
+
/** Maps the current/saved user into controlled values, e.g. nullable text to ''. */
|
|
15
|
+
toValues: (user: TUser) => TValues;
|
|
16
|
+
/**
|
|
17
|
+
* Maps ONLY changed fields into an API patch. Omit blank optional credentials
|
|
18
|
+
* here; preserve undefined/absent fields rather than turning them into null.
|
|
19
|
+
* The second argument is the complete value snapshot taken at submit time.
|
|
20
|
+
*/
|
|
21
|
+
toPatch: (changes: Partial<TValues>, values: Readonly<TValues>) => Partial<TUser>;
|
|
22
|
+
/**
|
|
23
|
+
* Propagates the server's saved user to other caches, then navigates if needed.
|
|
24
|
+
* Awaited before submission finishes. The auth client already persists and
|
|
25
|
+
* publishes the saved user; never seed caches from the submitted patch.
|
|
26
|
+
*/
|
|
27
|
+
onSaved?: (user: TUser) => void | Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
/** Controlled profile form state and native form bindings. */
|
|
30
|
+
export interface UseProfileFormResult<TUser, TValues extends Record<string, unknown>> extends Omit<UseFormResult<TValues>, 'register' | 'handleSubmit' | 'reset'> {
|
|
31
|
+
/** The shared auth user's current profile. */
|
|
32
|
+
user: TUser | null;
|
|
33
|
+
/** A user has been loaded; disable submission until this is true. */
|
|
34
|
+
ready: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The latest save/propagation failure. Rejected saves retain edits; a failed
|
|
37
|
+
* onSaved callback can be retried by submit without repeating the mutation.
|
|
38
|
+
*/
|
|
39
|
+
error: unknown;
|
|
40
|
+
/** Native input/textarea bindings with the field's concrete value type. */
|
|
41
|
+
register: <K extends keyof TValues>(name: K, options?: Parameters<UseFormResult<TValues>['register']>[1]) => Omit<FieldRegistration, 'value'> & {
|
|
42
|
+
value: TValues[K];
|
|
43
|
+
};
|
|
44
|
+
/** Bind to the native form, with a type="submit" button inside it. */
|
|
45
|
+
formProps: {
|
|
46
|
+
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Validates and saves one submit-time snapshot. Concurrent submits share the
|
|
50
|
+
* same promise. Resolves undefined on validation, save or propagation failure.
|
|
51
|
+
*/
|
|
52
|
+
submit: () => Promise<TUser | undefined>;
|
|
53
|
+
/** Discards edits and restores the newest loaded/saved profile baseline. */
|
|
54
|
+
reset: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Owns load → edit → submit → publish for the signed-in user's profile.
|
|
58
|
+
*
|
|
59
|
+
* Same-account background updates refresh untouched fields but preserve dirty
|
|
60
|
+
* controls. Submission snapshots the registered values before asynchronous
|
|
61
|
+
* validation, sends only changed fields through `toPatch`, and uses the auth
|
|
62
|
+
* client's configured updateProfile transport. The saved server response becomes
|
|
63
|
+
* the clean baseline; edits made while saving remain dirty. No request is made
|
|
64
|
+
* for a patch with no fields. A failed request retains the edits and shared user.
|
|
65
|
+
*
|
|
66
|
+
* @param options - Controlled defaults, identity and wire adapters, optional cache/navigation callback.
|
|
67
|
+
* @returns Form state, typed control bindings, native submit binding and save error.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useProfileForm<TUser, TValues extends Record<string, unknown>>(options: UseProfileFormOptions<TUser, TValues>): UseProfileFormResult<TUser, TValues>;
|
|
70
|
+
//# sourceMappingURL=useProfileForm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProfileForm.d.ts","sourceRoot":"","sources":["../../src/hooks/useProfileForm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAG5D,OAAO,EAAW,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAA;AAE/E,0EAA0E;AAC1E,MAAM,WAAW,qBAAqB,CAAC,KAAK,EAAE,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAE,SAAQ,IAAI,CACjG,cAAc,CAAC,OAAO,CAAC,EACvB,eAAe,CAChB;IACC,6EAA6E;IAC7E,aAAa,EAAE,OAAO,CAAA;IACtB,wFAAwF;IACxF,WAAW,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,CAAA;IACpC,oFAAoF;IACpF,QAAQ,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,CAAA;IAClC;;;;OAIG;IACH,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,C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E,OAAO,EAAE,qBAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,GAC7C,oBAAoB,CAAC,KAAK,EAAE,OAAO,CAAC,CAsLtC"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile editing over the app's bonded form and auth clients.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { useCallback, useInsertionEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
7
|
+
import { useAuth, useAuthClient } from './useAuth.js';
|
|
8
|
+
import { useForm } from './useForm.js';
|
|
9
|
+
/**
|
|
10
|
+
* Owns load → edit → submit → publish for the signed-in user's profile.
|
|
11
|
+
*
|
|
12
|
+
* Same-account background updates refresh untouched fields but preserve dirty
|
|
13
|
+
* controls. Submission snapshots the registered values before asynchronous
|
|
14
|
+
* validation, sends only changed fields through `toPatch`, and uses the auth
|
|
15
|
+
* client's configured updateProfile transport. The saved server response becomes
|
|
16
|
+
* the clean baseline; edits made while saving remain dirty. No request is made
|
|
17
|
+
* for a patch with no fields. A failed request retains the edits and shared user.
|
|
18
|
+
*
|
|
19
|
+
* @param options - Controlled defaults, identity and wire adapters, optional cache/navigation callback.
|
|
20
|
+
* @returns Form state, typed control bindings, native submit binding and save error.
|
|
21
|
+
*/
|
|
22
|
+
export function useProfileForm(options) {
|
|
23
|
+
const { defaultValues, getIdentity: _identity, toValues: _values, toPatch: _patch, onSaved: _saved, ...formOptions } = options;
|
|
24
|
+
const form = useForm({ ...formOptions, defaultValues });
|
|
25
|
+
const client = useAuthClient();
|
|
26
|
+
const { user } = useAuth();
|
|
27
|
+
const [error, setError] = useState();
|
|
28
|
+
const [isSaving, setIsSaving] = useState(false);
|
|
29
|
+
const optionsRef = useRef(options);
|
|
30
|
+
const committedClient = useRef(client);
|
|
31
|
+
const baseline = useRef({ ...defaultValues });
|
|
32
|
+
const owner = useRef(null);
|
|
33
|
+
const activeSave = useRef(null);
|
|
34
|
+
const pendingPropagation = useRef(undefined);
|
|
35
|
+
const mounted = useRef(false);
|
|
36
|
+
// Actions use committed configuration, never an abandoned concurrent render.
|
|
37
|
+
useInsertionEffect(() => {
|
|
38
|
+
optionsRef.current = options;
|
|
39
|
+
committedClient.current = client;
|
|
40
|
+
}, [options, client]);
|
|
41
|
+
useLayoutEffect(() => {
|
|
42
|
+
mounted.current = true;
|
|
43
|
+
return () => {
|
|
44
|
+
mounted.current = false;
|
|
45
|
+
};
|
|
46
|
+
}, []);
|
|
47
|
+
const readValues = useCallback(() => {
|
|
48
|
+
const values = { ...baseline.current };
|
|
49
|
+
for (const key of Object.keys(values))
|
|
50
|
+
values[key] = form.getValue(key);
|
|
51
|
+
return values;
|
|
52
|
+
}, [form.getValue]);
|
|
53
|
+
const rebase = useCallback((next, preserveAgainst) => {
|
|
54
|
+
const current = readValues();
|
|
55
|
+
baseline.current = { ...next };
|
|
56
|
+
form.reset(next);
|
|
57
|
+
if (preserveAgainst) {
|
|
58
|
+
for (const key of Object.keys(current)) {
|
|
59
|
+
if (!Object.is(current[key], preserveAgainst[key]))
|
|
60
|
+
form.setValue(key, current[key]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}, [readValues, form.reset, form.setValue]);
|
|
64
|
+
useLayoutEffect(() => {
|
|
65
|
+
const config = optionsRef.current;
|
|
66
|
+
if (!user) {
|
|
67
|
+
if (owner.current) {
|
|
68
|
+
owner.current = null;
|
|
69
|
+
pendingPropagation.current = undefined;
|
|
70
|
+
rebase({ ...config.defaultValues });
|
|
71
|
+
setError(undefined);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const identity = config.getIdentity(user);
|
|
76
|
+
const previous = owner.current;
|
|
77
|
+
const sameAccount = !previous || (previous.client === client && previous.identity === identity);
|
|
78
|
+
const previousBaseline = baseline.current;
|
|
79
|
+
// Keep this token across same-account hydration, but replace it when the
|
|
80
|
+
// account/client changes. A pending validation belongs to that exact owner.
|
|
81
|
+
if (!previous || !sameAccount)
|
|
82
|
+
owner.current = { client, identity };
|
|
83
|
+
rebase(config.toValues(user), sameAccount ? previousBaseline : undefined);
|
|
84
|
+
if (!sameAccount) {
|
|
85
|
+
pendingPropagation.current = undefined;
|
|
86
|
+
setError(undefined);
|
|
87
|
+
}
|
|
88
|
+
}, [user, client, rebase]);
|
|
89
|
+
const submit = useCallback(() => {
|
|
90
|
+
if (activeSave.current)
|
|
91
|
+
return activeSave.current;
|
|
92
|
+
const currentUser = client.getUser();
|
|
93
|
+
if (!mounted.current || !currentUser)
|
|
94
|
+
return Promise.resolve(undefined);
|
|
95
|
+
const config = optionsRef.current;
|
|
96
|
+
const identity = config.getIdentity(currentUser);
|
|
97
|
+
const submissionOwner = owner.current;
|
|
98
|
+
const ownsProfile = () => {
|
|
99
|
+
const current = client.getUser();
|
|
100
|
+
return (mounted.current &&
|
|
101
|
+
committedClient.current === client &&
|
|
102
|
+
owner.current === submissionOwner &&
|
|
103
|
+
current !== null &&
|
|
104
|
+
config.getIdentity(current) === identity);
|
|
105
|
+
};
|
|
106
|
+
const snapshot = readValues();
|
|
107
|
+
const before = { ...baseline.current };
|
|
108
|
+
const changes = {};
|
|
109
|
+
for (const key of Object.keys(snapshot)) {
|
|
110
|
+
if (!Object.is(snapshot[key], before[key]))
|
|
111
|
+
changes[key] = snapshot[key];
|
|
112
|
+
}
|
|
113
|
+
setError(undefined);
|
|
114
|
+
setIsSaving(true);
|
|
115
|
+
let saved;
|
|
116
|
+
const run = async () => {
|
|
117
|
+
try {
|
|
118
|
+
await form.handleSubmit(async () => {
|
|
119
|
+
// Validation can await arbitrary app work. Check BEFORE toPatch and
|
|
120
|
+
// again immediately before the mutation: its credentials are read by
|
|
121
|
+
// updateProfile now, not when the reader originally clicked Save.
|
|
122
|
+
if (!ownsProfile())
|
|
123
|
+
return;
|
|
124
|
+
const patch = config.toPatch(changes, snapshot);
|
|
125
|
+
if (Object.keys(patch).length > 0) {
|
|
126
|
+
if (!ownsProfile())
|
|
127
|
+
return;
|
|
128
|
+
saved = await client.updateProfile(patch);
|
|
129
|
+
if (!ownsProfile()) {
|
|
130
|
+
saved = undefined;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (mounted.current)
|
|
134
|
+
rebase(config.toValues(saved), snapshot);
|
|
135
|
+
pendingPropagation.current = saved;
|
|
136
|
+
}
|
|
137
|
+
if (pendingPropagation.current !== undefined) {
|
|
138
|
+
if (!ownsProfile()) {
|
|
139
|
+
saved = undefined;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const propagated = pendingPropagation.current;
|
|
143
|
+
await config.onSaved?.(propagated);
|
|
144
|
+
pendingPropagation.current = undefined;
|
|
145
|
+
saved = propagated;
|
|
146
|
+
}
|
|
147
|
+
})();
|
|
148
|
+
return saved;
|
|
149
|
+
}
|
|
150
|
+
catch (failure) {
|
|
151
|
+
if (mounted.current)
|
|
152
|
+
setError(failure);
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
activeSave.current = null;
|
|
157
|
+
if (mounted.current)
|
|
158
|
+
setIsSaving(false);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
activeSave.current = run();
|
|
162
|
+
return activeSave.current;
|
|
163
|
+
}, [client, readValues, form.handleSubmit, rebase]);
|
|
164
|
+
const register = useCallback((name, registerOptions) => {
|
|
165
|
+
return form.register(name, registerOptions);
|
|
166
|
+
}, [form.register]);
|
|
167
|
+
const reset = useCallback(() => {
|
|
168
|
+
form.reset(baseline.current);
|
|
169
|
+
setError(undefined);
|
|
170
|
+
}, [form.reset]);
|
|
171
|
+
const onSubmit = useCallback((event) => {
|
|
172
|
+
event.preventDefault();
|
|
173
|
+
void submit();
|
|
174
|
+
}, [submit]);
|
|
175
|
+
return {
|
|
176
|
+
...form,
|
|
177
|
+
formState: { ...form.formState, isSubmitting: isSaving || form.isSubmitting },
|
|
178
|
+
isSubmitting: isSaving || form.isSubmitting,
|
|
179
|
+
user,
|
|
180
|
+
ready: user !== null,
|
|
181
|
+
error,
|
|
182
|
+
register,
|
|
183
|
+
reset,
|
|
184
|
+
submit,
|
|
185
|
+
formProps: { onSubmit },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=useProfileForm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProfileForm.js","sourceRoot":"","sources":["../../src/hooks/useProfileForm.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAI1F,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AACrD,OAAO,EAAE,OAAO,EAA2C,MAAM,cAAc,CAAA;AAyD/E;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAC5B,OAA8C;IAE9C,MAAM,EACJ,aAAa,EACb,WAAW,EAAE,SAAS,EACtB,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,GAAG,WAAW,EACf,GAAG,OAAO,CAAA;IACX,MAAM,IAAI,GAAG,OAAO,CAAU,EAAE,GAAG,WAAW,EAAE,aAAa,EAAE,CAAC,CAAA;IAChE,MAAM,MAAM,GAAG,aAAa,EAAS,CAAA;IACrC,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,EAAS,CAAA;IACjC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAW,CAAA;IAC7C,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC/C,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;IAClC,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,CAAA;IACtC,MAAM,QAAQ,GAAG,MAAM,CAAC,EAAE,GA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,CAAC,KAAuC,EAAQ,EAAE;QAChD,KAAK,CAAC,cAAc,EAAE,CAAA;QACtB,KAAK,MAAM,EAAE,CAAA;IACf,CAAC,EACD,CAAC,MAAM,CAAC,CACT,CAAA;IAED,OAAO;QACL,GAAG,IAAI;QACP,SAAS,EAAE,EAAE,GAAG,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE,QAAQ,IAAI,IAAI,CAAC,YAAY,EAAE;QAC7E,YAAY,EAAE,QAAQ,IAAI,IAAI,CAAC,YAAY;QAC3C,IAAI;QACJ,KAAK,EAAE,IAAI,KAAK,IAAI;QACpB,KAAK;QACL,QAAQ;QACR,KAAK;QACL,MAAM;QACN,SAAS,EAAE,EAAE,QAAQ,EAAE;KACxB,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,33 @@
|
|
|
6
6
|
* chat, workspace, editor, preview), so framework-agnostic providers plug
|
|
7
7
|
* into React idioms.
|
|
8
8
|
*
|
|
9
|
+
* **Editable account/profile forms:** `useProfileForm` composes the bonded form
|
|
10
|
+
* and auth clients. It hydrates asynchronous user data without erasing dirty
|
|
11
|
+
* fields, snapshots controlled values on native form submission, submits only
|
|
12
|
+
* changed fields through an app-supplied adapter, and persists/publishes the
|
|
13
|
+
* server's saved user. Use its `onSaved` callback to update other query caches
|
|
14
|
+
* from that response before navigating. Configure the endpoint, method and JSON
|
|
15
|
+
* envelope once in `AuthClientConfig`; no page-local session store is needed.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const profile = useProfileForm<User, { name: string; email: string }>({
|
|
20
|
+
* defaultValues: { name: '', email: '' },
|
|
21
|
+
* getIdentity: (user) => user.id,
|
|
22
|
+
* toValues: (user) => ({ name: user.name ?? '', email: user.email ?? '' }),
|
|
23
|
+
* toPatch: (changes) => changes,
|
|
24
|
+
* onSaved: async (user) => {
|
|
25
|
+
* // Set/invalidate the app's profile query keys using the saved response.
|
|
26
|
+
* // Navigate only after those writes complete.
|
|
27
|
+
* },
|
|
28
|
+
* })
|
|
29
|
+
* <form {...profile.formProps}>
|
|
30
|
+
* <input {...profile.register('name')} />
|
|
31
|
+
* <input {...profile.register('email', { email: true })} type="email" />
|
|
32
|
+
* <button type="submit" disabled={!profile.ready || profile.isSubmitting}>Save</button>
|
|
33
|
+
* </form>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
9
36
|
* @example
|
|
10
37
|
* ```tsx
|
|
11
38
|
* import { MoleculeProvider, useAuth, useTheme, useTranslation } from '@molecule/app-react'
|
|
@@ -48,6 +75,23 @@
|
|
|
48
75
|
* ```
|
|
49
76
|
*
|
|
50
77
|
* @remarks
|
|
78
|
+
* - `useProfileForm` needs `AuthProvider` (or `MoleculeProvider auth={client}`)
|
|
79
|
+
* and uses the bonded forms provider, just like `useForm`. `defaultValues`
|
|
80
|
+
* and `toValues` must cover every registered control; map nullable text to
|
|
81
|
+
* `''` there. Add optional write-only controls such as `password: ''` to both,
|
|
82
|
+
* then omit a blank password in `toPatch`: `({ password, ...changes }) =>
|
|
83
|
+
* ({ ...changes, ...(password?.trim() ? { password } : {}) })`. `changes`
|
|
84
|
+
* contains only edited fields, so do not fill omitted fields with null or
|
|
85
|
+
* copy every property of the auth user into the request. `reset()` discards
|
|
86
|
+
* edits to the newest baseline. A changed `getIdentity(user)` clears the old
|
|
87
|
+
* account's edits. Field comparisons are shallow, matching form providers.
|
|
88
|
+
* A submission whose account or auth client changes, or whose form unmounts,
|
|
89
|
+
* during asynchronous validation is discarded before any mutation or saved
|
|
90
|
+
* callback. Same-account background hydration does not discard the submit.
|
|
91
|
+
* The hook captures save failures in `error`; render that error with the
|
|
92
|
+
* app's normal API-error presentation. Use native `<form>` submit semantics,
|
|
93
|
+
* and pass the typed `register()` props through custom controls to their
|
|
94
|
+
* actual input/textarea elements.
|
|
51
95
|
* - **Every hook throws when its provider is not mounted.** `MoleculeProvider` wires ONLY the
|
|
52
96
|
* services you pass as props — it is a convenience wrapper, not a default registry. The map:
|
|
53
97
|
* `useAuth`→`auth`, `useTranslation`/`useT`→`i18n`, `useTheme`→`theme`, `useRouter`→`router`,
|
|
@@ -104,6 +148,7 @@
|
|
|
104
148
|
export * from './agent-identity.js';
|
|
105
149
|
export * from './contexts.js';
|
|
106
150
|
export * from './hooks/index.js';
|
|
151
|
+
export * from './hooks/useProfileForm.js';
|
|
107
152
|
export * from './providers.js';
|
|
108
153
|
export * from './types.js';
|
|
109
154
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkJG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA;AAChC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,33 @@
|
|
|
6
6
|
* chat, workspace, editor, preview), so framework-agnostic providers plug
|
|
7
7
|
* into React idioms.
|
|
8
8
|
*
|
|
9
|
+
* **Editable account/profile forms:** `useProfileForm` composes the bonded form
|
|
10
|
+
* and auth clients. It hydrates asynchronous user data without erasing dirty
|
|
11
|
+
* fields, snapshots controlled values on native form submission, submits only
|
|
12
|
+
* changed fields through an app-supplied adapter, and persists/publishes the
|
|
13
|
+
* server's saved user. Use its `onSaved` callback to update other query caches
|
|
14
|
+
* from that response before navigating. Configure the endpoint, method and JSON
|
|
15
|
+
* envelope once in `AuthClientConfig`; no page-local session store is needed.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const profile = useProfileForm<User, { name: string; email: string }>({
|
|
20
|
+
* defaultValues: { name: '', email: '' },
|
|
21
|
+
* getIdentity: (user) => user.id,
|
|
22
|
+
* toValues: (user) => ({ name: user.name ?? '', email: user.email ?? '' }),
|
|
23
|
+
* toPatch: (changes) => changes,
|
|
24
|
+
* onSaved: async (user) => {
|
|
25
|
+
* // Set/invalidate the app's profile query keys using the saved response.
|
|
26
|
+
* // Navigate only after those writes complete.
|
|
27
|
+
* },
|
|
28
|
+
* })
|
|
29
|
+
* <form {...profile.formProps}>
|
|
30
|
+
* <input {...profile.register('name')} />
|
|
31
|
+
* <input {...profile.register('email', { email: true })} type="email" />
|
|
32
|
+
* <button type="submit" disabled={!profile.ready || profile.isSubmitting}>Save</button>
|
|
33
|
+
* </form>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
9
36
|
* @example
|
|
10
37
|
* ```tsx
|
|
11
38
|
* import { MoleculeProvider, useAuth, useTheme, useTranslation } from '@molecule/app-react'
|
|
@@ -48,6 +75,23 @@
|
|
|
48
75
|
* ```
|
|
49
76
|
*
|
|
50
77
|
* @remarks
|
|
78
|
+
* - `useProfileForm` needs `AuthProvider` (or `MoleculeProvider auth={client}`)
|
|
79
|
+
* and uses the bonded forms provider, just like `useForm`. `defaultValues`
|
|
80
|
+
* and `toValues` must cover every registered control; map nullable text to
|
|
81
|
+
* `''` there. Add optional write-only controls such as `password: ''` to both,
|
|
82
|
+
* then omit a blank password in `toPatch`: `({ password, ...changes }) =>
|
|
83
|
+
* ({ ...changes, ...(password?.trim() ? { password } : {}) })`. `changes`
|
|
84
|
+
* contains only edited fields, so do not fill omitted fields with null or
|
|
85
|
+
* copy every property of the auth user into the request. `reset()` discards
|
|
86
|
+
* edits to the newest baseline. A changed `getIdentity(user)` clears the old
|
|
87
|
+
* account's edits. Field comparisons are shallow, matching form providers.
|
|
88
|
+
* A submission whose account or auth client changes, or whose form unmounts,
|
|
89
|
+
* during asynchronous validation is discarded before any mutation or saved
|
|
90
|
+
* callback. Same-account background hydration does not discard the submit.
|
|
91
|
+
* The hook captures save failures in `error`; render that error with the
|
|
92
|
+
* app's normal API-error presentation. Use native `<form>` submit semantics,
|
|
93
|
+
* and pass the typed `register()` props through custom controls to their
|
|
94
|
+
* actual input/textarea elements.
|
|
51
95
|
* - **Every hook throws when its provider is not mounted.** `MoleculeProvider` wires ONLY the
|
|
52
96
|
* services you pass as props — it is a convenience wrapper, not a default registry. The map:
|
|
53
97
|
* `useAuth`→`auth`, `useTranslation`/`useT`→`i18n`, `useTheme`→`theme`, `useRouter`→`router`,
|
|
@@ -104,6 +148,7 @@
|
|
|
104
148
|
export * from './agent-identity.js';
|
|
105
149
|
export * from './contexts.js';
|
|
106
150
|
export * from './hooks/index.js';
|
|
151
|
+
export * from './hooks/useProfileForm.js';
|
|
107
152
|
export * from './providers.js';
|
|
108
153
|
export * from './types.js';
|
|
109
154
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkJG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,eAAe,CAAA;AAC7B,cAAc,kBAAkB,CAAA;AAChC,cAAc,2BAA2B,CAAA;AACzC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA"}
|