Browse Source

fixed flowtype errors with redux

pull/1/head
Arwed Mett 4 years ago
parent
commit
5988bc8869
Signed by: Pfeifenjoy
GPG Key ID: 86943827297DA9FC
  1. 16
      src/action/action.js
  2. 2
      src/action/index.js
  3. 14
      src/action/message.js
  4. 11
      src/reducer/index.js
  5. 19
      src/reducer/message.js
  6. 4
      src/render/index.js
  7. 8
      src/store/index.js
  8. 15
      src/store/middleware.js

16
src/action/action.js

@ -1,16 +0,0 @@
//@flow
export type async_action_type = "SEND_MESSAGE"
export type static_action_type = "SEND_MESSAGE"
export type AsyncAction = {|
type: async_action_type,
payload: Promise<*>
|}
export type StaticAction<T> = {|
type: static_action_type,
payload: T
|}
export type Action<T> = AsyncAction | StaticAction<T>

2
src/action/index.js

@ -1,4 +1,4 @@
//@flow
export { send_message } from "./message"
export type * from "./action"
export type { Action } from "./message"

14
src/action/message.js

@ -1,6 +1,16 @@
//@flow
import type { AsyncAction } from "./action"
export type Action = {
type: "SEND_MESSAGE"
} | {
type: "SEND_MESSAGE_PENDING"
} | {
type: "SEND_MESSAGE_FULFILLED",
payload: Response
} | {
type: "SEND_MESSAGE_REJECTED",
payload: Error
}
export type message_t = {
email: string;
@ -8,7 +18,7 @@ export type message_t = {
text: string;
};
export const send_message = (payload: message_t): AsyncAction => ({
export const send_message = (payload: message_t): Action => ({
type: "SEND_MESSAGE",
payload: fetch("api/message/create", {
method: "POST",

11
src/reducer/index.js

@ -1,6 +1,15 @@
//@flow
import { combineReducers } from "redux"
import type { Reducer } from "redux"
import message from "./message"
import type { Action } from "../action"
import type { State as MessageState } from "./message"
export default combineReducers({ message })
export type State = {
message: MessageState
}
const reducers: Reducer<State, Action> = combineReducers({ message })
export default reducers

19
src/reducer/message.js

@ -1,20 +1,13 @@
//@flow
type state_t = {
state: "IDLE" | "PENDING" | "FULFILLED" | "FAIL"
}
import type { Action } from "../action/message"
import type { Reducer } from "redux"
type action_t = {
type: "SEND_MESSAGE_PENDING"
} | {
type: "SEND_MESSAGE_FULFILLED",
payload: Response
} | {
type: "SEND_MESSAGE_REJECTED",
payload: Error
export type State = {
state: "IDLE" | "PENDING" | "FULFILLED" | "REJECTED"
}
export default (_: state_t, action: action_t) => {
const message: Reducer<State, Action> = (_, action: Action): State => {
switch(action.type) {
case "SEND_MESSAGE_PENDING":
return { state: "PENDING" }
@ -30,3 +23,5 @@ export default (_: state_t, action: action_t) => {
return { state: "IDLE" }
}
}
export default message

4
src/render/index.js

@ -1,7 +1,7 @@
//@flow
import React from "react"
import type { StaticAction } from "../action"
import type { Action } from "../action"
import createStore from "../store"
import { renderToString } from "react-dom/server"
import { Provider } from "react-redux"
@ -14,7 +14,7 @@ import style from "../style"
export const template = ejs.compile(index_file)
export const title = "Arwed Mett"
export default (actions: Array<StaticAction<*>>) => {
export default (actions: Array<Action>) => {
const store = createStore(actions)
const content = renderToString(

8
src/store/index.js

@ -1,12 +1,14 @@
//@flow
import { createStore } from "redux"
import type { Store } from "redux"
import reducers from "../reducer"
import middleware from "./middleware"
import type { StaticAction } from "../action"
import type { Action } from "../action"
import type { State } from "../reducer"
export default (actions: Array<StaticAction<*>> = [ ]) => {
const store = createStore(reducers, middleware())
export default (actions: Array<Action> = [ ]) => {
const store: Store<State, Action> = createStore(reducers, middleware())
for(const action of actions) {
store.dispatch(action)
}

15
src/store/middleware.js

@ -1,15 +1,14 @@
//@flow
import { applyMiddleware } from "redux"
import type { StoreEnhancer } from "redux"
import thunk from "redux-thunk"
import logger from "redux-logger"
import promise from "redux-promise-middleware"
import type { State } from "../reducer"
import type { Action } from "../action"
export default () => {
if(process.env.NODE_ENV !== "production") {
return applyMiddleware(promise, thunk, logger)
} else {
return applyMiddleware(promise, thunk)
}
}
export default (): StoreEnhancer<State, Action> =>
process.env.NODE_ENV !== "production" ?
applyMiddleware(promise, thunk) :
applyMiddleware(promise, thunk, logger)

Loading…
Cancel
Save