Skip to content

Commit

Permalink
Convert profile page store js files to ts (#7386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Shanks authored Jan 30, 2024
1 parent 5cd63b2 commit 334e9d5
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 80 deletions.
219 changes: 194 additions & 25 deletions packages/common/src/store/pages/profile/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Nullable } from 'utils'

import { ID, User, UserMetadata } from '../../../models'
import { ID, UID, UserMetadata } from '../../../models'

import { CollectionSortMode, FollowType } from './types'

Expand Down Expand Up @@ -28,13 +28,166 @@ export const DISMISS_PROFILE_METER = 'PROFILE/DISMISS_PROFILE_METER'
export const FETCH_TOP_TAGS = 'PROFILE/FETCH_TOP_TAGS'
export const FETCH_TOP_TAGS_SUCCEEDED = 'PROFILE/FETCH_TOP_TAGS_SUCCEEDED'
export const FETCH_TOP_TAGS_FAILED = 'PROFILE/FETCH_TOP_TAGS_FAILED'

export const SET_NOTIFICATION_SUBSCRIPTION =
'PROFILE/SET_NOTIFICATION_SUBSCRIPTION'

export const FETCH_COLLECTIONS = 'PROFILE/FETCH_COLLECTIONS'
export const FETCH_COLLECTIONS_SUCCEEDED = 'PROFILE/FETCH_COLLECTIONS_SUCCEEDED'
export const FETCH_COLLECTIONS_FAILED = 'PROFILE/FETCH_COLLECTIONS_FAILED'

export type FetchProfileAction = {
type: typeof FETCH_PROFILE
handle: string | null
userId: ID | null
forceUpdate: boolean
shouldSetLoading: boolean
deleteExistingEntry: boolean
fetchOnly?: boolean
}

export type FetchProfileSucceededAction = {
type: typeof FETCH_PROFILE_SUCCEEDED
handle: string
userId: ID
fetchOnly: boolean
}

export type FetchProfileFailedAction = {
type: typeof FETCH_PROFILE_FAILED
handle: string
}

export type SetCurrentUserAction = {
type: typeof SET_CURRENT_USER
handle: string
}

export type UpdateProfileAction = {
type: typeof UPDATE_PROFILE
metadata: UserMetadata
}

export type UpdateProfileSucceededAction = {
type: typeof UPDATE_PROFILE_SUCCEEDED
userId: ID
}

export type UpdateProfileFailedAction = {
type: typeof UPDATE_PROFILE_FAILED
}

export type UpdateCollectionSortModeAction = {
type: typeof UPDATE_COLLECTION_SORT_MODE
mode: CollectionSortMode
handle: string
}

export type SetProfileFieldAction = {
type: typeof SET_PROFILE_FIELD
field: string
value: string
handle: string
}

export type UpdateCurrentUserFollowsAction = {
type: typeof UPDATE_CURRENT_USER_FOLLOWS
follow?: boolean
handle: string
}

export type FetchFollowUsersAction = {
type: typeof FETCH_FOLLOW_USERS
followerGroup: FollowType
limit?: number
offset?: number
handle: string
}

export type FetchFollowUsersSucceededAction = {
type: typeof FETCH_FOLLOW_USERS_SUCCEEDED
followerGroup: FollowType
userIds: { id: ID; uid?: UID }[]
limit: number
offset: number
handle: string
}

export type FetchFollowUsersFailedAction = {
type: typeof FETCH_FOLLOW_USERS_FAILED
followerGroup: FollowType
limit: number
offset: number
handle: string
}

export type DismissProfileMeterAction = {
type: typeof DISMISS_PROFILE_METER
}

export type FetchTopTagsAction = {
type: typeof FETCH_TOP_TAGS
handle: string
userId: ID
}

export type FetchTopTagsSucceededAction = {
type: typeof FETCH_TOP_TAGS_SUCCEEDED
handle: string
topTags: string[]
}

export type FetchTopTagsFailedAction = {
type: typeof FETCH_TOP_TAGS_FAILED
handle: string
}

export type SetNotificationSubscriptionAction = {
type: typeof SET_NOTIFICATION_SUBSCRIPTION
userId: ID
isSubscribed: boolean
update?: boolean
handle?: string
}

export type FetchCollectionsAction = {
type: typeof FETCH_COLLECTIONS
handle: string
}

export type FetchCollectionsSucceededAction = {
type: typeof FETCH_COLLECTIONS_SUCCEEDED
handle: string
}

export type FetchCollectionsFailedAction = {
type: typeof FETCH_COLLECTIONS_FAILED
handle: string
}

export type ProfilePageAction =
| FetchProfileAction
| FetchProfileSucceededAction
| FetchProfileFailedAction
| SetCurrentUserAction
| UpdateProfileAction
| UpdateProfileSucceededAction
| UpdateProfileFailedAction
| UpdateCollectionSortModeAction
| SetProfileFieldAction
| UpdateCurrentUserFollowsAction
| FetchFollowUsersAction
| FetchFollowUsersSucceededAction
| FetchFollowUsersFailedAction
| DismissProfileMeterAction
| FetchTopTagsAction
| FetchTopTagsSucceededAction
| FetchTopTagsFailedAction
| SetNotificationSubscriptionAction
| FetchCollectionsAction
| FetchCollectionsSucceededAction
| FetchCollectionsFailedAction

// Either handle or userId is required
// TODO: Move this to redux toolkit
export function fetchProfile(
Expand All @@ -44,7 +197,7 @@ export function fetchProfile(
shouldSetLoading: boolean,
deleteExistingEntry: boolean,
fetchOnly = false
) {
): FetchProfileAction {
return {
type: FETCH_PROFILE,
handle,
Expand All @@ -60,42 +213,51 @@ export function fetchProfileSucceeded(
handle: string,
userId: ID,
fetchOnly: boolean
) {
): FetchProfileSucceededAction {
return { type: FETCH_PROFILE_SUCCEEDED, handle, userId, fetchOnly }
}

export function fetchProfileFailed(handle: string) {
export function fetchProfileFailed(handle: string): FetchProfileFailedAction {
return { type: FETCH_PROFILE_FAILED, handle }
}

export function setCurrentUser(handle: string) {
export function setCurrentUser(handle: string): SetCurrentUserAction {
return { type: SET_CURRENT_USER, handle }
}

export function updateProfile(metadata: UserMetadata) {
export function updateProfile(metadata: UserMetadata): UpdateProfileAction {
return { type: UPDATE_PROFILE, metadata }
}

export function updateProfileSucceeded(userId: ID) {
export function updateProfileSucceeded(
userId: ID
): UpdateProfileSucceededAction {
return { type: UPDATE_PROFILE_SUCCEEDED, userId }
}

export function updateProfileFailed() {
export function updateProfileFailed(): UpdateProfileFailedAction {
return { type: UPDATE_PROFILE_FAILED }
}

export function updateCollectionSortMode(
mode: CollectionSortMode,
handle: string
) {
): UpdateCollectionSortModeAction {
return { type: UPDATE_COLLECTION_SORT_MODE, mode, handle }
}

export function setProfileField(field: string, value: string, handle: string) {
export function setProfileField(
field: string,
value: string,
handle: string
): SetProfileFieldAction {
return { type: SET_PROFILE_FIELD, field, value, handle }
}

export function updateCurrentUserFollows(follow = false, handle: string) {
export function updateCurrentUserFollows(
follow = false,
handle: string
): UpdateCurrentUserFollowsAction {
return { type: UPDATE_CURRENT_USER_FOLLOWS, follow, handle }
}

Expand All @@ -104,17 +266,17 @@ export function fetchFollowUsers(
limit = 15,
offset = 0,
handle: string
) {
): FetchFollowUsersAction {
return { type: FETCH_FOLLOW_USERS, followerGroup, offset, limit, handle }
}

export function fetchFollowUsersSucceeded(
followerGroup: User[],
userIds: ID[],
followerGroup: FollowType,
userIds: { id: ID; uid?: UID }[],
limit: number,
offset: number,
handle: string
) {
): FetchFollowUsersSucceededAction {
return {
type: FETCH_FOLLOW_USERS_SUCCEEDED,
followerGroup,
Expand All @@ -126,11 +288,11 @@ export function fetchFollowUsersSucceeded(
}

export function fetchFollowUsersFailed(
followerGroup: User[],
followerGroup: FollowType,
limit: number,
offset: number,
handle: string
) {
): FetchFollowUsersFailedAction {
return {
type: FETCH_FOLLOW_USERS_FAILED,
followerGroup,
Expand All @@ -140,7 +302,7 @@ export function fetchFollowUsersFailed(
}
}

export function profileMeterDismissed() {
export function profileMeterDismissed(): DismissProfileMeterAction {
return { type: DISMISS_PROFILE_METER }
}

Expand All @@ -149,7 +311,7 @@ export function setNotificationSubscription(
isSubscribed: boolean,
update = false,
handle?: string
) {
): SetNotificationSubscriptionAction {
return {
type: SET_NOTIFICATION_SUBSCRIPTION,
userId,
Expand All @@ -159,44 +321,51 @@ export function setNotificationSubscription(
}
}

export function fetchCollections(handle: string) {
export function fetchCollections(handle: string): FetchCollectionsAction {
return {
type: FETCH_COLLECTIONS,
handle
}
}

export function fetchCollectionsSucceded(handle: string) {
export function fetchCollectionsSucceded(
handle: string
): FetchCollectionsSucceededAction {
return {
type: FETCH_COLLECTIONS_SUCCEEDED,
handle
}
}

export function fetchCollectionsFailed(handle: string) {
export function fetchCollectionsFailed(
handle: string
): FetchCollectionsFailedAction {
return {
type: FETCH_COLLECTIONS_FAILED,
handle
}
}

export function fetchTopTags(handle: string, userId: ID) {
export function fetchTopTags(handle: string, userId: ID): FetchTopTagsAction {
return {
type: FETCH_TOP_TAGS,
handle,
userId
}
}

export function fetchTopTagsSucceeded(handle: string, topTags: string[]) {
export function fetchTopTagsSucceeded(
handle: string,
topTags: string[]
): FetchTopTagsSucceededAction {
return {
type: FETCH_TOP_TAGS_SUCCEEDED,
handle,
topTags
}
}

export function fetchTopTagsFailed(handle: string) {
export function fetchTopTagsFailed(handle: string): FetchTopTagsFailedAction {
return {
type: FETCH_TOP_TAGS_FAILED,
handle
Expand Down
17 changes: 12 additions & 5 deletions packages/common/src/store/pages/profile/lineups/tracks/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/profile/lineups/tracks/actions'
Expand All @@ -9,15 +9,22 @@ export const initialState = {
prefix: PREFIX
}

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 tracks = (state = initialState, action) => {
const baseActionType = stripPrefix(PREFIX, action.type)
const tracks = (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
Loading

0 comments on commit 334e9d5

Please sign in to comment.