1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import {
TaskStatusResponse,
TopNPlusBucket,
UserLedgerTransactionsResponseTransactionsInner,
UserLedgerTransactionTypesSummary,
UserWalletBalance,
OfferwallReason
} from "@/api_fsb";
import { App } from "@/models/app";
import { RootState } from "@/store";
import { createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { PaginationState } from '@tanstack/react-table';
import moment from "moment";
const initialState: App = {
bpuid: undefined,
assignment_id: undefined,
loi: 1800,
currentBuckets: undefined,
currentBucketEntered: undefined,
taskStatus: undefined,
offerwall_reasons: [],
userWalletBalance: undefined,
userLedgerSummary: undefined,
userLedgerTxCount: undefined,
userLedgerTxs: [],
txPagination: { pageIndex: 0, pageSize: 10 },
txTotalItems: undefined,
txTotalPages: undefined
} as App
const appSlice = createSlice({
name: 'app',
initialState,
reducers: {
setProductUserID(state, action: PayloadAction<string>) {
state.bpuid = action.payload;
},
setAssignmentID(state, action: PayloadAction<string>) {
// This is really so silly. Amazon should simply not send
// anything if it's unavailable.
if (action.payload === "ASSIGNMENT_ID_NOT_AVAILABLE") {
state.assignment_id = undefined;
}
state.assignment_id = action.payload;
},
setTurkSubmitTo(state, action: PayloadAction<string>) {
state.turkSubmitTo = action.payload;
},
setLOI(state, action: PayloadAction<number>) {
state.loi = action.payload;
},
setAvailabilityCount(state, action: PayloadAction<number>) {
state.availability_count = action.payload
},
setAttemptedLiveEligibleCount(state, action: PayloadAction<number>) {
state.attempted_live_eligible_count = action.payload
},
setCurrentBuckets(state, action: PayloadAction<TopNPlusBucket[] | undefined>) {
state.currentBucketRequested = moment.utc().unix();
state.currentBuckets = action.payload
},
setOfferwallReasons(state, action: PayloadAction<OfferwallReason[]>) {
state.offerwall_reasons = action.payload;
},
setEnteredTimestamp(state) {
// Go back by 2 seconds to account for any time drift
state.currentBucketEntered = moment.utc().unix() - 2;
},
setTaskStatus(state, action: PayloadAction<TaskStatusResponse>) {
state.taskStatus = action.payload;
state.bpuid = action.payload.product_user_id;
},
setUserWalletBalance(state, action: PayloadAction<UserWalletBalance>) {
state.userWalletBalance = action.payload;
},
setUserLedgerSummary(state, action: PayloadAction<UserLedgerTransactionTypesSummary>) {
state.userLedgerSummary = action.payload;
},
setUserLedgerTxs(state, action: PayloadAction<UserLedgerTransactionsResponseTransactionsInner[]>) {
// We're not appending the transaction details, it's only going
// to reassign the current page of Transactions.
state.userLedgerTxs = action.payload;
},
setTxPagination(state, action: PayloadAction<PaginationState>) {
state.txPagination = action.payload;
},
setTxTotalItems(state, action: PayloadAction<number>) {
state.txTotalItems = action.payload;
},
setTxTotalPages(state, action: PayloadAction<number>) {
state.txTotalPages = action.payload;
}
}
})
export const {
setProductUserID,
setAssignmentID,
setTurkSubmitTo,
setLOI,
setAvailabilityCount,
setAttemptedLiveEligibleCount,
setOfferwallReasons,
setCurrentBuckets,
setEnteredTimestamp,
setTaskStatus,
setUserWalletBalance,
setUserLedgerSummary,
setUserLedgerTxs,
setTxPagination,
setTxTotalItems,
setTxTotalPages
} = appSlice.actions;
export default appSlice.reducer
export const getLOIText = createSelector(
[(state: RootState) => state.app],
(app_config): string => {
const lookup: Record<number, string> = {
600: "ten",
1200: "twenty",
1800: "thirty",
};
return lookup[app_config.loi] || " – ";
}
);
export const getAvailabilityCount = createSelector(
[(state: RootState) => state.app],
(app_config): number | undefined => {
return app_config.availability_count;
}
);
export const isLowBalance = createSelector(
[(state: RootState) => state.app],
(app_config): boolean => {
const bal_amt = app_config.userWalletBalance?.amount ?? 0
return bal_amt <= -90;
}
);
export const selectBucket = createSelector(
[
(state: RootState) => state.app.currentBuckets,
],
(buckets): TopNPlusBucket | null => {
if (buckets && buckets.length >= 1) {
return buckets[0]
}
return null;
}
);
export const getSurveyURL = createSelector(
[selectBucket],
(bucket): string | null => {
return bucket?.uri || null;
}
);
|