-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(UNI-221): refactored to use hoc to simplify component code
- Loading branch information
Showing
9 changed files
with
97 additions
and
30 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
File renamed without changes.
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,35 @@ | ||
import React from "react"; | ||
|
||
interface ErrorBoundaryProps { | ||
fallback: React.ReactElement; | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
hasError: boolean; | ||
} | ||
|
||
export default class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
|
||
this.state = {hasError: false}; | ||
} | ||
|
||
static getDerivedStateFromError(_: Error): ErrorBoundaryState { | ||
// 更新 state 使得下一次渲染能够显示降级后的 UI | ||
return {hasError: true} | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { | ||
// 你同样可以将错误日志上报给服务器 | ||
console.error('Uncaught error:', error, errorInfo); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return this.props.fallback; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} |
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,34 +1,16 @@ | ||
import {View} from "@tarojs/components"; | ||
import CalendarHeatMap from "@/components/calendar-heat-map"; | ||
import {useQuery} from "@apollo/client"; | ||
import {ErrorDisplay} from "@/components/ErrorDisplay"; | ||
import withGraphqlQuery from "@/components/higher-orders/with-graphql-query"; | ||
import {GITHUB_STATS} from "@/api/github"; | ||
|
||
|
||
const GitHubCalendar = () => { | ||
const {loading, error, data} = useQuery(GITHUB_STATS) | ||
|
||
if (error) { | ||
return <ErrorDisplay error={error}>发生了错误!</ErrorDisplay> | ||
} | ||
|
||
if (loading) { | ||
return <View> | ||
<CalendarHeatMap data={[]} showMonthLabel showWeekLabel theme='github' /> | ||
</View> | ||
} | ||
|
||
const contributions = data.viewer.contributionsCollection.contributionCalendar.weeks.map(week => week.contributionDays.map(day => ({ | ||
const GitHubCalendar = ({data}) => { | ||
const contributions = data?.viewer?.contributionsCollection?.contributionCalendar?.weeks?.map(week => | ||
week.contributionDays.map(day => ({ | ||
date: day.date, | ||
count: day.contributionCount | ||
})) | ||
).flat(); | ||
).flat() ?? []; | ||
|
||
return ( | ||
<View> | ||
<CalendarHeatMap data={contributions} showMonthLabel showWeekLabel theme='github' /> | ||
</View> | ||
) | ||
return <CalendarHeatMap data={contributions} showMonthLabel showWeekLabel theme='github' /> | ||
} | ||
|
||
export default GitHubCalendar | ||
export default withGraphqlQuery(GitHubCalendar, GITHUB_STATS); |
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,50 @@ | ||
import {DocumentNode, useQuery} from "@apollo/client"; | ||
import useComponentRefresh from "@/hooks/use-component-refresh"; | ||
import {ErrorDisplay} from "@/components/errors/ErrorDisplay"; | ||
import React from "react"; | ||
import ErrorBoundary from "@/components/errors/error-boundary"; | ||
|
||
interface WithGraphqlQueryProps<TData> { | ||
data: TData | null | undefined; | ||
} | ||
|
||
interface HOCProps<TData> extends Omit<WithGraphqlQueryProps<TData>, 'data'> { | ||
query: DocumentNode; | ||
LoadingComponent?: React.ComponentType; | ||
ErrorComponent?: React.ComponentType<{ error: Error, refetch: () => void }>; | ||
} | ||
|
||
const DefaultLoadingComponent: React.FC = () => <div>loading...</div>; | ||
const DefaultErrorComponent: React.FC<{ error: Error, refetch: () => void }> = ({error, refetch}) => ( | ||
<ErrorDisplay error={error} refetch={refetch}>发生了错误!</ErrorDisplay>); | ||
|
||
/** | ||
* A higher order component that wraps a component with a graphql query. | ||
* The component to wrap, all it needs to do is handle the data prop. error and such are handled by this HOC. | ||
* @param WrappedComponent | ||
* @param query | ||
*/ | ||
function withGraphqlQuery<TData = any>(WrappedComponent: React.ComponentType<WithGraphqlQueryProps<TData>>, query: DocumentNode) { | ||
const HOC: React.FC<HOCProps<TData>> = ({LoadingComponent, ErrorComponent, ...rest}) => { | ||
const {loading, data, error, refetch} = useQuery<TData>(query); | ||
useComponentRefresh(refetch); | ||
|
||
if (error) { | ||
const ErrorComponentToRender = ErrorComponent || DefaultErrorComponent; | ||
return <ErrorComponentToRender error={error} refetch={refetch} /> | ||
} | ||
|
||
if (loading || !data) { | ||
const LoadingComponentToRender = LoadingComponent || DefaultLoadingComponent; | ||
return <ErrorBoundary fallback={<LoadingComponentToRender />}> | ||
<WrappedComponent data={undefined} {...rest} /> | ||
</ErrorBoundary> | ||
} | ||
|
||
return <WrappedComponent data={data} {...rest} /> | ||
} | ||
|
||
return HOC; | ||
} | ||
|
||
export default withGraphqlQuery; |
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
73937cd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
weapp – ./
weapp-jeff-tians-projects.vercel.app
weapp-git-main-jeff-tians-projects.vercel.app
weapp-eight.vercel.app
taro.jefftian.dev