Skip to content

Commit c3136e0

Browse files
Merge pull request #26 from monstar-lab-oss/try-simply-folders
Refactoring for separation of concerns
2 parents c0ad8ee + 544abba commit c3136e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+8141
-6273
lines changed

.env.template

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_REACT_APP_API_HOST=https://reqres.in/api

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
/dist
1314

1415
# misc
1516
.DS_Store
@@ -21,3 +22,6 @@
2122
npm-debug.log*
2223
yarn-debug.log*
2324
yarn-error.log*
25+
26+
# env variables
27+
.env

.storybook/main.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
stories: ["../src/**/*.stories.@(js|jsx|ts|tsx)"],
5+
framework: "@storybook/react",
6+
core: {
7+
builder: "storybook-builder-vite",
8+
},
9+
async viteFinal(config) {
10+
return {
11+
...config,
12+
resolve: {
13+
alias: [
14+
{
15+
find: "@",
16+
replacement: path.resolve(__dirname, "../src"),
17+
},
18+
],
19+
},
20+
};
21+
},
22+
};

.storybook/preview.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const parameters = {
2+
actions: { argTypesRegex: "^on[A-Z].*" },
3+
controls: {
4+
matchers: {
5+
color: /(background|color)$/i,
6+
date: /Date$/,
7+
},
8+
},
9+
}

README.md

-46
This file was deleted.

__mocks__/api/login.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { rest } from "../rest";
2+
3+
export const login = [
4+
rest.post("/login", (_, res, ctx) =>
5+
res(
6+
// Delays response for 1000ms.
7+
ctx.delay(1000),
8+
ctx.json({
9+
token: "QpwL5tke4Pnpja7X4",
10+
})
11+
)
12+
),
13+
];

__mocks__/api/user.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { rest } from "../rest";
2+
import { getItem, setItem, setInitalItem } from "../mockDatabase";
3+
4+
const testUser = {
5+
id: 1,
6+
7+
first_name: "George",
8+
last_name: "Bluth",
9+
avatar: "https://reqres.in/img/faces/1-image.jpg",
10+
avatar_size_width: 128,
11+
avatar_size_height: 128,
12+
};
13+
setInitalItem({ testUser });
14+
15+
// Validate a token in the backend that is specific to a user
16+
const isValidatedAccessToken = (token?: string) => !!token;
17+
18+
export const user = [
19+
rest.get("/user/1", (req, res, ctx) => {
20+
const token = req.headers.get("Authorization")?.split(" ").at(-1);
21+
22+
if (!isValidatedAccessToken(token)) {
23+
return res(
24+
ctx.status(401),
25+
ctx.json({
26+
error: "Invalid Access Token",
27+
})
28+
);
29+
}
30+
31+
return res(ctx.json(getItem("testUser")));
32+
}),
33+
rest.post<{ email: string }>("/user/1", (req, res, ctx) => {
34+
const token = req.headers.get("Authorization")?.split(" ").at(-1);
35+
36+
if (!isValidatedAccessToken(token)) {
37+
return res(
38+
ctx.status(401),
39+
ctx.json({
40+
error: "Invalid Access Token",
41+
})
42+
);
43+
}
44+
45+
if (!req.body.email || typeof req.body.email !== "string") {
46+
return res(
47+
ctx.status(400),
48+
ctx.json({
49+
error: "Invalid Payload",
50+
})
51+
);
52+
}
53+
54+
setItem("testUser", {
55+
...testUser,
56+
email: req.body.email,
57+
});
58+
59+
return res(ctx.json({ message: "Update success" }));
60+
}),
61+
];

__mocks__/mockDatabase.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const setItem = (itemName: string, data: any) => {
2+
localStorage.setItem(itemName, JSON.stringify(data));
3+
};
4+
5+
export const getItem = (itemName: string) =>
6+
JSON.parse(localStorage.getItem(itemName) || "");
7+
8+
export const setInitalItem = (o: Record<string, any>) =>
9+
Object.keys(o).map((k) => {
10+
!localStorage.getItem(k) && localStorage.setItem(k, JSON.stringify(o[k]));
11+
});

__mocks__/rest.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { rest as _rest } from "msw";
2+
3+
const BASE_API_URL = "/api/v1";
4+
5+
// Not support yet: allow to define general configuration https://github.com/mswjs/msw/issues/617
6+
export const rest = {
7+
get: (url: string, fn) => _rest.get(`${BASE_API_URL}${url}`, fn),
8+
post: (url: string, fn) => _rest.post(`${BASE_API_URL}${url}`, fn),
9+
} as typeof _rest;

__mocks__/server.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { setupWorker } from "msw";
2+
import { user } from "./api/user";
3+
import { login } from "./api/login";
4+
5+
const requestHandler = [...login, ...user];
6+
7+
export const mockServer = setupWorker(...requestHandler);

