Skip to content

Commit

Permalink
Merge pull request #105 from MargoMarm/features/loaderForSignUpSignIn…
Browse files Browse the repository at this point in the history
…Page

added loader on SignUp SignIn page
  • Loading branch information
nzend authored Sep 29, 2023
2 parents 7b5602c + ba55061 commit 87ac491
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
26 changes: 26 additions & 0 deletions src/components/LoaderForPages/LoaderForPages.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { LoaderContainer } from './LoaderForPages.styled';

import { Puff } from 'react-loader-spinner';

const LoaderForPages = () => {
return (
<LoaderContainer>
<Puff
height={'100'}
width={'100'}
radius={1}
color="#E6533C"
ariaLabel="puff-loading"
wrapperStyle={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}
wrapperClass=""
visible={true}
/>
</LoaderContainer>
);
};

export default LoaderForPages;
13 changes: 13 additions & 0 deletions src/components/LoaderForPages/LoaderForPages.styled.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import styled from '@emotion/styled';

export const LoaderContainer = styled.div`
position: fixed;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
z-index: 200;
background-color: rgba(0, 0, 0, 0.5);
`;
20 changes: 14 additions & 6 deletions src/components/SharedLayout/SharedLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { Outlet } from 'react-router-dom';
import { Container } from './SheradLayout.styled';
import Header from '../../components/headersComp/Header/Header';
import Loader from '../../components/Lodaer/Loader';
import { useSelector } from 'react-redux';
import { isLoadingSignInOrSignUp } from '../../redux/auth/selectors';
import LoaderForPages from '../LoaderForPages/LoaderForPages';

const SharedLayout = () => {
const isLoading = useSelector(isLoadingSignInOrSignUp);

return (
<Container>
<Header />
<Suspense fallback={<Loader needToCenter/>}>
<Outlet />
</Suspense>
</Container>
<>
{isLoading && <LoaderForPages />}
<Container>
<Header />
<Suspense fallback={<Loader needToCenter />}>
<Outlet />
</Suspense>
</Container>
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/redux/auth/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const authUser = createAsyncThunk(
return data;
} catch (error) {
toast.error('Oops... Something went wrong! Try again!');
return rejectWithValue('Oops... Something went wrong!');
return rejectWithValue(error.response.data.message);
}
},
);
Expand Down
1 change: 1 addition & 0 deletions src/redux/auth/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export const selectDailyСalories = state => state.auth.user.dailyСalories;

export const selectError = state => state.auth.error;

export const isLoadingSignInOrSignUp = state => state.auth.isLoading;
18 changes: 16 additions & 2 deletions src/redux/auth/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const initialState = {
token: null,
isLoggedIn: false,
isRefreshing: false,
isLoading: false,
};

const replaceUserState = (state, payload) => {
Expand All @@ -40,15 +41,22 @@ export const authSlice = createSlice({
initialState,
reducers: {},
extraReducers: builder => {
builder.addCase(authUser.pending, state => {
state.error = null;
state.isLoading = true;
});
builder.addCase(authUser.fulfilled, (state, action) => {
state.token = action.payload.token;
state.user.name = action.payload.name;
state.user.email = action.payload.email;
state.isLoggedIn = true;
state.error = null;
state.isLoading = false;
});
builder.addCase(authUser.rejected, (state, action) => {
state.error = action.payload;
builder.addCase(authUser.rejected, (state, { payload }) => {
state.error = payload;
Notify.failure(payload);
state.isLoading = false;
});

builder.addCase(updateBodyParts.fulfilled, (state, { payload }) => {
Expand All @@ -60,13 +68,19 @@ export const authSlice = createSlice({
state.token = payload.token;
state.isLoggedIn = true;
state.error = null;
state.isLoading = false;
});
builder.addCase(logInUser.rejected, (state, action) => {
state.error = action.payload;
state.isLoading = false;
Notify.failure(
'Oops... Something went wrong! Enter correct email or password',
);
});
builder.addCase(logInUser.pending, state => {
state.error = null;
state.isLoading = true;
});

builder.addCase(logOutUser.fulfilled, state => {
state.user.name = null;
Expand Down

0 comments on commit 87ac491

Please sign in to comment.