This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: version Page - Replaces class by func. (#171)
* refactor: updated version page * refactor: rollback context * fix: added version provider
- Loading branch information
1 parent
d69fc1b
commit f5c77ff
Showing
12 changed files
with
6,576 additions
and
216 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,24 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useContext } from 'react'; | ||
|
||
import { callDetailPage, callReadme } from '../../utils/calls'; | ||
import { buildScopePackage } from '../../utils/package'; | ||
import Loading from '../../components/Loading/Loading'; | ||
import NotFound from '../../components/NotFound'; | ||
|
||
import { Layout } from './Layout'; | ||
import { DetailContextProvider } from './context'; | ||
import { StateInterface } from './types'; | ||
import VersionLayout from './VersionLayout'; | ||
import { DetailContext } from './context'; | ||
|
||
export function getRouterPackageName(params): string { | ||
const packageName = params.package; | ||
const { scope } = params; | ||
if (scope) { | ||
return buildScopePackage(scope, packageName); | ||
} | ||
|
||
return packageName; | ||
} | ||
|
||
function fillTitle(text: string): string { | ||
return `Verdaccio - ${text}`; | ||
} | ||
const Version: React.FC = () => { | ||
const detailContext = useContext(DetailContext); | ||
const { isLoading, hasNotBeenFound } = detailContext; | ||
|
||
function isVersionValid(packageMeta, packageVersion): boolean { | ||
const hasVersion = typeof packageVersion !== 'undefined'; | ||
if (!hasVersion) { | ||
// if is undefined, that means versions does not exist, we continue | ||
return true; | ||
if (isLoading) { | ||
return <Loading />; | ||
} | ||
|
||
const hasMatchVersion = Object.keys(packageMeta.versions).includes(packageVersion); | ||
return hasMatchVersion; | ||
} | ||
|
||
const Version = ({ match: { params } }) => { | ||
const pkgName = getRouterPackageName(params); | ||
const [readMe, setReadme] = useState(); | ||
const [packageName, setPackageName] = useState(pkgName); | ||
// eslint-disable-next-line no-unused-vars | ||
const [packageVersion, setPackageVersion] = useState(params.version); | ||
const [packageMeta, setPackageMeta] = useState(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [notFound, setNotFound] = useState(false); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
const packageMeta = (await callDetailPage(packageName, packageVersion)) as Partial<StateInterface>; | ||
const readMe = (await callReadme(packageName, packageVersion)) as Partial<StateInterface>; | ||
if (isVersionValid(packageMeta, packageVersion)) { | ||
setReadme(readMe); | ||
setPackageMeta(packageMeta); | ||
setIsLoading(false); | ||
} else { | ||
setIsLoading(false); | ||
setNotFound(true); | ||
} | ||
} catch (error) { | ||
setNotFound(true); | ||
setIsLoading(false); | ||
} | ||
})(); | ||
}, [packageName, packageVersion]); | ||
|
||
useEffect(() => { | ||
if (!packageVersion) { | ||
document.title = fillTitle(packageName); | ||
} else { | ||
document.title = fillTitle(`${packageName}@${packageVersion}`); | ||
} | ||
}, [packageName, packageVersion]); | ||
|
||
useEffect(() => { | ||
const pkgName = getRouterPackageName(params); | ||
|
||
setPackageName(pkgName); | ||
setPackageVersion(params.version); | ||
}, [params, setPackageName, setPackageVersion]); | ||
|
||
const isNotFound = notFound || typeof packageMeta === 'undefined' || typeof packageName === 'undefined' || typeof readMe === 'undefined'; | ||
const renderContent = (): React.ReactElement<HTMLElement> => { | ||
if (isLoading) { | ||
return <Loading />; | ||
} else if (isNotFound) { | ||
return <NotFound />; | ||
} else { | ||
return <Layout />; | ||
} | ||
}; | ||
if (hasNotBeenFound) { | ||
return <NotFound />; | ||
} | ||
|
||
return ( | ||
<DetailContextProvider value={{ packageMeta, packageVersion, readMe, packageName, enableLoading: setIsLoading }}>{renderContent()}</DetailContextProvider> | ||
); | ||
return <VersionLayout />; | ||
}; | ||
|
||
export default Version; |
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,69 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
import { callDetailPage, callReadme } from '../../utils/calls'; | ||
|
||
import getRouterPackageName from './get-route-package-name'; | ||
import { DetailContext } from './context'; | ||
import isPackageVersionValid from './is-package-version-valid'; | ||
|
||
interface Params { | ||
scope?: string; | ||
package: string; | ||
version?: string; | ||
} | ||
|
||
const VersionContextProvider: React.FC = ({ children }) => { | ||
const { version, package: pkgName, scope } = useParams<Params>(); | ||
const [packageName, setPackageName] = useState(getRouterPackageName(pkgName, scope)); | ||
const [packageVersion, setPackageVersion] = useState(version); | ||
const [packageMeta, setPackageMeta] = useState(); | ||
const [readMe, setReadme] = useState(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [hasNotBeenFound, setHasNotBeenFound] = useState(); | ||
|
||
useEffect(() => { | ||
const updatedPackageName = getRouterPackageName(pkgName, scope); | ||
setPackageName(updatedPackageName); | ||
}, [pkgName, scope]); | ||
|
||
useEffect(() => { | ||
setPackageVersion(version); | ||
}, [version]); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
const packageMeta = await callDetailPage(packageName, packageVersion); | ||
const readMe = await callReadme(packageName, packageVersion); | ||
if (isPackageVersionValid(packageMeta, packageVersion)) { | ||
setReadme(readMe); | ||
setPackageMeta(packageMeta); | ||
setIsLoading(false); | ||
} else { | ||
setIsLoading(false); | ||
setHasNotBeenFound(true); | ||
} | ||
} catch (error) { | ||
setHasNotBeenFound(true); | ||
setIsLoading(false); | ||
} | ||
})(); | ||
}, [packageName, packageVersion]); | ||
|
||
return ( | ||
<DetailContext.Provider | ||
value={{ | ||
packageMeta, | ||
packageVersion, | ||
readMe, | ||
packageName, | ||
isLoading, | ||
hasNotBeenFound, | ||
}}> | ||
{children} | ||
</DetailContext.Provider> | ||
); | ||
}; | ||
|
||
export default VersionContextProvider; |
18 changes: 5 additions & 13 deletions
18
src/pages/Version/Layout.tsx → src/pages/Version/VersionLayout.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 |
---|---|---|
@@ -1,28 +1,20 @@ | ||
import React, { FC, ReactElement } from 'react'; | ||
import React from 'react'; | ||
import Grid from '@material-ui/core/Grid'; | ||
|
||
import DetailContainer from '../../components/DetailContainer'; | ||
import DetailSidebar from '../../components/DetailSidebar'; | ||
|
||
function renderDetail(): ReactElement<HTMLElement> { | ||
return <DetailContainer />; | ||
} | ||
|
||
function renderSidebar(): ReactElement<HTMLElement> { | ||
return <DetailSidebar />; | ||
} | ||
|
||
const Layout: FC<{}> = () => { | ||
const VersionLayout: React.FC = () => { | ||
return ( | ||
<Grid className={'container content'} container={true} data-testid="version-layout" spacing={0}> | ||
<Grid item={true} md={8} xs={12}> | ||
{renderDetail()} | ||
<DetailContainer /> | ||
</Grid> | ||
<Grid item={true} md={4} xs={12}> | ||
{renderSidebar()} | ||
<DetailSidebar /> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export { Layout }; | ||
export default VersionLayout; |
Oops, something went wrong.