B2B-88: add starter kit structure and elements
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
|
||||
|
||||
export function usePersonalAccountData(
|
||||
userId: string,
|
||||
partialAccount?: {
|
||||
id: string | null;
|
||||
name: string | null;
|
||||
picture_url: string | null;
|
||||
},
|
||||
) {
|
||||
const client = useSupabase();
|
||||
const queryKey = ['account:data', userId];
|
||||
|
||||
const queryFn = async () => {
|
||||
if (!userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const response = await client
|
||||
.from('accounts')
|
||||
.select(
|
||||
`
|
||||
id,
|
||||
name,
|
||||
picture_url
|
||||
`,
|
||||
)
|
||||
.eq('primary_owner_user_id', userId)
|
||||
.eq('is_personal_account', true)
|
||||
.single();
|
||||
|
||||
if (response.error) {
|
||||
throw response.error;
|
||||
}
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
return useQuery({
|
||||
queryKey,
|
||||
queryFn,
|
||||
enabled: !!userId,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnMount: false,
|
||||
initialData: partialAccount?.id
|
||||
? {
|
||||
id: partialAccount.id,
|
||||
name: partialAccount.name,
|
||||
picture_url: partialAccount.picture_url,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
export function useRevalidatePersonalAccountDataQuery() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useCallback(
|
||||
(userId: string) =>
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['account:data', userId],
|
||||
}),
|
||||
[queryClient],
|
||||
);
|
||||
}
|
||||
29
packages/features/accounts/src/hooks/use-update-account.ts
Normal file
29
packages/features/accounts/src/hooks/use-update-account.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
|
||||
|
||||
type UpdateData = Database['public']['Tables']['accounts']['Update'];
|
||||
|
||||
export function useUpdateAccountData(accountId: string) {
|
||||
const client = useSupabase();
|
||||
|
||||
const mutationKey = ['account:data', accountId];
|
||||
|
||||
const mutationFn = async (data: UpdateData) => {
|
||||
const response = await client.from('accounts').update(data).match({
|
||||
id: accountId,
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
throw response.error;
|
||||
}
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
return useMutation({
|
||||
mutationKey,
|
||||
mutationFn,
|
||||
});
|
||||
}
|
||||
17
packages/features/accounts/src/hooks/use-user-workspace.ts
Normal file
17
packages/features/accounts/src/hooks/use-user-workspace.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
'use client';
|
||||
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { UserWorkspaceContext } from '../components';
|
||||
|
||||
export function useUserWorkspace() {
|
||||
const ctx = useContext(UserWorkspaceContext);
|
||||
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useUserWorkspace must be used within a UserWorkspaceProvider. This is only provided within the user workspace /home',
|
||||
);
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user