Skip to content
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
4 changes: 1 addition & 3 deletions components/molecules/PageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Paper, Typography } from "@material-ui/core"
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
import React from "react"
import { useSelector } from "react-redux"
import { IInitialState } from "../../store/states"
import { selectedPageSelector } from "../../store/page"

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand All @@ -26,8 +26,6 @@ const useStyles = makeStyles((theme: Theme) =>

interface IProps {}

const selectedPageSelector = (state: IInitialState) => state.page.selectedPage

/**
* Page header component
* @param props IProps
Expand Down
4 changes: 1 addition & 3 deletions components/organisms/ResponsiveDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
import MenuIcon from "@material-ui/icons/Menu"
import React, { useState } from "react"
import { useSelector } from "react-redux"
import { IInitialState } from "../../store/states"
import { selectedPageSelector } from "../../store/page"
import { Sidenavi } from "../organisms"

const drawerWidth = 290
Expand Down Expand Up @@ -51,8 +51,6 @@ interface IProps {
children: React.ReactNode
}

const selectedPageSelector = (state: IInitialState) => state.page.selectedPage

/**
* Responsive drawer
* @see https://material-ui.com/demos/drawers/#responsive-drawer
Expand Down
5 changes: 1 addition & 4 deletions components/organisms/Sidenavi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
import SvgIcon from "@material-ui/core/SvgIcon"
import { useDispatch, useSelector } from "react-redux"
import { Page, SiteInfo } from "../../constants"
import { PageActions } from "../../store/actions"
import { IInitialState } from "../../store/states"
import { PageActions, selectedPageSelector } from "../../store/page"
import { NextListItem } from "../molecules"

const useStyles = makeStyles((theme: Theme) =>
Expand Down Expand Up @@ -47,8 +46,6 @@ const useStyles = makeStyles((theme: Theme) =>

interface IProps {}

const selectedPageSelector = (state: IInitialState) => state.page.selectedPage

/**
* Side navigation component
* @param props IProps
Expand Down
4 changes: 1 addition & 3 deletions components/templates/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createStyles, makeStyles, Theme } from "@material-ui/core/styles"
import Head from "next/head"
import * as React from "react"
import { useSelector } from "react-redux"
import { IInitialState } from "../../store/states"
import { selectedPageSelector } from "../../store/page"
import { ResponsiveDrawer } from "../organisms"

const useStyles = makeStyles((theme: Theme) =>
Expand All @@ -17,8 +17,6 @@ interface IProps {
children: React.ReactNode
}

const selectedPageSelector = (state: IInitialState) => state.page.selectedPage

export const Layout = function(props: IProps) {
const { children } = props
const classes = useStyles(props)
Expand Down
2 changes: 1 addition & 1 deletion pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SpacingPaper } from "../components/atoms"
import { HeaderArticleContainer } from "../components/organisms"
import { Layout } from "../components/templates"
import { Page } from "../constants"
import { IPagePayload, PageActions } from "../store/actions"
import { IPagePayload, PageActions } from "../store/page"

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand Down
6 changes: 2 additions & 4 deletions pages/redux.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { SpacingPaper } from "../components/atoms"
import { HeaderArticleContainer } from "../components/organisms"
import { Layout } from "../components/templates"
import { Page } from "../constants"
import { CounterActions, IPagePayload, PageActions } from "../store/actions"
import { IInitialState } from "../store/states"
import { CounterActions, countSelector } from "../store/counter"
import { IPagePayload, PageActions } from "../store/page"

const useStyles = makeStyles((theme: Theme) =>
createStyles({
Expand All @@ -29,8 +29,6 @@ const useStyles = makeStyles((theme: Theme) =>
})
)

const countSelector = (state: IInitialState) => state.counter.count

function Redux() {
const classes = useStyles({})
const dispatch = useDispatch()
Expand Down
2 changes: 0 additions & 2 deletions store/actions/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions store/actions/CounterAction.ts → store/counter/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export interface ICounterPayload {
}

export const CounterActions = {
// no arguments
increment: actionCreator("increment"),
decrement: actionCreator("decrement"),
// with arguments
calculate: actionCreator<ICounterPayload>("calculate"),
}
3 changes: 3 additions & 0 deletions store/counter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./actions"
export * from "./selectors"
export * from "./states"
38 changes: 38 additions & 0 deletions store/counter/reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import produce from "immer"
import { reducerWithInitialState } from "typescript-fsa-reducers"
import {
CounterActions,
CounterInitialState,
ICounterPayload,
ICounterState,
} from "."

export default reducerWithInitialState(CounterInitialState)
.case(
CounterActions.increment,
(state: Readonly<ICounterState>): ICounterState => {
return produce(state, (draft: ICounterState) => {
draft.count = state.count + 1
})
}
)
.case(
CounterActions.decrement,
(state: Readonly<ICounterState>): ICounterState => {
return produce(state, (draft: ICounterState) => {
draft.count = state.count - 1
})
}
)
.case(
CounterActions.calculate,
(
state: Readonly<ICounterState>,
payload: ICounterPayload
): ICounterState => {
const { inputNumber } = payload
return produce(state, (draft: ICounterState) => {
draft.count = state.count + inputNumber
})
}
)
3 changes: 3 additions & 0 deletions store/counter/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { IInitialState } from "../states"

export const countSelector = (state: IInitialState) => state.counter.count
9 changes: 9 additions & 0 deletions store/counter/states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Counter
*/
export interface ICounterState {
count: number
}
export const CounterInitialState: ICounterState = {
count: 1,
}
File renamed without changes.
3 changes: 3 additions & 0 deletions store/page/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./actions"
export * from "./selectors"
export * from "./states"
15 changes: 15 additions & 0 deletions store/page/reducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import produce from "immer"
import { reducerWithInitialState } from "typescript-fsa-reducers"
import { IPagePayload, IPageState, PageActions, PageInitialState } from "."

export default reducerWithInitialState(PageInitialState).case(
PageActions.changePage,
(
state: Readonly<IPageState>,
payload: Readonly<IPagePayload>
): IPageState => {
return produce(state, (draft: IPageState) => {
draft.selectedPage = payload.selectedPage
})
}
)
4 changes: 4 additions & 0 deletions store/page/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IInitialState } from "../states"

