-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add a modal provider * feat: pass down entire modal. throw on invalid name * feat: closeAllModals. better types. only pass down modal * feat: update README * chore: be consistent (sorry 😅) * fix: typo * feat: use <>
- Loading branch information
1 parent
c026246
commit f50cc0e
Showing
17 changed files
with
283 additions
and
14 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
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,3 +1,2 @@ | ||
import EtherscanLink from './EtherscanLink.container' | ||
|
||
export default EtherscanLink |
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,29 @@ | ||
import { action } from 'typesafe-actions' | ||
|
||
// Open Modal | ||
|
||
export const OPEN_MODAL = 'Open Modal' | ||
|
||
export const openModal = (name: string, metadata: any = null) => | ||
action(OPEN_MODAL, { | ||
name, | ||
metadata | ||
}) | ||
|
||
export type OpenModalAction = ReturnType<typeof openModal> | ||
|
||
// Close Modal | ||
|
||
export const CLOSE_MODAL = 'Close Modal' | ||
|
||
export const closeModal = (name: string) => action(CLOSE_MODAL, { name }) | ||
|
||
export type CloseModalAction = ReturnType<typeof closeModal> | ||
|
||
// Close All Modals | ||
|
||
export const CLOSE_ALL_MODALS = 'Close All Modal' | ||
|
||
export const closeAllModals = () => action(CLOSE_ALL_MODALS, {}) | ||
|
||
export type CloseAllModalsAction = ReturnType<typeof closeAllModals> |
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,63 @@ | ||
import { | ||
OPEN_MODAL, | ||
CLOSE_MODAL, | ||
CLOSE_ALL_MODALS, | ||
OpenModalAction, | ||
CloseModalAction, | ||
CloseAllModalsAction | ||
} from './actions' | ||
import { Modal } from './types' | ||
|
||
export type ModalState = Record<string, Modal> | ||
|
||
const INITIAL_STATE: ModalState = {} | ||
|
||
export type ModalReducerAction = | ||
| OpenModalAction | ||
| CloseModalAction | ||
| CloseAllModalsAction | ||
|
||
export function modalReducer( | ||
state = INITIAL_STATE, | ||
action: ModalReducerAction | ||
) { | ||
switch (action.type) { | ||
case OPEN_MODAL: { | ||
const { name, metadata } = action.payload | ||
|
||
return { | ||
...state, | ||
[name]: { | ||
open: true, | ||
name, | ||
metadata | ||
} | ||
} | ||
} | ||
case CLOSE_MODAL: { | ||
const { name } = action.payload | ||
|
||
if (state[name]) { | ||
return { | ||
...state, | ||
[name]: { | ||
...state[name], | ||
open: false | ||
} | ||
} | ||
} else { | ||
// Invalid modal name | ||
return state | ||
} | ||
} | ||
case CLOSE_ALL_MODALS: { | ||
const newState = {} | ||
for (const name in state) { | ||
newState[name] = { ...state[name], open: false } | ||
} | ||
return newState | ||
} | ||
default: | ||
return state | ||
} | ||
} |
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,16 @@ | ||
import { ModalState } from './reducer' | ||
|
||
export const getState: (state: any) => ModalState = state => state.modal | ||
export const getOpenModals: (state: any) => ModalState = state => { | ||
const openModals = {} | ||
const modals = getState(state) | ||
|
||
for (const name in modals) { | ||
const modal = modals[name] | ||
if (modal.open) { | ||
openModals[name] = modal | ||
} | ||
} | ||
|
||
return openModals | ||
} |
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,9 @@ | ||
export type Modal = { | ||
open: boolean | ||
name: string | ||
metadata: any | ||
} | ||
|
||
export type ModalProps = { | ||
modal: Modal | ||
} |
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,16 @@ | ||
import { connect } from 'react-redux' | ||
import { RootDispatch } from '../../types' | ||
import { getState as getModals } from '../../modules/modal/selectors' | ||
import { MapStateProps, MapDispatchProps } from './ModalProvider.types' | ||
import ModalProvider from './ModalProvider' | ||
|
||
const mapState = (state: any): MapStateProps => ({ | ||
modals: getModals(state) | ||
}) | ||
|
||
const mapDispatch = (_: RootDispatch): MapDispatchProps => ({}) | ||
|
||
export default connect( | ||
mapState, | ||
mapDispatch | ||
)(ModalProvider) as any |
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,36 @@ | ||
import * as React from 'react' | ||
|
||
import { ModalProps } from '../../modules/modal/types' | ||
import { DefaultProps, Props } from './ModalProvider.types' | ||
|
||
export default class ModalProvider extends React.PureComponent<Props> { | ||
static defaultProps: DefaultProps = { | ||
children: null | ||
} | ||
|
||
render() { | ||
const { children, components, modals } = this.props | ||
|
||
const ModalComponents: JSX.Element[] = [] | ||
|
||
for (const name in modals) { | ||
const modal = modals[name] | ||
let ModalComponent: React.ComponentType<ModalProps> = components[name] | ||
|
||
if (!ModalComponent) { | ||
if (name) { | ||
throw new Error(`Couldn't find a modal Component named "${name}"`) | ||
} | ||
} | ||
|
||
ModalComponents.push(<ModalComponent key={name} modal={modal} />) | ||
} | ||
|
||
return ( | ||
<> | ||
{children} | ||
{ModalComponents} | ||
</> | ||
) | ||
} | ||
} |
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,14 @@ | ||
import { ModalState } from '../../modules/modal/reducer' | ||
import { ModalProps } from '../../modules/modal/types' | ||
|
||
export type DefaultProps = { | ||
children: React.ReactNode | null | ||
} | ||
|
||
export type Props = DefaultProps & { | ||
components: Record<string, React.ComponentType<ModalProps>> | ||
modals: ModalState | ||
} | ||
|
||
export type MapStateProps = Pick<Props, 'modals'> | ||
export type MapDispatchProps = {} |
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,2 @@ | ||
import ModalProvider from './ModalProvider.container' | ||
export default ModalProvider |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
import TranslationSetup from './TranslationSetup.container' | ||
|
||
export default TranslationSetup |
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,3 +1,2 @@ | ||
import TranslationProvider from './TranslationProvider.container' | ||
|
||
export default TranslationProvider |
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,13 +1,12 @@ | ||
import { connectWalletRequest } from '../../modules/wallet/actions' | ||
|
||
export interface DefaultProps { | ||
export type DefaultProps = { | ||
children: React.ReactNode | null | ||
} | ||
|
||
export interface Props extends DefaultProps { | ||
export type Props = DefaultProps & { | ||
onConnect: typeof connectWalletRequest | ||
} | ||
|
||
export type MapStateProps = {} | ||
export type MapDispatchProps = Pick<Props, 'onConnect'> | ||
|
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,3 +1,2 @@ | ||
import WalletProvider from './WalletProvider.container' | ||
|
||
export default WalletProvider |