Last active
October 4, 2023 03:32
-
-
Save korrio/66d644e522e95f06f7c92c8d34f3f5c3 to your computer and use it in GitHub Desktop.
dashboardSlice.tsx
This file contains hidden or 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
import { createAsyncThunk, createSlice, Draft } from '@reduxjs/toolkit'; | |
import { AppThunk, RootState } from '@/store/index'; | |
import Http from '@/api/Http'; | |
type DashboardState = { | |
title: string; | |
btc_pending: string; | |
btc_pending_usd: string; | |
capital: number; | |
your_income: string; | |
your_income_usd: number; | |
hash_power: number; | |
btc_per_day: string; | |
btc_per_day_usd: string; | |
btc_per_min: string; | |
btc_per_min_usd: string; | |
difficulty: string; | |
referral_count: number; | |
withdraw_received: number; | |
withdraw_pending: number; | |
withdraw_total: number; | |
isLoading: boolean; | |
}; | |
const initialState: DashboardState = { | |
"title": "", | |
"btc_pending": "", | |
"btc_pending_usd": "", | |
"capital": 0, | |
"your_income": "", | |
"your_income_usd": 0, | |
"hash_power": 0, | |
"btc_per_day": "", | |
"btc_per_day_usd": "", | |
"btc_per_min": "", | |
"btc_per_min_usd": "", | |
"difficulty": "", | |
"referral_count": 0, | |
"withdraw_received": 0, | |
"withdraw_pending": 0, | |
"withdraw_total": 0, | |
isLoading: false, | |
}; | |
// REDUCERS | |
export const dashboardSlice = createSlice({ | |
name: 'dashboard', | |
initialState, | |
reducers: { | |
setDashboard: (state: any, action: any) => { | |
}, | |
}, | |
extraReducers: (builder: any) => { | |
builder | |
.addCase(getDashboard.pending, (state:any) => { | |
state.isLoading = true; | |
}) | |
.addCase( | |
getDashboard.fulfilled, | |
(state: Draft<typeof initialState>, action:any) => { | |
state = action.payload | |
state.isLoading = false; | |
} | |
); | |
}, | |
}); | |
// ACTIONS | |
export const getDashboard = createAsyncThunk( | |
`/api/auth/dashboard`, | |
async (token:any, { rejectWithValue }) => { | |
try { | |
const response = await Http.get(`/api/auth/dashboard`, { | |
headers: { | |
Authorization: `Bearer ${token}`, | |
}, | |
}); | |
console.log("getDashboard",response.data) | |
return response.data as DashboardState; | |
} catch (err: any) { | |
if (!err.response) { | |
throw err; | |
} | |
return rejectWithValue({ | |
status: 'rejected', | |
message: err.response.data.errorMessage, | |
}); | |
} | |
} | |
); | |
export const dashboardSelector = (state: RootState) => state.dashboard as DashboardState; | |
export const { setDashboard } = dashboardSlice.actions; | |
export default dashboardSlice.reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment