Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
fix(stuff): fix types for with redux HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jan 30, 2019
1 parent 77e6922 commit eef71d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
35 changes: 15 additions & 20 deletions front/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
import App, { Container, NextAppContext } from 'next/app'
import Head from 'next/head'
import React, { Component as ReactComponent } from 'react'
import React from 'react'
import { Provider } from 'react-redux'
import { Option } from 'tsoption'

import { AppContext } from '@front/domain/AppContext'
import { Store } from '@front/domain/store/Store'
import { WithReduxProps } from '@front/domain/store/WithReduxProps'
import { withReduxStore } from '@front/domain/store/withReduxStore'

interface Props {
reduxStore: Store
pageProps: any
Component: ReactComponent
}

class OncohelpWeb extends App<Props> {
public static getInitialProps(context: NextAppContext) {
const ctx: AppContext = context.ctx as any
class CheckmoneyWeb extends App<WithReduxProps> {
public static getInitialProps(appContext: NextAppContext) {
const ctx: AppContext = appContext.ctx as any

if (ctx.req && ctx.req.cookies) {
// TODO: option
const token: string | undefined = ctx.req.cookies.token
const token = Option.of(ctx)
.map(context => context.req)
.map(request => request.cookies)
.map(cookies => cookies.token)

if (token) {
// TODO:
// ctx.reduxStore.dispatch(setToken(token))
}
if (token.nonEmpty()) {
// TODO:
// ctx.reduxStore.dispatch(setToken(token))
}

return App.getInitialProps(context)
return App.getInitialProps(appContext)
}

public render() {
Expand All @@ -46,4 +41,4 @@ class OncohelpWeb extends App<Props> {
}
}

export default withReduxStore(OncohelpWeb)
export default withReduxStore(CheckmoneyWeb)
5 changes: 5 additions & 0 deletions front/src/domain/store/WithReduxProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Store } from './Store'

export interface WithReduxProps {
reduxStore: Store
}
14 changes: 9 additions & 5 deletions front/src/domain/store/withReduxStore.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react'
import App, { AppProps, DefaultAppIProps, NextAppContext } from 'next/app'
import React, { ComponentType } from 'react'

import { AppContext } from '../AppContext'
import { getOrCreateStore } from './getOrCreateStore'
import { Store } from './Store'
import { WithReduxProps } from './WithReduxProps'

import { NextAppContext } from 'next/app'
type NextProps = AppProps & DefaultAppIProps

export const withReduxStore = (App: any) => {
return class AppWithRedux extends React.Component {
export const withReduxStore = (
Application: ComponentType<NextProps & WithReduxProps>,
) => {
return class AppWithRedux extends React.Component<NextProps> {
public static async getInitialProps(appContext: NextAppContext) {
const reduxStore = getOrCreateStore()
const context: AppContext = appContext.ctx as any
Expand All @@ -33,7 +37,7 @@ export const withReduxStore = (App: any) => {
}

public render() {
return <App {...this.props} reduxStore={this.reduxStore} />
return <Application {...this.props} reduxStore={this.reduxStore} />
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"circular": "madge --circular --extensions ts ./ --tsConfig tsconfig.madge.json"
},
"dependencies": {
"tsconfig-paths": "^3.7.0"
"tsconfig-paths": "^3.7.0",
"tsoption": "^0.7.0"
},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
Expand Down

0 comments on commit eef71d7

Please sign in to comment.