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

Fixed type inference for AppContext #188

Merged
merged 2 commits into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 18 additions & 9 deletions src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ import '../utils/styles/global';
import 'normalize.css';
import Footer from '../components/Footer';
import { FormError } from '../components/Login/Login';
import { PackageInterface } from '../components/Package/Package';

export const AppContext = React.createContext<{}>({});
export const AppContextProvider = AppContext.Provider;
export const AppContextConsumer = AppContext.Consumer;

export interface AppStateInterface {
error?: FormError;
interface AppContextData {
logoUrl: string;
scope: string;
isUserLoggedIn: boolean;
packages: PackageInterface[];
user: {
username?: string;
};
scope: string;
}
export const AppContext = React.createContext<AppContextData>({
logoUrl: window.VERDACCIO_LOGO,
user: {},
scope: window.VERDACCIO_SCOPE || '',
isUserLoggedIn: false,
packages: [],
});
const AppContextProvider = AppContext.Provider;
export const AppContextConsumer = AppContext.Consumer;

export interface AppStateInterface extends AppContextData {
error?: FormError;
showLoginModal: boolean;
isUserLoggedIn: boolean;
packages: any[];
isLoading: boolean;
}
export default class App extends Component<{}, AppStateInterface> {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react';

import PackageList from '../../components/PackageList';
import { PackageInterface } from '../../components/Package/Package';

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

const Home: React.FC<Props> = ({ packages }) => (
Expand Down
9 changes: 4 additions & 5 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { Component, ReactElement } from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import { createBrowserHistory } from 'history';

import { AppContextConsumer, AppStateInterface } from './App/App';
import { AppContextConsumer } from './App/App';
import Header from './components/Header';

const history = createBrowserHistory({
Expand Down Expand Up @@ -44,7 +44,7 @@ class RouterApp extends Component<RouterAppProps> {

return (
<AppContextConsumer>
{function renderConsumerVersionPage({ logoUrl, scope = '', user }: Partial<AppStateInterface>) {
{function renderConsumerVersionPage({ logoUrl, scope = '', user }) {
return <Header logo={logoUrl} onLogout={onLogout} onToggleLoginModal={onToggleLoginModal} scope={scope} username={user && user.username} />;
}}
</AppContextConsumer>
Expand All @@ -54,8 +54,7 @@ class RouterApp extends Component<RouterAppProps> {
public renderHomePage = (): ReactElement<HTMLDivElement> => {
return (
<AppContextConsumer>
{function renderConsumerVersionPage({ isUserLoggedIn, packages }: Partial<AppStateInterface>) {
// @ts-ignore
{function renderConsumerVersionPage({ isUserLoggedIn, packages }) {
return <HomePage isUserLoggedIn={isUserLoggedIn} packages={packages} />;
}}
</AppContextConsumer>
Expand All @@ -65,7 +64,7 @@ class RouterApp extends Component<RouterAppProps> {
public renderVersionPage = (routerProps): ReactElement<HTMLDivElement> => {
return (
<AppContextConsumer>
{function renderConsumerVersionPage({ isUserLoggedIn }: Partial<AppStateInterface>) {
{function renderConsumerVersionPage({ isUserLoggedIn }) {
return <VersionPackage {...routerProps} isUserLoggedIn={isUserLoggedIn} />;
}}
</AppContextConsumer>
Expand Down