summaryrefslogtreecommitdiff
path: root/jb-ui/src/models/appSlice.ts
diff options
context:
space:
mode:
authorMax Nanis2026-02-18 20:42:03 -0500
committerMax Nanis2026-02-18 20:42:03 -0500
commit3eaa56f0306ead818f64c3d99fc6d230d9b970a4 (patch)
tree9fecc2f1456e6321572e0e65f57106916df173e2 /jb-ui/src/models/appSlice.ts
downloadamt-jb-3eaa56f0306ead818f64c3d99fc6d230d9b970a4.tar.gz
amt-jb-3eaa56f0306ead818f64c3d99fc6d230d9b970a4.zip
HERE WE GO, HERE WE GO, HERE WE GO
Diffstat (limited to 'jb-ui/src/models/appSlice.ts')
-rw-r--r--jb-ui/src/models/appSlice.ts188
1 files changed, 188 insertions, 0 deletions
diff --git a/jb-ui/src/models/appSlice.ts b/jb-ui/src/models/appSlice.ts
new file mode 100644
index 0000000..87952d9
--- /dev/null
+++ b/jb-ui/src/models/appSlice.ts
@@ -0,0 +1,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;
+ }
+); \ No newline at end of file