babel.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
"@babel/preset-env",
5+
{
6+
targets: {
7+
node: "current",
8+
},
9+
},
10+
],
11+
[
12+
"@babel/preset-react",
13+
{
14+
runtime: "automatic",
15+
},
16+
],
17+
"@babel/preset-typescript",
18+
],
19+
};

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>React App</title>
7+
</head>
8+
<body>
9+
<noscript>You need to enable JavaScript to run this app.</noscript>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

jest-setup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom";

package.json

+59-33
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,69 @@
11
{
2-
"name": "react-boilerplate",
2+
"name": "reactjs-boilerplate",
33
"version": "0.1.0",
44
"private": true,
5-
"dependencies": {
6-
"@testing-library/jest-dom": "^5.11.4",
7-
"@testing-library/react": "^11.1.0",
8-
"@testing-library/user-event": "^12.1.10",
9-
"@types/jest": "^26.0.15",
10-
"@types/node": "^12.0.0",
11-
"@types/react": "^17.0.0",
12-
"@types/react-dom": "^17.0.0",
13-
"react": "^17.0.2",
14-
"react-dom": "^17.0.2",
15-
"react-scripts": "4.0.3",
16-
"typescript": "^4.1.2",
17-
"web-vitals": "^1.0.1"
5+
"repository": "[email protected]:monstar-lab-oss/reactjs-boilerplate.git",
6+
"author": "山下 仁麻 <[email protected]>",
7+
"engines": {
8+
"node": ">=16.x"
189
},
1910
"scripts": {
20-
"start": "react-scripts start",
21-
"build": "react-scripts build",
22-
"test": "react-scripts test",
23-
"eject": "react-scripts eject"
11+
"dev": "vite",
12+
"build": "tsc && vite build",
13+
"preview": "vite preview",
14+
"storybook": "start-storybook -p 6006",
15+
"build-storybook": "build-storybook",
16+
"test": "jest",
17+
"type": "tsc --noEmit"
18+
},
19+
"devDependencies": {
20+
"@babel/core": "7.16.7",
21+
"@babel/preset-env": "7.16.8",
22+
"@babel/preset-react": "7.16.7",
23+
"@babel/preset-typescript": "7.16.7",
24+
"@storybook/addon-actions": "6.5.0-alpha.16",
25+
"@storybook/addon-essentials": "6.5.0-alpha.16",
26+
"@storybook/addon-links": "6.5.0-alpha.16",
27+
"@storybook/react": "6.5.0-alpha.16",
28+
"@storybook/testing-react": "1.2.3",
29+
"@testing-library/jest-dom": "5.16.1",
30+
"@testing-library/react": "12.1.2",
31+
"@testing-library/user-event": "13.5.0",
32+
"@types/node": "17.0.8",
33+
"@types/react": "17.0.38",
34+
"@types/react-dom": "17.0.11",
35+
"@vitejs/plugin-react": "1.1.4",
36+
"babel-loader": "8.2.3",
37+
"identity-obj-proxy": "3.0.0",
38+
"jest": "27.4.7",
39+
"msw": "0.36.4",
40+
"sass": "1.48.0",
41+
"storybook-builder-vite": "0.1.13",
42+
"typescript": "4.5.4",
43+
"vite": "2.7.12"
44+
},
45+
"dependencies": {
46+
"classnames": "2.3.1",
47+
"constate": "3.3.0",
48+
"i18next": "21.6.6",
49+
"react": "17.0.2",
50+
"react-dom": "17.0.2",
51+
"react-hook-form": "7.23.0",
52+
"react-i18next": "11.15.3",
53+
"react-query": "3.34.8",
54+
"wouter": "2.8.0-alpha.2",
55+
"zustand": "3.7.1"
2456
},
25-
"eslintConfig": {
26-
"extends": [
27-
"react-app",
28-
"react-app/jest"
29-
]
57+
"msw": {
58+
"workerDirectory": "public"
3059
},
31-
"browserslist": {
32-
"production": [
33-
">0.2%",
34-
"not dead",
35-
"not op_mini all"
60+
"jest": {
61+
"testEnvironment": "jsdom",
62+
"setupFilesAfterEnv": [
63+
"<rootDir>/jest-setup.ts"
3664
],
37-
"development": [
38-
"last 1 chrome version",
39-
"last 1 firefox version",
40-
"last 1 safari version"
41-
]
65+
"moduleNameMapper": {
66+
"\\.(css|scss)$": "identity-obj-proxy"
67+
}
4268
}
4369
}

public/favicon.ico

-3.78 KB
Binary file not shown.

public/index.html

-43
This file was deleted.

public/logo192.png

-5.22 KB
Binary file not shown.

public/logo512.png

-9.44 KB
Binary file not shown.

0 commit comments

Comments
 (0)