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 1 commit
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"react-hook-form": "7.23.0",
"react-i18next": "11.15.3",
"react-query": "3.34.8",
"wouter": "2.8.0-alpha.2"
"wouter": "2.8.0-alpha.2",
"zustand": "3.7.1"
},
"msw": {
"workerDirectory": "public"
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const Header = ({ title, useAuth = _useAuth }: Props) => {
{isLoggedIn ? (
<>
<Button size="small" to="/count" label="Count" />
<Button size="small" to="/count_with_history" label="Undo/Redo" />
<Button size="small" onClick={logout} label="Logout" />
</>
) : null}
Expand Down
58 changes: 58 additions & 0 deletions src/pages/StateHistoryCount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useCountStore } from "@/stores/countStore";

const Count = () => {
const count = useCountStore((state) => state.curr);
return <p>You clicked {count}</p>;
};

const UndoCountButton: React.FC = () => {
const undo = useCountStore((state) => state.undo);
const hasPrev = useCountStore((state) => state.hasPrev());

return (
<button onClick={undo} disabled={!hasPrev}>
undo
</button>
);
};

const RedoCountButton: React.FC = () => {
const redo = useCountStore((state) => state.redo);
const hasNext = useCountStore((state) => state.hasNext());

return (
<button onClick={redo} disabled={!hasNext}>
redo
</button>
);
};

const SetCountButton: React.FC = ({}) => {
const getCount = useCountStore((state) => state.getCurr);
const set = useCountStore((state) => state.set);

return (
<>
<button onClick={() => set(getCount() - 1)}>-</button>
<button onClick={() => set(getCount() + 1)}>+</button>
</>
);
};

const ResetCountButton: React.FC = ({}) => {
const reset = useCountStore((state) => state.reset);

return <button onClick={() => reset(0)}>reset to 0</button>;
};

const StateHistoryCount = () => (
<>
<Count />
<SetCountButton />
<UndoCountButton />
<RedoCountButton />
<ResetCountButton />
</>
);
Comment on lines +48 to +56
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying out a counter with redo/undo functionality in zustand.
re-render appears to be prevented 👀

Kapture.2022-03-02.at.11.35.28.mp4


export default StateHistoryCount;
4 changes: 4 additions & 0 deletions src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Login = lazy(() => import("@/pages/Login"));
const Profile = lazy(() => import("@/pages/Profile"));
const NotFound = lazy(() => import("@/pages/NotFound"));
const SharedStateCount = lazy(() => import("@/pages/SharedStateCount"));
const StateHistoryCount = lazy(() => import("@/pages/StateHistoryCount"));

// TODO
jinmayamashita marked this conversation as resolved.
Show resolved Hide resolved
// RestrictAccess.tsx
Expand Down Expand Up @@ -68,6 +69,9 @@ export const Routes = () => {
<PrivateRoute path="/count">
<SharedStateCount />
</PrivateRoute>
<PrivateRoute path="/count_with_history">
<StateHistoryCount />
</PrivateRoute>
<Route>
<NotFound />
</Route>
Expand Down
55 changes: 55 additions & 0 deletions src/stores/countStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import create from "zustand";

type State = { prev: number[]; curr: number; next: number[] };

type Selectors = {
getCurr: () => State["curr"];
hasPrev: () => boolean;
hasNext: () => boolean;
};

type Actions = {
undo: () => void;
redo: () => void;
set: (value: State["curr"]) => void;
reset: (value: State["curr"]) => void;
};

type Store = State & Selectors & Actions;
export const useCountStore = create<Store>((set, get) => ({
// state
prev: [],
curr: 0,
next: [],

// selectors
getCurr: () => get().curr,
hasPrev: () => get().prev.length > 0,
hasNext: () => get().next.length > 0,

// actions
undo: () =>
set((state) => ({
prev: state.prev.slice(0, state.prev.length - 1),
curr: state.prev[state.prev.length - 1],
next: [state.curr, ...state.next],
})),
redo: () =>
set((state) => ({
prev: [...state.prev, state.curr],
curr: state.next[0],
next: state.next.slice(1),
})),
set: (newCurr) =>
set((state) => ({
prev: [...state.prev, state.curr],
curr: newCurr,
next: [],
})),
reset: (newCurr) =>
set({
prev: [],
curr: newCurr,
next: [],
}),
}));
3 changes: 2 additions & 1 deletion src/types/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type Path =
| "/home"
| "/profile"
| "/count"
| "/setcount";
| "/setcount"
| "/count_with_history";
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12176,6 +12176,11 @@ yocto-queue@^0.1.0:
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zustand@3.7.1:
version "3.7.1"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-3.7.1.tgz#7388f0a7175a6c2fd9a2880b383a4bf6cdf6b7c6"
integrity sha512-wHBCZlKj+bg03/hP+Tzv24YhnqqP8MCeN9ECPDXoF01062SIbnfl3j9O0znkDw1lNTY0a8WN3F///a0UhhaEqg==

zwitch@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/zwitch/-/zwitch-1.0.5.tgz#d11d7381ffed16b742f6af7b3f223d5cd9fe9920"
Expand Down