2021-12-24 21:06:01 +00:00
|
|
|
import { getFunctions, httpsCallable } from 'firebase/functions'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Fold } from '../../../common/fold'
|
2022-01-18 07:18:38 +00:00
|
|
|
import { User } from '../../../common/user'
|
2022-01-19 03:36:46 +00:00
|
|
|
import { randomString } from '../../../common/util/random'
|
2022-01-25 23:02:02 +00:00
|
|
|
import './init'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
const functions = getFunctions()
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export const cloudFunction = <RequestData, ResponseData>(name: string) =>
|
|
|
|
httpsCallable<RequestData, ResponseData>(functions, name)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-01-05 18:23:58 +00:00
|
|
|
export const createContract = cloudFunction('createContract')
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export const createFold = cloudFunction<
|
2022-01-25 20:47:25 +00:00
|
|
|
{ name: string; about: string; tags: string[] },
|
2022-01-21 23:21:46 +00:00
|
|
|
{ status: 'error' | 'success'; message?: string; fold?: Fold }
|
|
|
|
>('createFold')
|
|
|
|
|
2022-01-05 18:23:58 +00:00
|
|
|
export const placeBet = cloudFunction('placeBet')
|
|
|
|
|
|
|
|
export const resolveMarket = cloudFunction('resolveMarket')
|
|
|
|
|
|
|
|
export const sellBet = cloudFunction('sellBet')
|
2022-01-18 07:18:38 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
export const createUser: () => Promise<User | null> = () => {
|
|
|
|
let deviceToken = window.localStorage.getItem('device-token')
|
|
|
|
if (!deviceToken) {
|
|
|
|
deviceToken = randomString()
|
|
|
|
window.localStorage.setItem('device-token', deviceToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloudFunction('createUser')({ deviceToken })
|
2022-01-18 07:18:38 +00:00
|
|
|
.then((r) => (r.data as any)?.user || null)
|
|
|
|
.catch(() => null)
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
export const changeUserInfo = (data: {
|
|
|
|
username?: string
|
|
|
|
name?: string
|
|
|
|
avatarUrl?: string
|
|
|
|
}) => {
|
|
|
|
return cloudFunction('changeUserInfo')(data)
|
|
|
|
.then((r) => r.data as { status: string; message?: string })
|
|
|
|
.catch((e) => ({ status: 'error', message: e.message }))
|
|
|
|
}
|