-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95cf655
commit ff06ccf
Showing
9 changed files
with
118 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { FC, useContext, useEffect, useState } from 'react' | ||
|
||
import { ErrorContext } from '../hooks/error' | ||
import { ReloadContext } from '../hooks/dev' | ||
import { request } from '../tools' | ||
import { DefaultLoading } from '../DefaultLoading' | ||
import { ConfigContext } from '../hooks/config' | ||
|
||
import { AnyComp, FastProps } from './index' | ||
|
||
export interface ServerLoadProps { | ||
type: 'ServerLoad' | ||
url: string | ||
} | ||
|
||
export const ServerLoadComp: FC<ServerLoadProps> = ({ url }) => { | ||
const [componentProps, setComponentProps] = useState<FastProps | null>(null) | ||
|
||
const { error, setError } = useContext(ErrorContext) | ||
const reloadValue = useContext(ReloadContext) | ||
const { rootUrl, pathSendMode, Loading } = useContext(ConfigContext) | ||
|
||
useEffect(() => { | ||
// setViewData(null) | ||
let fetchUrl = rootUrl | ||
if (pathSendMode === 'query') { | ||
fetchUrl += `?path=${encodeURIComponent(url)}` | ||
} else { | ||
fetchUrl += url | ||
} | ||
|
||
const promise = request({ url: fetchUrl }) | ||
|
||
promise | ||
.then(([, data]) => setComponentProps(data as FastProps)) | ||
.catch((e) => { | ||
setError({ title: 'Request Error', description: e.message }) | ||
}) | ||
return () => { | ||
promise.then(() => null) | ||
} | ||
}, [rootUrl, pathSendMode, url, setError, reloadValue]) | ||
|
||
if (componentProps === null) { | ||
if (error) { | ||
return <></> | ||
} else if (Loading) { | ||
return <Loading /> | ||
} else { | ||
return <DefaultLoading /> | ||
} | ||
} else { | ||
return <AnyComp {...componentProps} /> | ||
} | ||
} |
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,51 +1,10 @@ | ||
import { useContext, useEffect, useState } from 'react' | ||
import { useContext } from 'react' | ||
|
||
import type { FastUIProps } from './index' | ||
|
||
import { FastProps, AnyComp } from './components' | ||
import { DefaultLoading } from './DefaultLoading' | ||
import { LocationContext } from './hooks/locationContext' | ||
import { ErrorContext } from './hooks/error' | ||
import { request } from './tools' | ||
import { ReloadContext } from './hooks/dev' | ||
|
||
type Props = Omit<FastUIProps, 'defaultClassName' | 'OnError' | 'customRender'> | ||
import { ServerLoadComp } from './components/ServerLoad' | ||
|
||
export function FastUIController({ rootUrl, pathSendMode, loading }: Props) { | ||
const [componentProps, setComponentProps] = useState<FastProps | null>(null) | ||
export function FastUIController() { | ||
const { fullPath } = useContext(LocationContext) | ||
|
||
const { error, setError } = useContext(ErrorContext) | ||
const reloadValue = useContext(ReloadContext) | ||
|
||
useEffect(() => { | ||
// setViewData(null) | ||
let url = rootUrl | ||
if (pathSendMode === 'query') { | ||
url += `?path=${encodeURIComponent(fullPath)}` | ||
} else { | ||
url += fullPath | ||
} | ||
|
||
const promise = request({ url }) | ||
|
||
promise | ||
.then(([, data]) => setComponentProps(data as FastProps)) | ||
.catch((e) => { | ||
setError({ title: 'Request Error', description: e.message }) | ||
}) | ||
return () => { | ||
promise.then(() => null).catch(() => null) | ||
} | ||
}, [rootUrl, pathSendMode, fullPath, setError, reloadValue]) | ||
|
||
if (componentProps === null) { | ||
if (error) { | ||
return <></> | ||
} else { | ||
return <>{loading ? loading() : <DefaultLoading />}</> | ||
} | ||
} else { | ||
return <AnyComp {...componentProps} /> | ||
} | ||
return <ServerLoadComp type="ServerLoad" url={fullPath} /> | ||
} |
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,23 @@ | ||
import { createContext, FC, useContext } from 'react' | ||
|
||
import type { CustomRender } from '../index' | ||
|
||
import { FastProps } from '../components' | ||
|
||
interface Config { | ||
rootUrl: string | ||
// defaults to 'append' | ||
pathSendMode?: 'append' | 'query' | ||
customRender?: CustomRender | ||
Loading?: FC | ||
} | ||
|
||
export const ConfigContext = createContext<Config>({ rootUrl: '' }) | ||
|
||
export const useCustomRender = (props: FastProps): FC | void => { | ||
const { customRender } = useContext(ConfigContext) | ||
|
||
if (customRender) { | ||
return customRender(props) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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