Refactor app passwords to use react-query (#1932)
This commit is contained in:
parent
310a7eaca7
commit
9f7a162a96
6 changed files with 177 additions and 115 deletions
56
src/state/queries/app-passwords.ts
Normal file
56
src/state/queries/app-passwords.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
|
||||
import {useQuery, useQueryClient, useMutation} from '@tanstack/react-query'
|
||||
import {useSession} from '../session'
|
||||
|
||||
export const RQKEY = () => ['app-passwords']
|
||||
|
||||
export function useAppPasswordsQuery() {
|
||||
const {agent} = useSession()
|
||||
return useQuery({
|
||||
queryKey: RQKEY(),
|
||||
queryFn: async () => {
|
||||
const res = await agent.com.atproto.server.listAppPasswords({})
|
||||
return res.data.passwords
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useAppPasswordCreateMutation() {
|
||||
const {agent} = useSession()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation<
|
||||
ComAtprotoServerCreateAppPassword.OutputSchema,
|
||||
Error,
|
||||
{name: string}
|
||||
>({
|
||||
mutationFn: async ({name}) => {
|
||||
return (
|
||||
await agent.com.atproto.server.createAppPassword({
|
||||
name,
|
||||
})
|
||||
).data
|
||||
},
|
||||
onSuccess() {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: RQKEY(),
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useAppPasswordDeleteMutation() {
|
||||
const {agent} = useSession()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation<void, Error, {name: string}>({
|
||||
mutationFn: async ({name}) => {
|
||||
await agent.com.atproto.server.revokeAppPassword({
|
||||
name,
|
||||
})
|
||||
},
|
||||
onSuccess() {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: RQKEY(),
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue