-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The previous solution was hard to understand what's the difference between transactions and activities. Instead of using the term transaction, we will keep all activities and use selectors to check which ones are completed. While `latestActivities` will be used to display notifications to the user.
- Loading branch information
1 parent
acd81b7
commit 6f45140
Showing
10 changed files
with
60 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { selectCompletedActivities } from "#/store/wallet" | ||
import { useAppSelector } from "./useAppSelector" | ||
|
||
export function useCompletedActivities() { | ||
return useAppSelector(selectCompletedActivities) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { selectLatestActivities } from "#/store/wallet" | ||
import { useAppSelector } from "./useAppSelector" | ||
|
||
export function useLatestActivities() { | ||
return useAppSelector(selectLatestActivities) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { Activity } from "#/types" | ||
import { createSelector } from "@reduxjs/toolkit" | ||
import { isActivityCompleted } from "#/utils" | ||
import { RootState } from ".." | ||
|
||
export const selectActivities = createSelector( | ||
(state: RootState) => state.wallet.activities, | ||
(activities) => Object.values(activities), | ||
export const selectLatestActivities = createSelector( | ||
(state: RootState) => state.wallet.latestActivities, | ||
(latestActivities) => Object.values(latestActivities), | ||
) | ||
|
||
export const selectTransactions = (state: RootState): Activity[] => | ||
state.wallet.transactions | ||
export const selectCompletedActivities = createSelector( | ||
(state: RootState) => state.wallet.activities, | ||
(activities) => | ||
activities.filter((activity) => isActivityCompleted(activity)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,56 @@ | ||
import { ActivitiesByIds, Activity } from "#/types" | ||
import { isActivityCompleted } from "#/utils" | ||
import { PayloadAction, createSlice } from "@reduxjs/toolkit" | ||
|
||
type WalletState = { | ||
activities: ActivitiesByIds | ||
transactions: Activity[] | ||
latestActivities: ActivitiesByIds | ||
activities: Activity[] | ||
} | ||
|
||
const initialState: WalletState = { | ||
activities: {}, | ||
transactions: [], | ||
latestActivities: {}, | ||
activities: [], | ||
} | ||
|
||
export const walletSlice = createSlice({ | ||
name: "wallet", | ||
initialState, | ||
reducers: { | ||
setTransactions(state, action: PayloadAction<Activity[]>) { | ||
state.transactions = action.payload | ||
}, | ||
setActivities(state, action: PayloadAction<Activity[]>) { | ||
const newActivitiesByIds = Object.fromEntries( | ||
action.payload.map((activity) => [activity.id, activity]), | ||
const allActivities = action.payload | ||
|
||
const pendingActivities = allActivities.reduce<[string, Activity][]>( | ||
(acc, activity) => { | ||
if (!isActivityCompleted(activity)) acc.push([activity.id, activity]) | ||
|
||
return acc | ||
}, | ||
[], | ||
) | ||
const newActivitiesIds = new Set(Object.keys(newActivitiesByIds)) | ||
const pendingActivitiesByIds = Object.fromEntries(pendingActivities) | ||
const pendingActivitiesIds = Object.keys(pendingActivities) | ||
|
||
const { latestActivities } = state | ||
const updatedActivitiesByIds = Object.values( | ||
state.activities, | ||
latestActivities, | ||
).reduce<ActivitiesByIds>((acc, activity) => { | ||
if (!newActivitiesIds.has(activity.id)) { | ||
if (!pendingActivitiesIds.includes(activity.id)) | ||
acc[activity.id] = { ...activity, status: "completed" } | ||
} | ||
|
||
return acc | ||
}, {}) | ||
|
||
state.activities = Object.assign( | ||
state.activities = allActivities | ||
state.latestActivities = Object.assign( | ||
updatedActivitiesByIds, | ||
newActivitiesByIds, | ||
pendingActivitiesByIds, | ||
) | ||
}, | ||
deleteActivity(state, action: PayloadAction<string>) { | ||
deleteLatestActivity(state, action: PayloadAction<string>) { | ||
const activityId = action.payload | ||
delete state.activities[activityId] | ||
delete state.latestActivities[activityId] | ||
}, | ||
}, | ||
}) | ||
|
||
export const { setTransactions, setActivities, deleteActivity } = | ||
walletSlice.actions | ||
export const { setActivities, deleteLatestActivity } = walletSlice.actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Activity } from "#/types" | ||
|
||
export const isActivityCompleted = (activity: Activity): boolean => | ||
activity.status === "completed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters