Last active
January 11, 2017 03:41
-
-
Save cqfd/642f307097b6f174b2c85d1dba8c2f58 to your computer and use it in GitHub Desktop.
Admin-only React components with Flow
This file contains hidden or 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
// @flow | |
import React from 'react' | |
import type { AdminProof } from './User' | |
export default function AdminOnly(props: { proof: AdminProof }) { | |
return <p>Secret admin stuff!</p> | |
} |
This file contains hidden or 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
// @flow | |
import React from 'react' | |
import AdminOnly from './AdminOnly' | |
import { checkIfAdmin } from './User' | |
function App() { | |
const currentUser = ... | |
const proof = checkIfAdmin(currentUser) | |
if (proof != null) { | |
return <AdminOnly proof={proof} /> | |
} else { | |
return <p>Nothing to see here...</p> | |
} | |
} |
This file contains hidden or 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
// @flow | |
class _AdminProof {} | |
export type AdminProof = _AdminProof | |
export function checkIfAdmin(user: { isAdmin: bool }): ?AdminProof { | |
if (user.isAdmin) { | |
return new _AdminProof | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment