From 108edf4dfeeeede08181eea78adeef1f318971f5 Mon Sep 17 00:00:00 2001 From: phamhieu Date: Thu, 4 May 2023 12:56:24 +0700 Subject: [PATCH] fix: create new user profile --- studio/data/profile/profile-query.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/studio/data/profile/profile-query.ts b/studio/data/profile/profile-query.ts index 479cb7233aa..63d16a441d7 100644 --- a/studio/data/profile/profile-query.ts +++ b/studio/data/profile/profile-query.ts @@ -1,8 +1,9 @@ import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query' -import { get } from 'lib/common/fetch' +import { get, post } from 'lib/common/fetch' import { API_URL } from 'lib/constants' import { useCallback } from 'react' import { profileKeys } from './keys' +import Telemetry from 'lib/telemetry' export type Profile = { id: number @@ -19,11 +20,36 @@ export type Profile = { export type ProfileResponse = Profile +/** + * Create profile for new user + * This will also need to send a sign_up event + */ +async function createProfile() { + const response = await post(`${API_URL}/profile`, {}) + if (response.error) { + throw response.error + } + + Telemetry.sendEvent( + { category: 'conversion', action: 'sign_up', label: '' }, + { + screenResolution: + typeof window !== 'undefined' ? `${window.innerWidth}x${window.innerHeight}` : undefined, + language: 'en-US', + } + ) + + return response +} + export async function getProfile(signal?: AbortSignal) { const response = await get(`${API_URL}/profile`, { signal, }) if (response.error) { + // if response.error is not found + // we need to send request to create new user profile + if (response.error.code === 404) return await createProfile() throw response.error }