Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-redux #41

Merged
merged 8 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@

### Integration tests and msw

### Login / Registration flow

[pull request](https://github.com/nickovchinnikov/coursesbox/pull/40)

### Login / Registration flow
### React-redux and user flow part 1

### React-redux in Jest and Storybook

[pull request](https://github.com/nickovchinnikov/coursesbox/pull/41)
126 changes: 113 additions & 13 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"next": "12.1.6",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-hook-form": "^7.30.0"
"react-hook-form": "^7.30.0",
"react-redux": "^8.0.1"
},
"devDependencies": {
"@babel/core": "^7.17.10",
Expand Down
11 changes: 8 additions & 3 deletions frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { AppProps } from "next/app";
import { Provider } from "react-redux";

import { store } from "@/store";

import { Layout } from "@/components/Layout";

function MyApp({ Component, pageProps }: AppProps) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
<Provider store={store}>
<Layout>
<Component {...pageProps} />
</Layout>
</Provider>
);
}

Expand Down
18 changes: 17 additions & 1 deletion frontend/pages/login.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { NextPage } from "next";
import Link from "next/link";
import { useRouter } from "next/router";
import { useSelector, useDispatch } from "react-redux";
import styled from "@emotion/styled";
import { useForm } from "react-hook-form";

import { RootState, AppDispatch } from "@/store";
import { selectUser, login } from "@/services/userSlice";

import { CenteredTile } from "@/components/Tile";
import { Input, ConditionalFeedback } from "@/components/Input";
import { Button } from "@/components/Button";
Expand All @@ -23,14 +28,25 @@ const Login: NextPage = () => {
handleSubmit,
formState: { errors },
} = useForm<LoginForm>();
const router = useRouter();

const { jwt, error } = useSelector<RootState, RootState["user"]>(selectUser);
const dispatch = useDispatch<AppDispatch>();

if (Boolean(jwt) && !error) {
router.push("/user");
}

const onSubmit = (data: LoginForm) => {
console.log(data);
dispatch(login(data));
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<CenteredTile header="Login">
<h3>
<ConditionalFeedback>{error?.message}</ConditionalFeedback>
</h3>
<StyledInput
label="Identifier"
placeholder="username or email"
Expand Down
32 changes: 25 additions & 7 deletions frontend/pages/user.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import type { NextPage } from "next";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";

import { RootState, AppDispatch } from "@/store";
import { selectUser, logout } from "@/services/userSlice";

import { CenteredTile } from "@/components/Tile";
import { Button } from "@/components/Button";

const User: NextPage = () => {
const userMock = { username: "John Doe", email: "john@doe.com" };
const router = useRouter();
const { username, email, error } = useSelector<RootState, RootState["user"]>(
selectUser
);
const dispatch = useDispatch<AppDispatch>();

const logoutHandler = async () => {
console.log("logout");
useEffect(() => {
if (!username || Boolean(error)) {
dispatch(logout());
router.push("/login");
}
}, []);

const logoutHandler = () => {
dispatch(logout());
router.push("/");
};

return (
return username && email ? (
<CenteredTile header="Profile">
<h3>username: {userMock.username}</h3>
<h3>email: {userMock.email}</h3>
<h3>username: {username}</h3>
<h3>email: {email}</h3>
<Button onClick={logoutHandler}>Logout</Button>
</CenteredTile>
);
) : null;
};

export default User;
8 changes: 2 additions & 6 deletions frontend/services/userSlice.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { storeCreator as globalStoreCreator } from "@/store";
import { configureStore } from "@reduxjs/toolkit";

import { mockUser, ValidationError, RegistrationError } from "@/mocks/user";

Expand All @@ -10,11 +10,7 @@ import {
registration,
} from "./userSlice";

const rootReducer = {
user: reducer,
};

const storeCreator = () => globalStoreCreator(rootReducer);
const storeCreator = () => configureStore({ reducer: { user: reducer } });

const updatedState = {
jwt: mockUser.jwt,
Expand Down
Loading