-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(frontend): Adding customizable logic (#3112)
* feature(frontend): Adding customizable logic * feature(frontend): Adding customizable logic * update naming and exports --------- Co-authored-by: Jorge Padilla <[email protected]>
- Loading branch information
Showing
13 changed files
with
114 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {Button, ButtonProps, Tooltip} from 'antd'; | ||
import {Operation, useCustomization} from 'providers/Customization/Customization.provider'; | ||
|
||
interface IProps extends ButtonProps { | ||
operation: Operation; | ||
} | ||
|
||
const AllowButton = ({operation, ...props}: IProps) => { | ||
const {getIsAllowed} = useCustomization(); | ||
const isAllowed = getIsAllowed(operation); | ||
|
||
return ( | ||
<Tooltip title={!isAllowed ? 'You are not allowed to perform this operation' : ''}> | ||
<Button {...props} disabled={!isAllowed} /> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default AllowButton; |
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,3 @@ | ||
export {Operation} from 'providers/Customization'; | ||
// eslint-disable-next-line no-restricted-exports | ||
export {default} from './AllowButton'; |
17 changes: 17 additions & 0 deletions
17
web/src/components/CustomizationWrapper/CustomizationWrapper.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {useMemo} from 'react'; | ||
import CustomizationProvider from 'providers/Customization'; | ||
|
||
interface IProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const getComponent = <T,>(id: string, fallback: React.ComponentType<T>) => fallback; | ||
const getIsAllowed = () => true; | ||
|
||
const CustomizationWrapper = ({children}: IProps) => { | ||
const customizationProviderValue = useMemo(() => ({getComponent, getIsAllowed}), []); | ||
|
||
return <CustomizationProvider value={customizationProviderValue}>{children}</CustomizationProvider>; | ||
}; | ||
|
||
export default CustomizationWrapper; |
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 @@ | ||
// eslint-disable-next-line no-restricted-exports | ||
export {default} from './CustomizationWrapper'; |
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
30 changes: 30 additions & 0 deletions
30
web/src/providers/Customization/Customization.provider.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {createContext, useContext} from 'react'; | ||
|
||
export enum Operation { | ||
Configure = 'configure', | ||
Edit = 'edit', | ||
View = 'view', | ||
} | ||
|
||
interface IContext { | ||
getComponent<T>(name: string, fallback: React.ComponentType<T>): React.ComponentType<T>; | ||
getIsAllowed(operation: Operation): boolean; | ||
} | ||
|
||
export const Context = createContext<IContext>({ | ||
getComponent: (name, fallback) => fallback, | ||
getIsAllowed: () => true, | ||
}); | ||
|
||
export const useCustomization = () => useContext(Context); | ||
|
||
interface IProps { | ||
children: React.ReactNode; | ||
value: IContext; | ||
} | ||
|
||
const CustomizationProvider = ({children, value}: IProps) => { | ||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
}; | ||
|
||
export default CustomizationProvider; |
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 {useCustomization} from './Customization.provider'; | ||
|
||
const withCustomization = <P extends object>(Component: React.ComponentType<P>, id: string) => { | ||
const WrappedComponent = (props: P) => { | ||
const {getComponent} = useCustomization(); | ||
const CustomizedComponent = getComponent(id, Component); | ||
|
||
return <CustomizedComponent {...props} />; | ||
}; | ||
|
||
return WrappedComponent; | ||
}; | ||
|
||
export default withCustomization; |
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,3 @@ | ||
// eslint-disable-next-line no-restricted-exports | ||
export {default, useCustomization, Operation} from './Customization.provider'; | ||
export {default as withCustomization} from './WithCustomization'; |