Use real endpoints, optimistic state
This commit is contained in:
@@ -1,30 +1,32 @@
|
|||||||
import {useMutation} from '@tanstack/react-query'
|
import {
|
||||||
|
type AppBskyUnspeccedDefs,
|
||||||
|
type AppBskyUnspeccedInitAgeAssurance,
|
||||||
|
} from '@atproto/api'
|
||||||
|
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {wait} from '#/lib/async/wait'
|
import {wait} from '#/lib/async/wait'
|
||||||
import {isNetworkError} from '#/lib/hooks/useCleanError'
|
import {isNetworkError} from '#/lib/hooks/useCleanError'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
// import {useAgent} from '#/state/session';
|
import {createAgeAssuranceQueryKey} from '#/state/age-assurance'
|
||||||
|
import {useAgent} from '#/state/session'
|
||||||
type TempInputSchema = {
|
|
||||||
email: string
|
|
||||||
language: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useInitAgeAssurance() {
|
export function useInitAgeAssurance() {
|
||||||
// const agent = useAgent()
|
const qc = useQueryClient()
|
||||||
|
const agent = useAgent()
|
||||||
return useMutation({
|
return useMutation({
|
||||||
async mutationFn(_props: TempInputSchema) {
|
async mutationFn(props: AppBskyUnspeccedInitAgeAssurance.InputSchema) {
|
||||||
// 2s wait is good actually, email sending takes a hot sec
|
/*
|
||||||
// const {data} = await wait(2e3, agent.app.bsky.unspecced.initAgeAssurance(props))
|
* 2s wait is good actually. Email sending takes a hot sec and this helps
|
||||||
await wait(
|
* ensure the email is ready for the user once they open their inbox.
|
||||||
|
*/
|
||||||
|
const {data} = await wait(
|
||||||
2e3,
|
2e3,
|
||||||
(() => ({
|
agent.app.bsky.unspecced.initAgeAssurance(props),
|
||||||
data: {
|
)
|
||||||
$type: 'app.bsky.unspecced.defs#ageAssuranceState',
|
|
||||||
lastInitiatedAt: new Date().toISOString(),
|
qc.setQueryData<AppBskyUnspeccedDefs.AgeAssuranceState>(
|
||||||
status: 'pending',
|
createAgeAssuranceQueryKey(agent.session?.did ?? 'never'),
|
||||||
},
|
() => data,
|
||||||
}))(),
|
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
onError(e) {
|
onError(e) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {preferencesQueryKey} from '#/state/queries/preferences'
|
|||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
|
|
||||||
const logger = Logger.create(Logger.Context.AgeAssurance)
|
const logger = Logger.create(Logger.Context.AgeAssurance)
|
||||||
const createAgeAssuranceQueryKey = (did: string) =>
|
export const createAgeAssuranceQueryKey = (did: string) =>
|
||||||
['ageAssurance', did] as const
|
['ageAssurance', did] as const
|
||||||
const DEFAULT_AGE_ASSURANCE_STATE: AppBskyUnspeccedDefs.AgeAssuranceState = {
|
const DEFAULT_AGE_ASSURANCE_STATE: AppBskyUnspeccedDefs.AgeAssuranceState = {
|
||||||
lastInitiatedAt: undefined,
|
lastInitiatedAt: undefined,
|
||||||
@@ -44,12 +44,7 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
try {
|
try {
|
||||||
const {data} = await wait(
|
const {data} = await wait(
|
||||||
1e3,
|
1e3,
|
||||||
(() => ({
|
agent.app.bsky.unspecced.getAgeAssuranceState(),
|
||||||
data: {
|
|
||||||
lastInitiatedAt: undefined,
|
|
||||||
status: 'unknown',
|
|
||||||
} as AppBskyUnspeccedDefs.AgeAssuranceState,
|
|
||||||
}))(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(`fetch`, {
|
logger.debug(`fetch`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user