-
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.
Move most actions logic to react-providers
closes #109
- Loading branch information
1 parent
7c88865
commit 54f7555
Showing
11 changed files
with
194 additions
and
67 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
2 changes: 1 addition & 1 deletion
2
packages/chakra-components/src/components/Election/Actions/Actions.tsx
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
38 changes: 15 additions & 23 deletions
38
packages/chakra-components/src/components/Election/Actions/ActionsProvider.tsx
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,27 +1,19 @@ | ||
import { PropsWithChildren, createContext, useContext } from 'react' | ||
import { useActionsProvider } from './use-actions-provider' | ||
|
||
export type ActionsState = ReturnType<typeof useActionsProvider> | ||
|
||
export const ActionsContext = createContext<ActionsState | undefined>(undefined) | ||
|
||
export const useActions = () => { | ||
const ctxt = useContext(ActionsContext) | ||
if (!ctxt) { | ||
throw new Error( | ||
'useActions returned `undefined`, maybe you forgot to wrap the component within <ActionsProvider />?' | ||
) | ||
} | ||
|
||
return ctxt | ||
import { ActionsProvider as RActionsProvider } from '@vocdoni/react-providers' | ||
import { PropsWithChildren, ReactElement } from 'react' | ||
import { useActionsToast } from './use-actions-toast' | ||
|
||
export const ActionsProvider = (props: PropsWithChildren) => { | ||
return ( | ||
<RActionsProvider> | ||
<ChakraInternalActionsProvider {...props} /> | ||
</RActionsProvider> | ||
) | ||
} | ||
|
||
export type ActionsProviderComponentProps = PropsWithChildren | ||
|
||
export const ActionsProvider = ({ children }: ActionsProviderComponentProps) => { | ||
const value = useActionsProvider() | ||
// We need to define an "internal" component in order to be able to use the | ||
// hooks, otherwise they wouldn't have access to the ActionsProvider context | ||
const ChakraInternalActionsProvider = ({ children }: PropsWithChildren) => { | ||
useActionsToast() | ||
|
||
return <ActionsContext.Provider value={value}>{children}</ActionsContext.Provider> | ||
return children as ReactElement | ||
} | ||
|
||
ActionsProvider.displayName = 'ActionsProvider' |
5 changes: 2 additions & 3 deletions
5
packages/chakra-components/src/components/Election/Actions/Cancel.tsx
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
5 changes: 2 additions & 3 deletions
5
packages/chakra-components/src/components/Election/Actions/Continue.tsx
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
5 changes: 2 additions & 3 deletions
5
packages/chakra-components/src/components/Election/Actions/End.tsx
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
5 changes: 2 additions & 3 deletions
5
packages/chakra-components/src/components/Election/Actions/Pause.tsx
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
34 changes: 34 additions & 0 deletions
34
packages/chakra-components/src/components/Election/Actions/use-actions-toast.ts
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,34 @@ | ||
import { ToastId, useToast } from '@chakra-ui/toast' | ||
import { useActions } from '@vocdoni/react-providers' | ||
import { useEffect, useRef } from 'react' | ||
|
||
export const useActionsToast = () => { | ||
const tRef = useRef<ToastId>() | ||
const { info, error } = useActions() | ||
const toast = useToast() | ||
|
||
// show toasts for info and error | ||
useEffect(() => { | ||
if (toast && info === null && tRef.current) { | ||
toast.close(tRef.current) | ||
} | ||
if (info && toast) { | ||
tRef.current = toast({ | ||
title: info.title, | ||
description: info.description, | ||
status: 'info', | ||
duration: null, | ||
isClosable: false, | ||
}) | ||
} | ||
if (error && toast) { | ||
toast({ | ||
title: error.title, | ||
description: error.description, | ||
status: 'error', | ||
duration: 7000, | ||
isClosable: false, | ||
}) | ||
} | ||
}, [info, error, toast]) | ||
} |
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
Oops, something went wrong.