-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chungheon_yi
committed
Nov 22, 2024
1 parent
1a63ed3
commit f5dc84f
Showing
9 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function deepClone(value: any) { | ||
// @ts-ignore | ||
return global.structuredClone ? structuredClone?.(value) : JSON.parse(JSON.stringify(value)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './deepClone' | ||
export * from './ProxyWrapper' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { useState } from 'react' | ||
|
||
import { createTodoScope, TodoProvider } from './scope' | ||
import TodoList from './ToDoList' | ||
|
||
const user1Scope = createTodoScope() | ||
const user2Scope = createTodoScope() | ||
const user3Scope = createTodoScope() | ||
|
||
const App = () => { | ||
const [activeUserId, setActiveUserId] = useState<'user1' | 'user2' | 'user3'>('user1') | ||
const todoScopes = { | ||
user1: user1Scope({}), | ||
user2: user2Scope({}), | ||
user3: user3Scope({}), | ||
} as const | ||
|
||
const handleUserChange = (userId: 'user1' | 'user2' | 'user3') => { | ||
setActiveUserId(userId) | ||
} | ||
|
||
const scope = todoScopes?.[activeUserId]?.__scopeTodoList | ||
|
||
return ( | ||
<div> | ||
<h1>Multi-User To-Do List</h1> | ||
<div> | ||
<button type="button" onClick={() => handleUserChange('user1')}> | ||
Switch to User 1 | ||
</button> | ||
<button type="button" onClick={() => handleUserChange('user2')}> | ||
Switch to User 2 | ||
</button> | ||
<button type="button" onClick={() => handleUserChange('user3')}> | ||
Switch to User 3 | ||
</button> | ||
</div> | ||
<TodoProvider scope={todoScopes.user1?.__scopeTodoList}> | ||
<TodoProvider scope={todoScopes.user2?.__scopeTodoList}> | ||
<TodoProvider scope={todoScopes.user3?.__scopeTodoList}> | ||
<TodoList scope={scope} /> | ||
</TodoProvider> | ||
</TodoProvider> | ||
</TodoProvider> | ||
</div> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import App from './App' | ||
|
||
/* eslint-disable max-classes-per-file */ | ||
|
||
const meta = { | ||
title: 'scope/to-do-list', | ||
|
||
parameters: {}, | ||
argTypes: {}, | ||
} | ||
|
||
export default meta | ||
|
||
export const Example = () => { | ||
return <App /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Scope } from '@lodado/react-namespace' | ||
import React, { useState } from 'react' | ||
|
||
import { useTodoNamespaceStore } from './scope' | ||
|
||
interface UserTodoListProps { | ||
scope: Scope<any> | ||
} | ||
|
||
const TodoList: React.FC<UserTodoListProps> = ({ scope }) => { | ||
const { user, todos, addTodo, toggleTodo, removeTodo } = useTodoNamespaceStore( | ||
(state) => ({ user: state.user, todos: state.todos }), | ||
scope, | ||
) | ||
const [newTodo, setNewTodo] = useState('') | ||
|
||
const handleAddTodo = () => { | ||
if (newTodo.trim() !== '') { | ||
addTodo(newTodo.trim()) | ||
setNewTodo('') | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>{user} To-Do List</h2> | ||
<input type="text" value={newTodo} onChange={(e) => setNewTodo(e.target.value)} placeholder="Add a new task..." /> | ||
<button type="button" onClick={handleAddTodo}> | ||
Add | ||
</button> | ||
<ul> | ||
{todos.map((todo) => ( | ||
<li key={todo.id} style={{ display: 'flex', alignItems: 'center' }}> | ||
<input type="checkbox" checked={todo.completed} onChange={() => toggleTodo(todo.id)} /> | ||
<span | ||
style={{ | ||
textDecoration: todo.completed ? 'line-through' : 'none', | ||
marginLeft: '8px', | ||
}} | ||
> | ||
{todo.title} | ||
</span> | ||
<button type="button" onClick={() => removeTodo(todo.id)} style={{ marginLeft: 'auto', color: 'red' }}> | ||
Remove | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export default TodoList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createNamespaceScope } from '@lodado/react-namespace' | ||
|
||
import TodoStore from './toDoStore' | ||
|
||
export const [createTodoContext, createTodoScope] = createNamespaceScope('TodoList') | ||
|
||
export const { Provider: TodoProvider, useNamespaceStores: useTodoNamespaceStore } = createTodoContext<TodoStore>( | ||
'TodoList', | ||
{ | ||
localStore: () => new TodoStore(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NamespaceStore } from '@lodado/namespace-core' | ||
|
||
export interface Todo { | ||
id: number | ||
title: string | ||
completed: boolean | ||
} | ||
|
||
export interface TodoState { | ||
todos: Todo[] | ||
user: string | ||
} | ||
|
||
let index = 0 | ||
|
||
export default class TodoStore extends NamespaceStore<TodoState> { | ||
constructor() { | ||
index += 1 | ||
super({ user: `user${index}}`, todos: [] }) | ||
} | ||
|
||
addTodo(title: string) { | ||
const newTodo: Todo = { | ||
id: Date.now(), | ||
title, | ||
completed: false, | ||
} | ||
this.state.todos = [...this.state.todos, newTodo] | ||
} | ||
|
||
toggleTodo(id: number) { | ||
this.state.todos = this.state.todos.map((todo) => (todo.id === id ? { ...todo, completed: !todo.completed } : todo)) | ||
} | ||
|
||
removeTodo(id: number) { | ||
this.state.todos = this.state.todos.filter((todo) => todo.id !== id) | ||
} | ||
} |