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

Refactoring for separation of concerns #26

Merged
merged 29 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
57a868c
good bye
test-user-jinma-ml Dec 9, 2021
5a53bd0
initialize with vite
test-user-jinma-ml Dec 9, 2021
e258b82
add simple pages
test-user-jinma-ml Dec 9, 2021
51de226
introduce msw
test-user-jinma-ml Dec 10, 2021
cf8fbf4
adjust mock api response
test-user-jinma-ml Dec 13, 2021
d5668bd
rename to http
test-user-jinma-ml Dec 13, 2021
a7e6255
implement auth hook
test-user-jinma-ml Dec 13, 2021
4e75e63
adapting to the existing style
test-user-jinma-ml Dec 15, 2021
6ea1cc5
adjusting layouts with routes
test-user-jinma-ml Dec 15, 2021
6d695f8
add delay to mock response
test-user-jinma-ml Dec 15, 2021
842067f
render error message
test-user-jinma-ml Dec 15, 2021
b85d330
npx npm-check-updates -u
jinmayamashita Jan 14, 2022
415e4c0
example useMutation hook
jinmayamashita Jan 14, 2022
11b92de
update user type
jinmayamashita Jan 14, 2022
7172283
try use constate
jinmayamashita Jan 14, 2022
b29e2dc
add separate components
jinmayamashita Jan 14, 2022
0786075
add storybook
jinmayamashita Jan 14, 2022
e8b699a
try testing button with storybook
jinmayamashita Jan 17, 2022
03b7b25
introduce i18next
jinmayamashita Jan 17, 2022
a520ee9
specify the version of node
jinmayamashita Jan 17, 2022
d3f4fba
add type check script
jinmayamashita Jan 17, 2022
06908af
adjust demo styles
jinmayamashita Jan 17, 2022
d7acb04
count component to simply
jinmayamashita Jan 17, 2022
dc95587
use lazy loads
jinmayamashita Jan 17, 2022
fd316ca
try to zustand
jinmayamashita Mar 2, 2022
91c4b73
use css modules with classNames
jinmayamashita Mar 2, 2022
5f94e94
Update __mocks__/api/login.ts
jinmayamashita Mar 2, 2022
08f9c22
Update src/pages/NotFound.tsx
jinmayamashita Mar 2, 2022
544abba
Update src/routes.tsx
jinmayamashita Mar 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_REACT_APP_API_HOST=https://reqres.in/api
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand All @@ -21,3 +22,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env variables
.env
22 changes: 22 additions & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require("path");

module.exports = {
stories: ["../src/**/*.stories.@(js|jsx|ts|tsx)"],
framework: "@storybook/react",
core: {
builder: "storybook-builder-vite",
},
async viteFinal(config) {
return {
...config,
resolve: {
alias: [
{
find: "@",
replacement: path.resolve(__dirname, "../src"),
},
],
},
};
},
};
9 changes: 9 additions & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
46 changes: 0 additions & 46 deletions README.md

This file was deleted.

13 changes: 13 additions & 0 deletions __mocks__/api/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { rest } from "../rest";

export const login = [
rest.post("/login", (_, res, ctx) =>
res(
// Delays response for 2000ms.
jinmayamashita marked this conversation as resolved.
Show resolved Hide resolved
ctx.delay(1000),
ctx.json({
token: "QpwL5tke4Pnpja7X4",
})
)
),
];
61 changes: 61 additions & 0 deletions __mocks__/api/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { rest } from "../rest";
import { getItem, setItem, setInitalItem } from "../mockDatabase";

const testUser = {
id: 1,
email: "george.bluth@reqres.in",
first_name: "George",
last_name: "Bluth",
avatar: "https://reqres.in/img/faces/1-image.jpg",
avatar_size_width: 128,
avatar_size_height: 128,
};
setInitalItem({ testUser });

// Validate a token in the backend that is specific to a user
const isValidatedAccessToken = (token?: string) => !!token;

