Skip to content

Commit

Permalink
users
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvard committed May 25, 2024
1 parent 9b63b20 commit e7b14f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/pages/brukere.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { Spinner } from '../components/loading/Spinner'
import React from 'react'
import { Button, Heading, Table } from '@navikt/ds-react'
import { UseUsers } from '../queries/useUsers'
import { UseMutateUser } from '../queries/mutateUser'

const Brukere: NextPage = () => {
const { data } = UseUsers()
const muteteUser = UseMutateUser()

if (!data) {
return <Spinner />
}
Expand All @@ -30,12 +33,22 @@ const Brukere: NextPage = () => {
<Table.DataCell>{user.name}</Table.DataCell>
<Table.DataCell>
{user.active && (
<Button type={'button'} variant={'danger'} size={'xsmall'}>
<Button type={'button'} variant={'danger'} size={'xsmall'}
onClick={() => muteteUser.mutate({
id: user.id,
request: { active: false },
})}
>
Deaktiver
</Button>
)}
{!user.active && (
<Button type={'button'} variant={'secondary'} size={'xsmall'}>
<Button type={'button'} variant={'secondary'} size={'xsmall'}
onClick={() => muteteUser.mutate({
id: user.id,
request: { active: true },
})}
>
Aktiver
</Button>
)}
Expand Down
36 changes: 36 additions & 0 deletions src/queries/mutateUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useAuthState } from 'react-firebase-hooks/auth'
import { getFirebaseAuth } from '../auth/clientApp'
import { useMutation, useQueryClient } from '@tanstack/react-query'


interface MuteteUserReq {
id: string,
request: {
paid?: boolean,
admin?: boolean,
active?: boolean,
}
}

export function UseMutateUser() {
const queryClient = useQueryClient()
const [user] = useAuthState(getFirebaseAuth())

return useMutation<any, unknown, MuteteUserReq>({
mutationFn: async (req) => {
const idtoken = await user?.getIdToken()
const responsePromise = await fetch(`https://betpool-2022-backend.vercel.app/api/v1/users/${req.id}`, {
method: 'PUT',
body: JSON.stringify(req.request),
headers: { Authorization: `Bearer ${idtoken}` },
})
return responsePromise.json()
},

onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['users'] }).then()
queryClient.invalidateQueries({ queryKey: ['all-bets'] }).then()

},
})
}

0 comments on commit e7b14f3

Please sign in to comment.