Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

fix: package list refresh based on logged-in user #415

Merged
merged 4 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 13 additions & 37 deletions src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { Router } from 'react-router-dom';

import storage from '../utils/storage';
import { isTokenExpire } from '../utils/login';
import API from '../utils/api';
import Header from '../components/Header';
import Footer from '../components/Footer';
import Box from '../muiComponents/Box';
import Loading from '../components/Loading';
import StyleBaseline from '../design-tokens/StyleBaseline';
import { Theme } from '../design-tokens/theme';

Expand All @@ -33,11 +31,9 @@ const StyledBoxContent = styled(Box)<{ theme?: Theme }>(({ theme }) => ({
/* eslint-disable react-hooks/exhaustive-deps */
const App: React.FC = () => {
const [user, setUser] = useState();
const [packages, setPackages] = useState([]);
const [isLoading, setIsLoading] = useState(true);

/**
* Logouts user
* Logout user
* Required by: <Header />
*/
const logout = () => {
Expand All @@ -59,46 +55,26 @@ const App: React.FC = () => {
setUser({ username });
};

const loadOnHandler = async () => {
try {
const packages = await API.request('packages', 'GET');
// FIXME add correct type for package
setPackages(packages as never[]);
} catch (error) {
// FIXME: add dialog
console.error({
title: 'Warning',
message: `Unable to load package list: ${error.message}`,
});
}

setIsLoading(false);
};

useEffect(() => {
checkUserAlreadyLoggedIn();
loadOnHandler();
}, []);

return (
<>
<StyleBaseline />
<StyledBox display="flex" flexDirection="column" height="100%">
{isLoading ? (
<Loading />
) : (
<>
<Router history={history}>
<AppContextProvider packages={packages} user={user}>
<Header />
<StyledBoxContent flexGrow={1}>
<AppRoute />
</StyledBoxContent>
</AppContextProvider>
</Router>
<Footer />
</>
)}
<>
<Router history={history}>
<AppContextProvider user={user}>
<Header />
<StyledBoxContent flexGrow={1}>
{/* eslint-disable-next-line react/jsx-max-depth */}
<AppRoute />
</StyledBoxContent>
</AppContextProvider>
</Router>
<Footer />
</>
</StyledBox>
</>
);
Expand Down
1 change: 0 additions & 1 deletion src/App/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createContext } from 'react';
export interface AppProps {
user?: User;
scope: string;
packages: any[];
}

export interface User {
Expand Down
11 changes: 1 addition & 10 deletions src/App/AppContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import React, { useState, useEffect } from 'react';
import AppContext, { AppProps, User } from './AppContext';

interface Props {
packages: any[];
user?: User;
}

/* eslint-disable react-hooks/exhaustive-deps */
const AppContextProvider: React.FC<Props> = ({ children, packages, user }) => {
const AppContextProvider: React.FC<Props> = ({ children, user }) => {
const [state, setState] = useState<AppProps>({
scope: window.VERDACCIO_SCOPE || '',
packages,
user,
});

Expand All @@ -22,13 +20,6 @@ const AppContextProvider: React.FC<Props> = ({ children, packages, user }) => {
});
}, [user]);

useEffect(() => {
setState({
...state,
packages,
});
}, [packages]);

const setUser = (user?: User) => {
setState({
...state,
Expand Down
4 changes: 2 additions & 2 deletions src/App/AppRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AppRoute: React.FC = () => {
throw Error('The app Context was not correct used');
}

const { user, packages } = appContext;
const { user } = appContext;

const isUserLoggedIn = user && user.username;

Expand All @@ -39,7 +39,7 @@ const AppRoute: React.FC = () => {
<Suspense fallback={<Loading />}>
<Switch>
<ReactRouterDomRoute exact={true} path={Route.ROOT}>
<HomePage isUserLoggedIn={!!isUserLoggedIn} packages={packages || []} />
<HomePage isUserLoggedIn={!!isUserLoggedIn} />
</ReactRouterDomRoute>
<ReactRouterDomRoute exact={true} path={Route.PACKAGE}>
<VersionContextProvider>
Expand Down
14 changes: 7 additions & 7 deletions src/components/Header/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('<Header /> component with logged in state', () => {
test('should load the component in logged out state', () => {
const { container, queryByTestId, getByText } = render(
<Router>
<AppContextProvider packages={props.packages}>
<AppContextProvider>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -32,7 +32,7 @@ describe('<Header /> component with logged in state', () => {
test('should load the component in logged in state', () => {
const { container, getByTestId, queryByText } = render(
<Router>
<AppContextProvider packages={props.packages} user={props.user}>
<AppContextProvider user={props.user}>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -46,7 +46,7 @@ describe('<Header /> component with logged in state', () => {
test('should open login dialog', async () => {
const { getByText } = render(
<Router>
<AppContextProvider packages={props.packages}>
<AppContextProvider>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -61,7 +61,7 @@ describe('<Header /> component with logged in state', () => {
test('should logout the user', async () => {
const { getByText, getByTestId } = render(
<Router>
<AppContextProvider packages={props.packages} user={props.user}>
<AppContextProvider user={props.user}>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -79,7 +79,7 @@ describe('<Header /> component with logged in state', () => {
test("The question icon should open a new tab of verdaccio's website - installation doc", () => {
const { getByTestId } = render(
<Router>
<AppContextProvider packages={props.packages} user={props.user}>
<AppContextProvider user={props.user}>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -92,7 +92,7 @@ describe('<Header /> component with logged in state', () => {
test('should open the registrationInfo modal when clicking on the info icon', async () => {
const { getByTestId } = render(
<Router>
<AppContextProvider packages={props.packages} user={props.user}>
<AppContextProvider user={props.user}>
<Header />
</AppContextProvider>
</Router>
Expand All @@ -109,7 +109,7 @@ describe('<Header /> component with logged in state', () => {
test('should close the registrationInfo modal when clicking on the button close', async () => {
const { getByTestId, getByText, queryByTestId } = render(
<Router>
<AppContextProvider packages={props.packages} user={props.user}>
<AppContextProvider user={props.user}>
<Header />
</AppContextProvider>
</Router>
Expand Down
1 change: 0 additions & 1 deletion src/components/LoginDialog/LoginDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import LoginDialog from './LoginDialog';

const appContextValue: AppContextProps = {
scope: '',
packages: [],
setUser: jest.fn(),
};

Expand Down
34 changes: 26 additions & 8 deletions src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import { PackageList } from '../../components/PackageList';
import { PackageInterface } from '../../components/Package/Package';
import API from '../../utils/api';
import Loading from '../../components/Loading';

interface Props {
isUserLoggedIn: boolean;
packages: PackageInterface[];
}

const Home: React.FC<Props> = ({ packages }) => (
<div className="container content">
<PackageList packages={packages} />
</div>
);
const Home: React.FC<Props> = ({ isUserLoggedIn }) => {
const [packages, setPackages] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const loadPackages = async () => {
try {
const packages = await API.request('packages', 'GET');
// FIXME add correct type for package
setPackages(packages as never[]);
} catch (error) {
// FIXME: add dialog
console.error({
title: 'Warning',
message: `Unable to load package list: ${error.message}`,
});
}
setIsLoading(false);
};
useEffect(() => {
loadPackages().then();
}, [isUserLoggedIn]);

return <div className="container content">{isLoading ? <Loading /> : <PackageList packages={packages} />}</div>;
};

export default Home;