Skip to content

Commit

Permalink
Convert Feed page store js files to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Shanks committed Jan 30, 2024
1 parent b006c02 commit c51c54e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
45 changes: 37 additions & 8 deletions packages/common/src/store/pages/feed/actions.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
// @ts-nocheck
// TODO(nkang) - convert to TS
import { FeedFilter } from 'models/FeedFilter'
import { ID } from 'models/Identifiers'

export const FETCH_SUGGESTED_FOLLOW_USERS = 'FEED/FETCH_SUGGESTED_FOLLOW_USERS'
export const FOLLOW_USERS = 'FEED/FOLLOW_USERS'
export const SET_SUGGESTED_FOLLOWS = 'FEED/SET_SUGGESTED_FOLLOWS'
export const SET_FEED_FILTER = 'FEED/SET_FEED_FILTER'

export const fetchSuggestedFollowUsers = () => ({
type: FETCH_SUGGESTED_FOLLOW_USERS
})
export type FetchSuggestedFollowUsersAction = {
type: typeof FETCH_SUGGESTED_FOLLOW_USERS
}

export type FollowUsersAction = {
type: typeof FOLLOW_USERS
userIds: ID[]
}

export type SetSuggestedFollowsAction = {
type: typeof SET_SUGGESTED_FOLLOWS
userIds: ID[]
}

export type SetFeedFilterAction = {
type: typeof SET_FEED_FILTER
filter: FeedFilter
}

export type FeedPageActions =
| FetchSuggestedFollowUsersAction
| FollowUsersAction
| SetSuggestedFollowsAction
| SetFeedFilterAction

export const fetchSuggestedFollowUsers =
(): FetchSuggestedFollowUsersAction => ({
type: FETCH_SUGGESTED_FOLLOW_USERS
})

export const followUsers = (userIds) => ({
export const followUsers = (userIds: ID[]): FollowUsersAction => ({
type: FOLLOW_USERS,
userIds
})

export const setSuggestedFollows = (userIds) => ({
export const setSuggestedFollows = (
userIds: ID[]
): SetSuggestedFollowsAction => ({
type: SET_SUGGESTED_FOLLOWS,
userIds
})

export const setFeedFilter = (filter) => ({
export const setFeedFilter = (filter: FeedFilter): SetFeedFilterAction => ({
type: SET_FEED_FILTER,
filter
})
17 changes: 12 additions & 5 deletions packages/common/src/store/pages/feed/lineup/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
// TODO(nkang) - convert to TS
import { LineupState } from 'models/Lineup'
import { Track } from 'models/Track'
import { RESET_SUCCEEDED, stripPrefix } from 'store/lineup/actions'
import { initialLineupState } from 'store/lineup/reducer'
import { PREFIX } from 'store/pages/feed/lineup/actions'
Expand All @@ -11,15 +11,22 @@ export const initialState = {
entryIds: new Set([])
}

type ResetSucceededAction = {
type: typeof RESET_SUCCEEDED
}

const actionsMap = {
[RESET_SUCCEEDED](state, action) {
[RESET_SUCCEEDED](_state: LineupState<Track>, _action: ResetSucceededAction) {
const newState = initialState
return newState
}
}

const feed = (state = initialState, action) => {
const baseActionType = stripPrefix(PREFIX, action.type)
const feed = (state = initialState, action: ResetSucceededAction) => {
const baseActionType = stripPrefix(
PREFIX,
action.type
) as typeof RESET_SUCCEEDED
const matchingReduceFunction = actionsMap[baseActionType]
if (!matchingReduceFunction) return state
return matchingReduceFunction(state, action)
Expand Down
29 changes: 19 additions & 10 deletions packages/common/src/store/pages/feed/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// @ts-nocheck
// TODO(nkang) - convert to TS
import { asLineup } from 'store/lineup/reducer'
import { LineupActions, asLineup } from 'store/lineup/reducer'
import {
SET_SUGGESTED_FOLLOWS,
SET_FEED_FILTER
SET_FEED_FILTER,
SetSuggestedFollowsAction,
SetFeedFilterAction,
FeedPageActions
} from 'store/pages/feed/actions'
import { PREFIX as FeedPrefix } from 'store/pages/feed/lineup/actions'
import feedReducer, {
initialState as feedLinupInitialState
} from 'store/pages/feed/lineup/reducer'

import { FeedFilter } from '../../../models'
import { FeedFilter, Track } from '../../../models'

import { FeedPageState } from './types'

const initialState = {
suggestedFollows: [],
Expand All @@ -19,13 +22,16 @@ const initialState = {
}

const actionsMap = {
[SET_SUGGESTED_FOLLOWS](state, action) {
[SET_SUGGESTED_FOLLOWS](
state: FeedPageState,
action: SetSuggestedFollowsAction
) {
return {
...state,
suggestedFollows: action.userIds
}
},
[SET_FEED_FILTER](state, action) {
[SET_FEED_FILTER](state: FeedPageState, action: SetFeedFilterAction) {
return {
...state,
feedFilter: action.filter
Expand All @@ -35,13 +41,16 @@ const actionsMap = {

const feedLineupReducer = asLineup(FeedPrefix, feedReducer)

const reducer = (state = initialState, action) => {
const feed = feedLineupReducer(state.feed, action)
const reducer = (
state = initialState,
action: FeedPageActions | LineupActions<Track>
) => {
const feed = feedLineupReducer(state.feed, action as LineupActions<Track>)
if (feed !== state.feed) return { ...state, feed }

const matchingReduceFunction = actionsMap[action.type]
if (!matchingReduceFunction) return state
return matchingReduceFunction(state, action)
return matchingReduceFunction(state, action as FeedPageActions)
}

export default reducer

0 comments on commit c51c54e

Please sign in to comment.