-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement useSaasAccount * Implement useSaasAccount on needed screens * Implement update account * Improve FormSubmitMessage * Expose fetch account * Bulk fixes * Usa await instead of chaining promises
- Loading branch information
Showing
12 changed files
with
166 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
export interface OrgInterface { | ||
import { AccountData, IAccount } from '@vocdoni/sdk' | ||
|
||
export type SaasOrganizationData = { | ||
active: boolean | ||
address: string | ||
createdAt: string | ||
name: string | ||
website: string | ||
description: string | ||
size: string | ||
type: string | ||
country: string | ||
timezone: string | ||
language: string | ||
logo: string | ||
header: string | ||
subdomain: string | ||
color: string | ||
communications: boolean | ||
} | ||
|
||
export type CreateOrgParams = Partial<Omit<OrgInterface, 'active' | 'address' | 'createdAt'>> | ||
export type OrganizationData = SaasOrganizationData & AccountData | ||
|
||
export type CreateOrgParams = Partial< | ||
Pick<IAccount, 'name' | 'description' | 'logo' | 'header'> & | ||
Omit<SaasOrganizationData, 'active' | 'address' | 'createdAt'> | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createContext, ReactNode } from 'react' | ||
import { useSaasAccountProvider } from './useSaasAccountProvider' | ||
|
||
export const SaasAccountContext = createContext<ReturnType<typeof useSaasAccountProvider> | undefined>(undefined) | ||
|
||
export const SaasAccountProvider = ({ children }: { children: ReactNode }) => { | ||
const isSaas = !!import.meta.env.SAAS_URL | ||
const saasAcount = useSaasAccountProvider({ options: { enabled: isSaas } }) | ||
if (!isSaas) { | ||
return children | ||
} | ||
return <SaasAccountContext.Provider value={saasAcount}>{children}</SaasAccountContext.Provider> | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useContext } from 'react' | ||
import { SaasAccountContext } from '~components/AccountSaas/SaasAccountContext' | ||
|
||
export const useSaasAccount = () => { | ||
const context = useContext(SaasAccountContext) | ||
if (!context) { | ||
throw new Error('useSaasAccount must be used within an SaasAccountProvider') | ||
} | ||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useClient } from '@vocdoni/react-providers' | ||
import { useQuery, UseQueryOptions } from '@tanstack/react-query' | ||
import { OrganizationData } from '~components/AccountSaas/AccountTypes' | ||
import { useAuth } from '~components/Auth/useAuth' | ||
import { ApiEndpoints } from '~components/Auth/api' | ||
import { useCallback } from 'react' | ||
|
||
const useSaasOrganization = ({ | ||
options, | ||
}: { | ||
options?: Omit<UseQueryOptions<OrganizationData>, 'queryKey' | 'queryFn'> | ||
} = {}) => { | ||
const { bearedFetch, signerAddress } = useAuth() | ||
|
||
return useQuery({ | ||
queryKey: ['organizations', 'info', signerAddress], | ||
queryFn: () => bearedFetch<OrganizationData>(ApiEndpoints.ORGANIZATION.replace('{address}', signerAddress)), | ||
enabled: !!signerAddress, | ||
...options, | ||
}) | ||
} | ||
|
||
export const useSaasAccountProvider = (options?: Parameters<typeof useSaasOrganization>[0]) => { | ||
const { | ||
account: accountSDK, | ||
fetchAccount, | ||
errors: { fetch: sdkAccountError }, | ||
loading: { fetch: sdkAccountLoading }, | ||
} = useClient() | ||
const { | ||
data: saasData, | ||
refetch, | ||
isLoading: isSaasLoading, | ||
isError: isSaasError, | ||
error: saasError, | ||
} = useSaasOrganization(options) | ||
|
||
const refetchAccount = useCallback(() => { | ||
refetch() | ||
fetchAccount() | ||
}, [refetch, fetchAccount]) | ||
|
||
const organization: OrganizationData = { ...accountSDK, ...saasData } | ||
|
||
const isLoading = isSaasLoading || sdkAccountLoading | ||
const isError = isSaasError || !!sdkAccountError | ||
const error = saasError || sdkAccountError | ||
|
||
return { organization, refetchAccount, isLoading, isError, error } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters