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; } );