export const selectedPageSelector = (state: IInitialState) =>
state.page.selectedPage
11 changes: 11 additions & 0 deletions store/page/states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Page } from "../../constants"

/**
* Page
*/
export interface IPageState {
selectedPage: Page
}
export const PageInitialState: IPageState = {
selectedPage: Page.TOP,
}
4 changes: 2 additions & 2 deletions store/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { combineReducers } from "redux"
import { countReducers } from "./reducers/CounterReducer"
import { pageReducers } from "./reducers/PageReducer"
import countReducers from "./counter/reducers"
import pageReducers from "./page/reducers"
import { IInitialState } from "./states"

export const combinedReducers = combineReducers<IInitialState>({
Expand Down
31 changes: 0 additions & 31 deletions store/reducers/CounterReducer.ts

This file was deleted.

13 changes: 0 additions & 13 deletions store/reducers/PageReducer.ts

This file was deleted.

2 changes: 0 additions & 2 deletions store/reducers/index.ts

This file was deleted.

39 changes: 4 additions & 35 deletions store/states.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
import { Page } from "../constants"

/**
* Counter
*/
export interface ICounterState {
count: number
}
export const CounterInitialState: ICounterState = {
count: 1,
}

/**
* Page
*/
export interface IPageState {
selectedPage: Page
}
export const PageInitialState: IPageState = {
selectedPage: Page.TOP,
}
import { CounterInitialState, ICounterState } from "./counter"
import { IPageState, PageInitialState } from "./page"

/**
* Initial state tree interface
*/
export interface IInitialState {
/**
* Root counter state
*/
counter: ICounterState
/**
* Root page state
*/
page: IPageState
counter: Readonly<ICounterState>
page: Readonly<IPageState>
}

/**
* Initial state tree
*/
export const InitialState: IInitialState = {
/**
* Root counter state
*/
counter: CounterInitialState,
/**
* Root page state
*/
page: PageInitialState,
}