From 19fe7330c467a8581ba159f7de98bd235f6d2215 Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sat, 30 Oct 2021 23:56:53 +0800 Subject: [PATCH 1/6] TagList - add export of AddTagAction --- src/features/tags/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/tags/types.ts b/src/features/tags/types.ts index 7c28f64..bf2328b 100644 --- a/src/features/tags/types.ts +++ b/src/features/tags/types.ts @@ -11,7 +11,7 @@ export type TagsActionTypes = | AddTagAction | RemoveTagAction; -interface AddTagAction { +export interface AddTagAction { type: typeof ADD_TAG; id: number; user_id: number | null; From 9c80c4434825dcfead5df7a8a51f6ae16f7322a8 Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sun, 31 Oct 2021 00:19:03 +0800 Subject: [PATCH 2/6] TagList - convert to typescript --- src/features/tagsList/{constants.js => constants.ts} | 0 src/features/tagsList/{index.js => index.ts} | 0 src/features/tagsList/{reducer.js => reducer.ts} | 8 ++++++-- 3 files changed, 6 insertions(+), 2 deletions(-) rename src/features/tagsList/{constants.js => constants.ts} (100%) rename src/features/tagsList/{index.js => index.ts} (100%) rename src/features/tagsList/{reducer.js => reducer.ts} (87%) diff --git a/src/features/tagsList/constants.js b/src/features/tagsList/constants.ts similarity index 100% rename from src/features/tagsList/constants.js rename to src/features/tagsList/constants.ts diff --git a/src/features/tagsList/index.js b/src/features/tagsList/index.ts similarity index 100% rename from src/features/tagsList/index.js rename to src/features/tagsList/index.ts diff --git a/src/features/tagsList/reducer.js b/src/features/tagsList/reducer.ts similarity index 87% rename from src/features/tagsList/reducer.js rename to src/features/tagsList/reducer.ts index 2c55d35..a23715d 100644 --- a/src/features/tagsList/reducer.js +++ b/src/features/tagsList/reducer.ts @@ -1,10 +1,14 @@ +import { ReceiveEntitiesAction } from "./../actions"; import { RECEIVE_ENTITIES, ADD_TAG } from "../actionTypes"; +import { Tags, AddTagAction } from "./../tags/types"; -const getIds = (tags) => { +const getIds = (tags: Tags): number[] => { return Object.values(tags).map((tag) => tag.id); }; -const tagsByList = (state = {}, action) => { +export type TagsByListActionTypes = ReceiveEntitiesAction | AddTagAction; + +const tagsByList = (state: any = {}, action: TagsByListActionTypes) => { switch (action.type) { case RECEIVE_ENTITIES: const { entities } = action.payload; From ac3a736e543cf125e980dcf0f7f5b5ad461d5a2c Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sun, 31 Oct 2021 19:49:36 +0800 Subject: [PATCH 3/6] User - add new types and exported other feature types --- src/features/actions.ts | 10 +++------- src/features/auth/types.ts | 7 +------ src/features/subscriptions/types.ts | 2 +- src/features/user/types.ts | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 src/features/user/types.ts diff --git a/src/features/actions.ts b/src/features/actions.ts index f142e68..ad6809b 100644 --- a/src/features/actions.ts +++ b/src/features/actions.ts @@ -1,12 +1,7 @@ +import { User } from "./user/types"; import { RECEIVE_ENTITIES } from "./actionTypes"; import { Tags } from "./tags/types"; -export interface Users { - id: number; - username: string; - image: string; -} - export interface Readings { id: number; title: string; @@ -22,7 +17,7 @@ export interface Readings { } export interface Entities { - user?: Users; + users?: User[]; readings?: Readings; tags?: Tags; } @@ -35,6 +30,7 @@ export interface ReceiveEntitiesAction { list: any; id?: any; user_id?: number | null; + users?: User[]; } export const receiveEntities = ( diff --git a/src/features/auth/types.ts b/src/features/auth/types.ts index 272206e..fec0ee4 100644 --- a/src/features/auth/types.ts +++ b/src/features/auth/types.ts @@ -1,11 +1,6 @@ import { SET_CURRENT_USER } from "../actionTypes"; import { AlertActionTypes } from "../alerts/types"; - -export interface User { - id: number | null; - username: string | null; - image: string | null; -} +import { User } from "../user/types"; export interface AuthState { isAuthenticated: boolean; diff --git a/src/features/subscriptions/types.ts b/src/features/subscriptions/types.ts index 7ace278..09775bc 100644 --- a/src/features/subscriptions/types.ts +++ b/src/features/subscriptions/types.ts @@ -9,7 +9,7 @@ import { RootState } from "../rootReducer"; export type SubscriptionState = any; -interface LoadSubscriptionAction { +export interface LoadSubscriptionAction { type: typeof LOAD_SUBSCRIPTIONS; users: any; id: number; diff --git a/src/features/user/types.ts b/src/features/user/types.ts new file mode 100644 index 0000000..f239b4c --- /dev/null +++ b/src/features/user/types.ts @@ -0,0 +1,20 @@ +import { LOAD_USER } from "../actionTypes"; + +import { ReceiveEntitiesAction } from "../actions"; +import { LoadSubscriptionAction } from "../subscriptions/types"; + +export interface User { + id: number; + username: string; + image: string; +} + +export type UserActionTypes = + | ReceiveEntitiesAction + | LoadUserAction + | LoadSubscriptionAction; + +export interface LoadUserAction { + type: typeof LOAD_USER; + users: User[]; +} From 13c36eaeed9e8b6d0dcc867d7966c8927eff7f73 Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sun, 31 Oct 2021 20:02:46 +0800 Subject: [PATCH 4/6] User - reducer to typescript: required to add userFollowers for loadSubscriptions action. Added userFollower prop to all required fall through action. Add generic to api call --- src/common/services/api.ts | 4 ++-- src/features/actions.ts | 3 ++- src/features/subscriptions/actions.ts | 11 ++++++++--- src/features/subscriptions/types.ts | 3 ++- src/features/user/actions.js | 4 ++-- src/features/user/{reducer.js => reducer.ts} | 16 ++++++++++------ src/features/user/types.ts | 6 ++++++ 7 files changed, 32 insertions(+), 15 deletions(-) rename src/features/user/{reducer.js => reducer.ts} (67%) diff --git a/src/common/services/api.ts b/src/common/services/api.ts index 15f3f2c..b7fa7d7 100644 --- a/src/common/services/api.ts +++ b/src/common/services/api.ts @@ -17,11 +17,11 @@ export function setTokenHeader(token: string | boolean): void { * @param {string} path - the route path/endpoint * @param {object} data - (optional) data in JSON form for POST requests */ -export const apiCall = ( +export const apiCall = ( method: Method, url: string, data?: any -): AxiosPromise => { +): AxiosPromise => { return new Promise((resolve, reject) => { return axios .request({ method, url, data }) diff --git a/src/features/actions.ts b/src/features/actions.ts index ad6809b..50d7825 100644 --- a/src/features/actions.ts +++ b/src/features/actions.ts @@ -1,4 +1,4 @@ -import { User } from "./user/types"; +import { User, UserFollowers } from "./user/types"; import { RECEIVE_ENTITIES } from "./actionTypes"; import { Tags } from "./tags/types"; @@ -31,6 +31,7 @@ export interface ReceiveEntitiesAction { id?: any; user_id?: number | null; users?: User[]; + userFollowers?: UserFollowers; } export const receiveEntities = ( diff --git a/src/features/subscriptions/actions.ts b/src/features/subscriptions/actions.ts index 97025f8..6a7ea2e 100644 --- a/src/features/subscriptions/actions.ts +++ b/src/features/subscriptions/actions.ts @@ -11,12 +11,17 @@ import { postingNewSubscription, fetchingSubscriptionsIfNeeded, } from "./types"; +import { UserFollowers } from "../user/types"; +import { AxiosResponse } from "axios"; const { addAlert } = alerts.actions; -export const loadSubscriptions = (users: any, id: number) => ({ +export const loadSubscriptions = ( + userFollowers: AxiosResponse, + id: number +) => ({ type: LOAD_SUBSCRIPTIONS, - users, + userFollowers, id, }); @@ -35,7 +40,7 @@ export const removeSubscriptions = (id: number, user_id: number | null) => ({ export const fetchSubscriptions = (user_id: number): fetchingSubscriptions => async (dispatch) => { - return apiCall("get", `/users/${user_id}/subscriptions`) + return apiCall("get", `/users/${user_id}/subscriptions`) .then((res) => dispatch(loadSubscriptions(res, user_id))) .catch((err) => dispatch(addAlert({ message: err.message, type: "danger" })) diff --git a/src/features/subscriptions/types.ts b/src/features/subscriptions/types.ts index 09775bc..61b968e 100644 --- a/src/features/subscriptions/types.ts +++ b/src/features/subscriptions/types.ts @@ -6,14 +6,15 @@ import { REMOVE_SUBSCRIPTIONS, } from "../actionTypes"; import { RootState } from "../rootReducer"; +import { UserFollowers } from "../user/types"; export type SubscriptionState = any; export interface LoadSubscriptionAction { type: typeof LOAD_SUBSCRIPTIONS; - users: any; id: number; user_id: never; + userFollowers: UserFollowers; } interface AddSubscriptionAction { diff --git a/src/features/user/actions.js b/src/features/user/actions.js index 758f4b8..47327db 100644 --- a/src/features/user/actions.js +++ b/src/features/user/actions.js @@ -4,9 +4,9 @@ import { LOAD_USER } from "../actionTypes"; const { addError } = alerts.actions; -export const loadUser = (user) => ({ +export const loadUser = (users) => ({ type: LOAD_USER, - user, + users, }); export const fetchUser = (id) => { diff --git a/src/features/user/reducer.js b/src/features/user/reducer.ts similarity index 67% rename from src/features/user/reducer.js rename to src/features/user/reducer.ts index e1312e6..83f8f4b 100644 --- a/src/features/user/reducer.js +++ b/src/features/user/reducer.ts @@ -3,8 +3,9 @@ import { LOAD_USER, LOAD_SUBSCRIPTIONS, } from "../actionTypes"; +import { UserActionTypes } from "./types"; -const user = (state = {}, action) => { +const user = (state = {}, action: UserActionTypes) => { switch (action.type) { case RECEIVE_ENTITIES: const { entities } = action.payload; @@ -13,17 +14,20 @@ const user = (state = {}, action) => { } /* falls through */ case LOAD_USER: - if (action && action.user) { + if (action && action.users) { return { ...state, - [action.user[0].id]: action.user[0], + [action.users[0].id]: action.users[0], }; } /* falls through */ case LOAD_SUBSCRIPTIONS: - const { users } = action; - if (users) { - const allSubscriptions = [...users.following, ...users.followers]; + const { userFollowers } = action; + if (userFollowers) { + const allSubscriptions = [ + ...userFollowers.following, + ...userFollowers.followers, + ]; const uniqueSubscriptions = allSubscriptions.reduce((object, user) => { return { ...object, diff --git a/src/features/user/types.ts b/src/features/user/types.ts index f239b4c..3b53e4b 100644 --- a/src/features/user/types.ts +++ b/src/features/user/types.ts @@ -9,6 +9,11 @@ export interface User { image: string; } +export interface UserFollowers { + followers: any[]; + following: any[]; +} + export type UserActionTypes = | ReceiveEntitiesAction | LoadUserAction @@ -17,4 +22,5 @@ export type UserActionTypes = export interface LoadUserAction { type: typeof LOAD_USER; users: User[]; + userFollowers?: UserFollowers; } From f212662350b0f0fe0a839f35371afac1a6c0ed20 Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sun, 31 Oct 2021 20:19:55 +0800 Subject: [PATCH 5/6] User - coverted action,selector, index, constant to typescript --- src/features/user/actions.js | 30 ---------------- src/features/user/actions.ts | 34 +++++++++++++++++++ .../user/{constants.js => constants.ts} | 0 src/features/user/{index.js => index.ts} | 0 src/features/user/selectors.js | 3 -- src/features/user/selectors.ts | 3 ++ src/features/user/types.ts | 18 +++++++++- 7 files changed, 54 insertions(+), 34 deletions(-) delete mode 100644 src/features/user/actions.js create mode 100644 src/features/user/actions.ts rename src/features/user/{constants.js => constants.ts} (100%) rename src/features/user/{index.js => index.ts} (100%) delete mode 100644 src/features/user/selectors.js create mode 100644 src/features/user/selectors.ts diff --git a/src/features/user/actions.js b/src/features/user/actions.js deleted file mode 100644 index 47327db..0000000 --- a/src/features/user/actions.js +++ /dev/null @@ -1,30 +0,0 @@ -import { apiCall } from "../../common/services/api"; -import alerts from "../alerts"; -import { LOAD_USER } from "../actionTypes"; - -const { addError } = alerts.actions; - -export const loadUser = (users) => ({ - type: LOAD_USER, - users, -}); - -export const fetchUser = (id) => { - return (dispatch) => { - return apiCall("get", `/users/${id}`) - .then((res) => dispatch(loadUser(res))) - .catch((err) => dispatch(addError(err))); - }; -}; -const shouldFetchUser = (state, id) => { - const user = state.user[id]; - if (!user) return true; -}; - -export const fetchUserIfNeeded = (id) => { - return (dispatch, getState) => { - if (shouldFetchUser(getState(), id)) { - return dispatch(fetchUser(id)); - } - }; -}; diff --git a/src/features/user/actions.ts b/src/features/user/actions.ts new file mode 100644 index 0000000..9609f48 --- /dev/null +++ b/src/features/user/actions.ts @@ -0,0 +1,34 @@ +import { AxiosResponse } from "axios"; +import { apiCall } from "../../common/services/api"; +import alerts from "../alerts"; +import { LOAD_USER } from "../actionTypes"; +import { fetchingUser, fetchingUserIfNeeded, User } from "./types"; + +const { addAlert } = alerts.actions; + +export const loadUser = (users: AxiosResponse) => ({ + type: LOAD_USER, + users, +}); + +export const fetchUser = (id: number): fetchingUser => { + return (dispatch) => { + return apiCall("get", `/users/${id}`) + .then((res) => dispatch(loadUser(res))) + .catch((err) => + dispatch(addAlert({ message: err.message, type: "danger" })) + ); + }; +}; +const shouldFetchUser = (state: any, id: number) => { + const user = state.user[id]; + if (!user) return true; +}; + +export const fetchUserIfNeeded = (id: number): fetchingUserIfNeeded => { + return (dispatch, getState) => { + if (shouldFetchUser(getState(), id)) { + return dispatch(fetchUser(id)); + } + }; +}; diff --git a/src/features/user/constants.js b/src/features/user/constants.ts similarity index 100% rename from src/features/user/constants.js rename to src/features/user/constants.ts diff --git a/src/features/user/index.js b/src/features/user/index.ts similarity index 100% rename from src/features/user/index.js rename to src/features/user/index.ts diff --git a/src/features/user/selectors.js b/src/features/user/selectors.js deleted file mode 100644 index b5a109a..0000000 --- a/src/features/user/selectors.js +++ /dev/null @@ -1,3 +0,0 @@ -export const getUserById = (state, id) => { - return state.user[id]; -}; diff --git a/src/features/user/selectors.ts b/src/features/user/selectors.ts new file mode 100644 index 0000000..33d7f76 --- /dev/null +++ b/src/features/user/selectors.ts @@ -0,0 +1,3 @@ +export const getUserById = (state: any, id: number) => { + return state.user[id]; +}; diff --git a/src/features/user/types.ts b/src/features/user/types.ts index 3b53e4b..08774ec 100644 --- a/src/features/user/types.ts +++ b/src/features/user/types.ts @@ -1,5 +1,7 @@ +import { Action } from "redux"; +import { ThunkAction } from "redux-thunk"; +import { RootState } from "./../rootReducer"; import { LOAD_USER } from "../actionTypes"; - import { ReceiveEntitiesAction } from "../actions"; import { LoadSubscriptionAction } from "../subscriptions/types"; @@ -24,3 +26,17 @@ export interface LoadUserAction { users: User[]; userFollowers?: UserFollowers; } + +export type fetchingUser = ThunkAction< + Promise | undefined, + RootState, + unknown, + Action +>; + +export type fetchingUserIfNeeded = ThunkAction< + Promise | undefined, + RootState, + unknown, + Action +>; From a5db1d0f2188dcc0339d80224c0761e9f8255ced Mon Sep 17 00:00:00 2001 From: hjpunzalan Date: Sun, 31 Oct 2021 21:05:34 +0800 Subject: [PATCH 6/6] User - UserAside component converted to typescript: modified types of other selectors and components (id: string | number) --- src/common/Card.tsx | 5 ++- src/features/globalReadings/selectors.ts | 8 ++-- src/features/subscriptions/actions.ts | 8 ++-- src/features/subscriptions/selectors.ts | 4 +- src/features/tags/actions.ts | 4 +- .../user/{UserAside.js => UserAside.tsx} | 37 ++++++++++++++----- src/features/user/actions.ts | 8 ++-- src/features/user/selectors.ts | 2 +- 8 files changed, 49 insertions(+), 27 deletions(-) rename src/features/user/{UserAside.js => UserAside.tsx} (87%) diff --git a/src/common/Card.tsx b/src/common/Card.tsx index 764fd2a..ecc289e 100644 --- a/src/common/Card.tsx +++ b/src/common/Card.tsx @@ -1,13 +1,14 @@ import React from "react"; import { NavLink } from "react-router-dom"; +import { arrayOfIds } from "../features/subscriptions/types"; import UserImage from "./UserImage"; interface CardProps { id?: string; image?: string; username?: string; - followings?: string; - followers?: string; + followings?: arrayOfIds | null; + followers?: arrayOfIds | null; children: React.ReactNode; } diff --git a/src/features/globalReadings/selectors.ts b/src/features/globalReadings/selectors.ts index f048f71..f9a452c 100644 --- a/src/features/globalReadings/selectors.ts +++ b/src/features/globalReadings/selectors.ts @@ -21,9 +21,9 @@ const getTagReadings = (readings: any[], tag_id: number): any[] => { export const getReadings = ( state: any, list: string, - fav: any, - outdated: any, - tag_id: number + fav?: any, + outdated?: any, + tag_id?: number ): any[] | void => { // give time for readingsByList items to load if ( @@ -56,7 +56,7 @@ export const getReadingById = (state: any, list: string, id: number): any => { export const getWebsites = ( state: any, list: string, - tag_id: number + tag_id?: number ): { [k: string]: any } => { let websiteCount: { [k: string]: any } = {}; diff --git a/src/features/subscriptions/actions.ts b/src/features/subscriptions/actions.ts index 6a7ea2e..1459b0a 100644 --- a/src/features/subscriptions/actions.ts +++ b/src/features/subscriptions/actions.ts @@ -18,7 +18,7 @@ const { addAlert } = alerts.actions; export const loadSubscriptions = ( userFollowers: AxiosResponse, - id: number + id: string | number ) => ({ type: LOAD_SUBSCRIPTIONS, userFollowers, @@ -38,7 +38,7 @@ export const removeSubscriptions = (id: number, user_id: number | null) => ({ }); export const fetchSubscriptions = - (user_id: number): fetchingSubscriptions => + (user_id: string | number): fetchingSubscriptions => async (dispatch) => { return apiCall("get", `/users/${user_id}/subscriptions`) .then((res) => dispatch(loadSubscriptions(res, user_id))) @@ -84,7 +84,7 @@ export const postNewSubscription = const shouldFetchSubscriptions = ( state: any, - id: number + id: string | number ): boolean | undefined => { const subscriptions = state.subscriptions[id]; const upToDate = state.subscriptions.upToDate; @@ -94,7 +94,7 @@ const shouldFetchSubscriptions = ( }; export const fetchSubscriptionsIfNeeded = ( - id: number + id: string | number ): fetchingSubscriptionsIfNeeded => { return (dispatch, getState) => { if (shouldFetchSubscriptions(getState(), id)) { diff --git a/src/features/subscriptions/selectors.ts b/src/features/subscriptions/selectors.ts index 9f5d783..7dc81c9 100644 --- a/src/features/subscriptions/selectors.ts +++ b/src/features/subscriptions/selectors.ts @@ -9,7 +9,7 @@ export const getSubscriptions = ( export const getFollowers = ( state: any, - id: number + id: number | string ): arrayOfIds | undefined => { if (state.subscriptions[id]) { return state.subscriptions[id].followers; @@ -18,7 +18,7 @@ export const getFollowers = ( export const getFollowings = ( state: any, - id: number + id: number | string ): arrayOfIds | undefined => { if (state.subscriptions[id]) { return state.subscriptions[id].following; diff --git a/src/features/tags/actions.ts b/src/features/tags/actions.ts index 8d336f4..121e0b5 100644 --- a/src/features/tags/actions.ts +++ b/src/features/tags/actions.ts @@ -27,7 +27,7 @@ export const loadTags = (tags: Tags) => ({ tags, }); -export const fetchTags = (list: string, id: number): fetchingTags => { +export const fetchTags = (list: string, id: string): fetchingTags => { return (dispatch, getState) => { if (list === "global") { dispatch(addLoader("tags")); @@ -121,7 +121,7 @@ const shouldFetchTags = (state: any, list: string) => { export const fetchTagsIfNeeded = ( list: string, - id: number + id: string ): fetchingTagsIfNeeded => { return (dispatch, getState) => { if (shouldFetchTags(getState(), list)) { diff --git a/src/features/user/UserAside.js b/src/features/user/UserAside.tsx similarity index 87% rename from src/features/user/UserAside.js rename to src/features/user/UserAside.tsx index 1e0083a..83a6ef7 100644 --- a/src/features/user/UserAside.js +++ b/src/features/user/UserAside.tsx @@ -1,6 +1,6 @@ import React, { Component } from "react"; -import { NavLink } from "react-router-dom"; -import { connect } from "react-redux"; +import { NavLink, RouteComponentProps } from "react-router-dom"; +import { connect, ConnectedProps } from "react-redux"; import subscriptions from "../subscriptions"; import globalReadings from "../globalReadings"; import tags, { TagsAside } from "../tags"; @@ -9,12 +9,25 @@ import { getUserById } from "./selectors"; import Card from "../../common/Card"; import Subscribe from "../../common/Subscribe"; import ReadingStats from "../../common/ReadingsStats"; +import { RootState } from "../rootReducer"; const { getReadings, getWebsites, getUserReadingsInNeedOfUpdate } = globalReadings.selectors; const { getFollowers, getFollowings } = subscriptions.selectors; -class UserAside extends Component { +type UserAsideProps = PropsFromRedux & OwnProps; + +interface matchProps { + id: string; +} + +interface OwnProps extends RouteComponentProps { + key: number; + id: number; + fav: any; +} + +class UserAside extends Component { componentDidMount() { if (this.props.match) { this.props.fetchTagsIfNeeded( @@ -32,7 +45,7 @@ class UserAside extends Component { } } - componentDidUpdate(prevProps) { + componentDidUpdate(prevProps: UserAsideProps) { if ( this.props.match && prevProps.match && @@ -73,7 +86,7 @@ class UserAside extends Component { totalFavorites = 0, totalOutdated = 0; - let u = {}; + let u: any = {}; if (user) u = user; else u = currentUser; // if (!readings) u = currentUser; @@ -85,7 +98,9 @@ class UserAside extends Component { totalReadings = readings.length; totalWebsites = Object.keys(websites).length; - totalBooks = totalWords.toFixed(2); + // totalWords.toFixed(2); returns string + // Math.round(totalWords * 1e2) / 1e2; returns number + totalBooks = Math.round(totalWords * 1e2) / 1e2; for (const prop in websites) { if (websites[prop] > maxReads) { @@ -182,7 +197,7 @@ class UserAside extends Component { } } -function mapStateToProps(state, ownProps) { +function mapStateToProps(state: RootState, ownProps: OwnProps) { return { readings: getReadings(state, ownProps.match.params.id), websites: getWebsites(state, ownProps.match.params.id), @@ -200,9 +215,13 @@ function mapStateToProps(state, ownProps) { }; } -export default connect(mapStateToProps, { +const connector = connect(mapStateToProps, { ...subscriptions.actions, ...globalReadings.actions, ...tags.actions, fetchUserIfNeeded, -})(UserAside); +}); + +type PropsFromRedux = ConnectedProps; + +export default connector(UserAside); diff --git a/src/features/user/actions.ts b/src/features/user/actions.ts index 9609f48..3919f97 100644 --- a/src/features/user/actions.ts +++ b/src/features/user/actions.ts @@ -11,7 +11,7 @@ export const loadUser = (users: AxiosResponse) => ({ users, }); -export const fetchUser = (id: number): fetchingUser => { +export const fetchUser = (id: string | number): fetchingUser => { return (dispatch) => { return apiCall("get", `/users/${id}`) .then((res) => dispatch(loadUser(res))) @@ -20,12 +20,14 @@ export const fetchUser = (id: number): fetchingUser => { ); }; }; -const shouldFetchUser = (state: any, id: number) => { +const shouldFetchUser = (state: any, id: string | number) => { const user = state.user[id]; if (!user) return true; }; -export const fetchUserIfNeeded = (id: number): fetchingUserIfNeeded => { +export const fetchUserIfNeeded = ( + id: string | number +): fetchingUserIfNeeded => { return (dispatch, getState) => { if (shouldFetchUser(getState(), id)) { return dispatch(fetchUser(id)); diff --git a/src/features/user/selectors.ts b/src/features/user/selectors.ts index 33d7f76..9b1c386 100644 --- a/src/features/user/selectors.ts +++ b/src/features/user/selectors.ts @@ -1,3 +1,3 @@ -export const getUserById = (state: any, id: number) => { +export const getUserById = (state: any, id: number | string) => { return state.user[id]; };