export const user = [
rest.get("/user/1", (req, res, ctx) => {
const token = req.headers.get("Authorization")?.split(" ").at(-1);

if (!isValidatedAccessToken(token)) {
return res(
ctx.status(401),
ctx.json({
error: "Invalid Access Token",
})
);
}

return res(ctx.json(getItem("testUser")));
}),
rest.post<{ email: string }>("/user/1", (req, res, ctx) => {
const token = req.headers.get("Authorization")?.split(" ").at(-1);

if (!isValidatedAccessToken(token)) {
return res(
ctx.status(401),
ctx.json({
error: "Invalid Access Token",
})
);
}

if (!req.body.email || typeof req.body.email !== "string") {
return res(
ctx.status(400),
ctx.json({
error: "Invalid Payload",
})
);
}

setItem("testUser", {
...testUser,
email: req.body.email,
});

return res(ctx.json({ message: "Update success" }));
}),
];
11 changes: 11 additions & 0 deletions __mocks__/mockDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const setItem = (itemName: string, data: any) => {
localStorage.setItem(itemName, JSON.stringify(data));
};

export const getItem = (itemName: string) =>
JSON.parse(localStorage.getItem(itemName) || "");

export const setInitalItem = (o: Record<string, any>) =>
Object.keys(o).map((k) => {
!localStorage.getItem(k) && localStorage.setItem(k, JSON.stringify(o[k]));
});
9 changes: 9 additions & 0 deletions __mocks__/rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { rest as _rest } from "msw";

const BASE_API_URL = "/api/v1";

// Not support yet: allow to define general configuration https://github.com/mswjs/msw/issues/617
export const rest = {
get: (url: string, fn) => _rest.get(`${BASE_API_URL}${url}`, fn),
post: (url: string, fn) => _rest.post(`${BASE_API_URL}${url}`, fn),
} as typeof _rest;
7 changes: 7 additions & 0 deletions __mocks__/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { setupWorker } from "msw";
import { user } from "./api/user";
import { login } from "./api/login";

const requestHandler = [...login, ...user];

export const mockServer = setupWorker(...requestHandler);
19 changes: 19 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
[
"@babel/preset-react",
{
runtime: "automatic",
},
],
"@babel/preset-typescript",
],
};
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
1 change: 1 addition & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
92 changes: 59 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
{
"name": "react-boilerplate",
"name": "reactjs-boilerplate",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
"repository": "git@github.com:monstar-lab-oss/reactjs-boilerplate.git",
"author": "山下 仁麻 <jinma.yamashita@monstar-lab.com>",
"engines": {
"node": ">=16.x"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"test": "jest",
"type": "tsc --noEmit"
},
"devDependencies": {
"@babel/core": "7.16.7",
"@babel/preset-env": "7.16.8",
"@babel/preset-react": "7.16.7",
"@babel/preset-typescript": "7.16.7",
"@storybook/addon-actions": "6.5.0-alpha.16",
"@storybook/addon-essentials": "6.5.0-alpha.16",
"@storybook/addon-links": "6.5.0-alpha.16",
"@storybook/react": "6.5.0-alpha.16",
"@storybook/testing-react": "1.2.3",
"@testing-library/jest-dom": "5.16.1",
"@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.5.0",
"@types/node": "17.0.8",
"@types/react": "17.0.38",
"@types/react-dom": "17.0.11",
"@vitejs/plugin-react": "1.1.4",
"babel-loader": "8.2.3",
"identity-obj-proxy": "3.0.0",
"jest": "27.4.7",
"msw": "0.36.4",
"sass": "1.48.0",
"storybook-builder-vite": "0.1.13",
"typescript": "4.5.4",
"vite": "2.7.12"
},
"dependencies": {
"classnames": "2.3.1",
"constate": "3.3.0",
"i18next": "21.6.6",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-hook-form": "7.23.0",
"react-i18next": "11.15.3",
"react-query": "3.34.8",
"wouter": "2.8.0-alpha.2",
"zustand": "3.7.1"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
"msw": {
"workerDirectory": "public"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
"jest": {
"testEnvironment": "jsdom",
"setupFilesAfterEnv": [
"<rootDir>/jest-setup.ts"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
"moduleNameMapper": {
"\\.(css|scss)$": "identity-obj-proxy"
}
}
}
Binary file removed public/favicon.ico
Binary file not shown.
43 changes: 0 additions & 43 deletions public/index.html

This file was deleted.

Binary file removed public/logo192.png
Binary file not shown.
Binary file removed public/logo512.png
Binary file not shown.