generated from IvettaGoIT/react_vite
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from MargoMarm/features/statisticOnHomePage
added dynamic changes to VideoCountExercises and BurnedCalories stati…
- Loading branch information
Showing
10 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import axios from 'axios'; | ||
import { toast } from 'react-toastify'; | ||
|
||
axios.defaults.baseURL = 'https://power-pulse-rest-api.onrender.com'; | ||
|
||
export const getVideoCountAndBurnedCaloriesStatistics = createAsyncThunk( | ||
'getVideoCountAndBurnedCaloriesStatistics', | ||
async (_, { rejectWithValue }) => { | ||
try { | ||
const { data } = await axios.get('/api/statistics'); | ||
return data; | ||
} catch (error) { | ||
toast.error('Oops... Something went wrong! Try again!'); | ||
return rejectWithValue('Oops... Something went wrong!'); | ||
} | ||
}, | ||
{ | ||
condition: (_, { getState }) => { | ||
const state = getState(); | ||
if (state.statistics.allExercises > 1) { | ||
return false; | ||
} | ||
}, | ||
}, | ||
); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const getAllExercises = state => state.statistics.allExercises; | ||
|
||
export const getAllUsers = state => state.statistics.allUsers; | ||
|
||
export const getUsersBurnedCalories = state => state.statistics.usersBurnedCalories; | ||
|
||
export const getUsersTimeTraining = state => state.statistics.usersTimeTraining; | ||
|
||
export const getUsersTraining = state => state.statistics.usersTraining; | ||
|
||
export const isLoadingStatictics = state => state.statistics.isLoading; | ||
|
||
export const getErrorStatistics = state => state.statistics.error; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { getVideoCountAndBurnedCaloriesStatistics } from './operations'; | ||
import formatNumber from '../../utils/formatNumberStatistics'; | ||
|
||
const contactsInitialValue = { | ||
isLoading: false, | ||
error: null, | ||
allExercises: 0, | ||
allUsers: 0, | ||
usersBurnedCalories: 0, | ||
usersTimeTraining: 0, | ||
usersTraining: 0, | ||
}; | ||
|
||
const handlePending = state => { | ||
state.isLoading = true; | ||
state.error = null; | ||
}; | ||
|
||
const handleFullfield = state => { | ||
state.isLoading = false; | ||
state.error = null; | ||
}; | ||
|
||
const handleRejected = (state, payload) => { | ||
state.isLoading = false; | ||
state.error = payload.error; | ||
}; | ||
|
||
const getStatistics = createSlice({ | ||
name: 'statistics', | ||
initialState: contactsInitialValue, | ||
extraReducers: builder => { | ||
builder.addCase( | ||
getVideoCountAndBurnedCaloriesStatistics.pending, | ||
handlePending, | ||
); | ||
builder.addCase( | ||
getVideoCountAndBurnedCaloriesStatistics.fulfilled, | ||
(state, { payload }) => { | ||
handleFullfield(state, payload); | ||
state.allExercises = payload.AllExercises; | ||
state.allUsers = payload.AllUsers; | ||
state.usersBurnedCalories = formatNumber(payload.usersBurnedCalories); | ||
state.usersTimeTraining = payload.usersTimeTraining; | ||
state.usersTraining = payload.usersTraining; | ||
}, | ||
); | ||
builder.addCase( | ||
getVideoCountAndBurnedCaloriesStatistics.rejected, | ||
handleRejected, | ||
); | ||
}, | ||
}); | ||
|
||
export const statisticsReducer = getStatistics.reducer; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function formatNumber(number) { | ||
if (number >= 1e6) { | ||
return (number / 1e6).toFixed(1) + 'M'; | ||
} else if (number >= 1e3) { | ||
return (number / 1e3).toFixed(1) + 'K'; | ||
} else { | ||
return number.toString(); | ||
} | ||
} | ||
|
||
export default formatNumber; |