From 3eaa56f0306ead818f64c3d99fc6d230d9b970a4 Mon Sep 17 00:00:00 2001 From: Max Nanis Date: Wed, 18 Feb 2026 20:42:03 -0500 Subject: HERE WE GO, HERE WE GO, HERE WE GO --- jb-ui/src/models/appSlice.ts | 188 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 jb-ui/src/models/appSlice.ts (limited to 'jb-ui/src/models/appSlice.ts') 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) { + state.bpuid = action.payload; + }, + + setAssignmentID(state, action: PayloadAction) { + // 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) { + state.turkSubmitTo = action.payload; + }, + + setLOI(state, action: PayloadAction) { + state.loi = action.payload; + }, + + setAvailabilityCount(state, action: PayloadAction) { + state.availability_count = action.payload + }, + + setAttemptedLiveEligibleCount(state, action: PayloadAction) { + state.attempted_live_eligible_count = action.payload + }, + + setCurrentBuckets(state, action: PayloadAction) { + state.currentBucketRequested = moment.utc().unix(); + state.currentBuckets = action.payload + }, + + setOfferwallReasons(state, action: PayloadAction) { + 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) { + state.taskStatus = action.payload; + + state.bpuid = action.payload.product_user_id; + }, + + setUserWalletBalance(state, action: PayloadAction) { + state.userWalletBalance = action.payload; + }, + + setUserLedgerSummary(state, action: PayloadAction) { + state.userLedgerSummary = action.payload; + }, + + setUserLedgerTxs(state, action: PayloadAction) { + // 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) { + state.txPagination = action.payload; + }, + + setTxTotalItems(state, action: PayloadAction) { + state.txTotalItems = action.payload; + }, + + setTxTotalPages(state, action: PayloadAction) { + 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 = { + 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 -- cgit v1.2.3