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

This is my first attempt to refactor LoginWithContext example using Typescript #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions videos/why-i-love-usereducer/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LoginUseReducer from './LoginUseReducer';
import LoginUseReducerImmer from './LoginUseReducerImmer';
import LoginWithContext from './LoginWithContext';
import LoginUseReducerTypeScript from './LoginUseReducerTypeScript';
import LoginWithContextTypescript from './LoginWithContextTypescript';

function useLocationHash() {
const [hash, setHash] = useState(window.location.hash);
Expand Down Expand Up @@ -35,6 +36,7 @@ function App() {
useReducerImmer: LoginUseReducerImmer,
withContext: LoginWithContext,
useReducerTypeScript: LoginUseReducerTypeScript,
withContextTypeScript: LoginWithContextTypescript,
});
return (
<>
Expand All @@ -53,6 +55,9 @@ function App() {
<br />
<br />
<a href='#useReducerTypeScript'>LoginUseReducerTypeScript</a>
<br />
<br />
<a href='#withContextTypeScript'>LoginWithContextTypeScript</a>
</div>
)}
{CurrentRoute && <CurrentRoute />}
Expand Down
207 changes: 207 additions & 0 deletions videos/why-i-love-usereducer/src/LoginWithContextTypescript.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import React, { useContext } from "react";
import { useImmerReducer } from "use-immer";
import { login } from "./utils";

type Todo = { title: string; completed: boolean };
const presetTodos: Todo[] = [
{
title: "Get milk",
completed: true,
},
{
title: "Make YouTube video",
completed: false,
},
{
title: "Write blog post",
completed: false,
},
];

const initialState: LoginState = {
username: "",
password: "",
isLoading: false,
error: "",
isLoggedIn: false,
variant: "login",
todos: presetTodos,
};

interface LoginState {
username: string;
password: string;
isLoading: boolean;
error: string;
isLoggedIn: boolean;
variant: "login" | "forgetPassword";
todos: Todo[];
}

type LoginAction =
| { type: "login" | "success" | "error" | "logOut" }
| { type: "field"; fieldName: string; payload: string }
| { type: "toggleTodoCompleted"; payload: string };

function loginReducer(draft: LoginState, action: LoginAction) {
switch (action.type) {
case "field": {
draft[action.fieldName as "username" | "password"] = action.payload;
return;
}
case "login": {
draft.error = "";
draft.isLoading = true;
return;
}
case "success": {
draft.isLoggedIn = true;
return;
}
case "error": {
draft.error = "Incorrect username or password!";
draft.isLoggedIn = false;
draft.isLoading = false;
draft.username = "";
draft.password = "";
return;
}
case "logOut": {
draft.isLoggedIn = false;
draft.isLoading = false;
draft.username = "";
draft.password = "";
return;
}
case "toggleTodoCompleted": {
const todo = draft.todos.find((item) => item.title === action.payload);
if (todo) todo.completed = !todo.completed;
break;
}
default:
}
}

const StateContext = React.createContext<LoginState | null>(null);
const DispatchContext = React.createContext<React.Dispatch<any> | null>(null);

export default function LoginWithContextTypescript() {
const [state, dispatch] = useImmerReducer(loginReducer, initialState);
const {
username,
password,
isLoading,
error,
isLoggedIn,
todos,
} = state as LoginState;
const onSubmit = async (e: React.FormEvent) => {
e.preventDefault();

dispatch({ type: "login" });

try {
await login({ username, password });
dispatch({ type: "success" });
} catch (err) {
dispatch({ type: "error" });
}
};

return (
<DispatchContext.Provider value={dispatch}>
<StateContext.Provider value={state}>
<div className="App useContext">
<div className="login-container">
{isLoggedIn ? (
<>
<h1>Welcome {username}!</h1>
<button
type="button"
onClick={() => dispatch({ type: "logOut" })}
>
Log Out
</button>
</>
) : (
<form className="form" onSubmit={onSubmit}>
{error && <p className="error">{error}</p>}
<p>Please Login!</p>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) =>
dispatch({
type: "field",
fieldName: "username",
payload: e.currentTarget.value,
})
}
/>
<input
type="password"
placeholder="password"
autoComplete="new-password"
value={password}
onChange={(e) =>
dispatch({
type: "field",
fieldName: "password",
payload: e.currentTarget.value,
})
}
/>
<button className="submit" type="submit" disabled={isLoading}>
{isLoading ? "Logging in..." : "Log In"}
</button>
</form>
)}
</div>

<TodoPage theTodos={todos} />
</div>
</StateContext.Provider>
</DispatchContext.Provider>
);
}
interface TodoProps {
theTodos: Todo[];
}
function TodoPage({ theTodos }: TodoProps) {
return (
<div className="todoContainer">
<h2>Todos</h2>
{theTodos.map((item: Todo) => (
<TodoItem key={item.title} {...item} />
))}
</div>
);
}

function TodoItem({ title, completed }: Todo) {
const dispatch = useContext(DispatchContext);
const state = useContext(StateContext);
const { isLoggedIn } = state as LoginState;
return (
<div className="todoItem">
<p>{title}</p>
<div>
<input
type="checkbox"
checked={completed}
onClick={() => {
if (!isLoggedIn) {
alert("Please login to click this!");
}
}}
onChange={() => {
if (isLoggedIn && dispatch) {
dispatch({ type: "toggleTodoCompleted", payload: title });
}
}}
/>
</div>
</div>
);
}