Last active
May 9, 2022 20:07
-
-
Save thanhtschoepe/d88cc41d8af6eab82741afd2ab30a1f0 to your computer and use it in GitHub Desktop.
How to write a React component
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
import { useQuery } from 'react-query' | |
import LoadingIndicator from '../core/components/LoadingIndicator | |
import ErrorMessage from '../core/components/ErrorMessage | |
import { Report } from '../types' | |
import { showTeamReport } from '../utils/ApiHelper.js' | |
// Note: Declare with interface, not type. | |
interface ReactTableProps { | |
teamId: string | |
} | |
// Note: Don't type using React.FC; that's deprecated. | |
const TeamReport = ({ teamId }: ReactTableProps) => { | |
const { isLoading, isError, error, data } = useQuery<Report>(['reports', teamId], async () => (await showTeamReport(teamId, reportId)).data) | |
if (isLoading) return <LoadingIndicator /> | |
if (isError) return <ErrorMessage error={error}/> | |
return (<div> | |
<h1>{data.title}</h1> | |
<p>Total expenses: {data.expenses_total}</p> | |
<p>Total distances: {data.kms_total}</p> | |
</div>) | |
} | |
export default TeamReport |
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
// https://www.typescriptlang.org/docs/handbook/interfaces.html | |
type AllowedAction = 'Unsubmit' | 'Delete' | 'Approve' | 'Reject' | 'Mark as Paid' // non-exhaustive list for demo only | |
interface Approver { | |
approver: "You" | string; | |
approved: boolean; | |
position: number; | |
approved_on: string; | |
} | |
interface Report { | |
commute_disallowance_enabled: boolean; | |
daily_disallowance: number; | |
date_range: string[] | null; | |
disallowance_amount: number; | |
disallowed_kms: number; | |
disallowed_miles: number; | |
employee_number: string | null; | |
expenses_total: number; | |
from_date: string | null; | |
is_locked: boolean; | |
is_public: boolean; | |
kms_total: number; | |
allowed_actions: IAllowedAction[]; | |
approvers: Approver[] | |
// ... additional properties | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment