-
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.
refs #854 Merge remote-tracking branch 'origin/develop' into f/alerts…
…-854
- Loading branch information
Showing
63 changed files
with
1,996 additions
and
931 deletions.
There are no files selected for viewing
File renamed without changes.
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 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,67 @@ | ||
import { createContext } from '@chakra-ui/react-utils' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { useClient } from '@vocdoni/react-providers' | ||
import { dotobject, ensure0x } from '@vocdoni/sdk' | ||
import { ReactNode, useMemo } from 'react' | ||
import { useAuth } from '~components/Auth/useAuth' | ||
import type { Plan } from '~components/Pricing/Plans' | ||
import { ApiEndpoints } from './api' | ||
|
||
type PermissionsContextType = { | ||
permission: (key: string) => any | ||
subscription: SubscriptionType | ||
loading: boolean | ||
} | ||
|
||
type SubscriptionType = { | ||
subscriptionDetails: { | ||
planID: number | ||
startDate: string // ISO 8601 Date String | ||
endDate: string // ISO 8601 Date String | ||
renewalDate: string // ISO 8601 Date String | ||
active: boolean | ||
maxCensusSize: number | ||
} | ||
usage: { | ||
sentSMS: number | ||
sentEmails: number | ||
subOrgs: number | ||
members: number | ||
} | ||
plan: Plan | ||
} | ||
|
||
const [SubscriptionProvider, useSubscription] = createContext<PermissionsContextType>({ | ||
name: 'PermissionsContext', | ||
errorMessage: 'usePermissions must be used within a PermissionsProvider', | ||
}) | ||
|
||
const SubscriptionProviderComponent: React.FC<{ children: ReactNode }> = ({ children }) => { | ||
const { bearedFetch } = useAuth() | ||
const { account } = useClient() | ||
|
||
// Fetch organization subscription details | ||
// TODO: In the future, this may be merged with the role permissions (not yet defined) | ||
const { data: subscription, isFetching } = useQuery({ | ||
queryKey: ['organizationSubscription', account?.address], | ||
queryFn: () => | ||
bearedFetch<SubscriptionType>( | ||
ApiEndpoints.OrganizationSubscription.replace('{address}', ensure0x(account?.address)) | ||
), | ||
// Cache for 15 minutes | ||
staleTime: 15 * 60 * 1000, | ||
enabled: !!account?.address, | ||
}) | ||
|
||
// Helper function to access permission using dot notation | ||
const permission = useMemo(() => { | ||
return (key: string) => { | ||
if (!subscription || !subscription.plan) return undefined | ||
return dotobject(subscription.plan, key) | ||
} | ||
}, [subscription]) | ||
|
||
return <SubscriptionProvider value={{ permission, subscription, loading: isFetching }} children={children} /> | ||
} | ||
|
||
export { SubscriptionProviderComponent as SubscriptionProvider, useSubscription } |
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 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
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,28 @@ | ||
import { Checkbox, CheckboxProps, Text, useMultiStyleConfig } from '@chakra-ui/react' | ||
import { cloneElement, ReactElement } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
|
||
export type DetailedCheckboxProps = CheckboxProps & { | ||
badge?: ReactElement | ||
description?: string | ||
icon?: ReactElement | ||
name: string | ||
title: string | ||
} | ||
|
||
export const DetailedCheckbox = ({ icon, badge, title, description, name, ...props }: DetailedCheckboxProps) => { | ||
const styles = useMultiStyleConfig('DetailedCheckbox', props) | ||
const { register } = useFormContext() | ||
|
||
return ( | ||
<Checkbox variant='detailed' {...register(name)} {...props} sx={styles.checkbox}> | ||
<Text sx={styles.title}> | ||
{icon && cloneElement(icon, { sx: styles.icon })} | ||
{title} | ||
</Text> | ||
<Text sx={styles.description}>{description}</Text> | ||
|
||
{badge && cloneElement(badge, { sx: styles.badge })} | ||
</Checkbox> | ||
) | ||
} |
Oops, something went wrong.