diff options
| author | Noah Loomans <noahloomans@gmail.com> | 2017-12-21 13:10:05 +0100 | 
|---|---|---|
| committer | Noah Loomans <noahloomans@gmail.com> | 2017-12-21 13:10:05 +0100 | 
| commit | 4ca30295d7d9f3dd7ba2e105952ff627f6b702a4 (patch) | |
| tree | 48285ecb1957171ebb9b0106aaf2d1a184972d0e /src/client/react | |
| parent | f0c8cf0e79f003514fd65a70def5820205955a77 (diff) | |
Add strict typing
Except for functions because of https://github.com/reactjs/redux/issues/2709
Diffstat (limited to 'src/client/react')
| -rw-r--r-- | src/client/react/components/container/Search.tsx | 27 | ||||
| -rw-r--r-- | src/client/react/components/presentational/IconFromUserType.tsx | 2 | ||||
| -rw-r--r-- | src/client/react/reducers.ts | 1 | ||||
| -rw-r--r-- | src/client/react/reducers/search.ts | 2 | ||||
| -rw-r--r-- | src/client/react/users.ts | 23 | 
5 files changed, 26 insertions, 29 deletions
diff --git a/src/client/react/components/container/Search.tsx b/src/client/react/components/container/Search.tsx index fdd6c83..b22c26e 100644 --- a/src/client/react/components/container/Search.tsx +++ b/src/client/react/components/container/Search.tsx @@ -13,8 +13,8 @@ import users from '../../users';  import Results from './Results';  import IconFromUserType from '../presentational/IconFromUserType'; -interface SearchStatehProps { -  selectedResult: string, +interface SearchStateProps { +  selectedResult: string | null,    isExactMatch: boolean,  } @@ -23,8 +23,8 @@ interface SearchDispatchProps {    inputChange(typedValue: string): void,  } -class Search extends React.Component<SearchStatehProps & SearchDispatchProps, any> { -  constructor(props: SearchStatehProps & SearchDispatchProps) { +class Search extends React.Component<SearchStateProps & SearchDispatchProps, any> { +  constructor(props: SearchStateProps & SearchDispatchProps) {      super(props);      this.state = { @@ -80,7 +80,7 @@ class Search extends React.Component<SearchStatehProps & SearchDispatchProps, an          <div className="search__input-wrapper">            <div className="search__icon-wrapper">              <IconFromUserType -              userType={isExactMatch ? users.byId[selectedResult].type : null} +              userType={(selectedResult && isExactMatch) ? users.byId[selectedResult].type : undefined}                defaultIcon={<SearchIcon />}              />            </div> @@ -99,26 +99,11 @@ class Search extends React.Component<SearchStatehProps & SearchDispatchProps, an    }  } -// Search.propTypes = { -//   selectedResult: PropTypes.string, -//   isExactMatch: PropTypes.bool.isRequired, -//   dispatch: PropTypes.func.isRequired, -// }; - -// Search.defaultProps = { -//   selectedResult: null, -// }; - -const mapStateToProps = (state: State):SearchStatehProps => ({ +const mapStateToProps = (state: State):SearchStateProps => ({    selectedResult: state.search.selectedResult,    isExactMatch: state.search.isExactMatch,  }); -// const mapDispatchToProps = { -//   inputChange, -//   changeSelectedResult, -// }; -  const mapDispatchToProps = (dispatch: any): SearchDispatchProps => ({    inputChange(typedValue) {      dispatch(inputChange(typedValue)); diff --git a/src/client/react/components/presentational/IconFromUserType.tsx b/src/client/react/components/presentational/IconFromUserType.tsx index d77ea1b..83af34c 100644 --- a/src/client/react/components/presentational/IconFromUserType.tsx +++ b/src/client/react/components/presentational/IconFromUserType.tsx @@ -9,7 +9,7 @@ import TeacherIcon = require('react-icons/lib/md/account-circle');  //   defaultIcon?: JSX.Element,  // } -const IconFromUserType: React.StatelessComponent<{ userType: string, defaultIcon?: JSX.Element }> = (props) => { +const IconFromUserType: React.StatelessComponent<{ userType?: string, defaultIcon?: JSX.Element }> = (props) => {    switch (props.userType) {      case 'c':        return <ClassIcon />; diff --git a/src/client/react/reducers.ts b/src/client/react/reducers.ts index 254fe76..6f92e1d 100644 --- a/src/client/react/reducers.ts +++ b/src/client/react/reducers.ts @@ -5,6 +5,7 @@ export interface State {    search: SearchState,  } +// @ts-ignore  const rootReducer = combineReducers<State>({    search,  }); diff --git a/src/client/react/reducers/search.ts b/src/client/react/reducers/search.ts index 658d3ca..470e650 100644 --- a/src/client/react/reducers/search.ts +++ b/src/client/react/reducers/search.ts @@ -61,7 +61,7 @@ const search = (state = DEFAULT_STATE, action: Action): State => {        if (isExactMatch) return state;        const prevSelectedResult = state.selectedResult; -      const prevSelectedResultIndex = results.indexOf(prevSelectedResult); +      const prevSelectedResultIndex = prevSelectedResult ? results.indexOf(prevSelectedResult) : -1;        let nextSelectedResultIndex =          prevSelectedResultIndex + action.relativeChange; diff --git a/src/client/react/users.ts b/src/client/react/users.ts index a80a1c5..a16e40f 100644 --- a/src/client/react/users.ts +++ b/src/client/react/users.ts @@ -1,6 +1,9 @@  /* global USERS */  import { combineReducers, createStore } from 'redux'; +import { AnyAction } from 'redux'; +import { Reducer } from 'redux'; +import { ReducersMapObject } from 'redux';  export interface User {    type: string, @@ -21,7 +24,11 @@ declare global {  const getId = ({ type, value }: User) => `${type}/${value}`; -const byId = (state = {}, action: Action) => { +type ByIdState = { +  [userId: string]: User, +} + +const byId = (state: ByIdState = {}, action: Action): ByIdState => {    switch (action.type) {      case 'USERS/ADD_USER':        return { @@ -35,7 +42,9 @@ const byId = (state = {}, action: Action) => {    }  }; -const allIds = (state : any[] = [], action : Action) => { +type AllIdsState = string[] + +const allIds = (state: AllIdsState = [], action: Action): AllIdsState => {    switch (action.type) {      case 'USERS/ADD_USER':        return [ @@ -47,7 +56,9 @@ const allIds = (state : any[] = [], action : Action) => {    }  }; -const allUsers = (state : any[] = [], action : Action) => { +type AllUsersState = User[]; + +const allUsers = (state: AllUsersState = [], action: Action): AllUsersState => {    switch (action.type) {      case 'USERS/ADD_USER':        return [ @@ -62,9 +73,9 @@ const allUsers = (state : any[] = [], action : Action) => {  };  interface State { -  byId: any, -  allIds: string[], -  allUsers: User[] +  byId: ByIdState, +  allIds: AllIdsState, +  allUsers: AllUsersState,  }  const store = createStore(combineReducers<State>({  | 
