-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
cdf5b6d
commit ea5a63d
Showing
29 changed files
with
50,070 additions
and
60,202 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 was deleted.
Oops, something went wrong.
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,17 @@ | ||
.spacer { | ||
flex: 1; | ||
} | ||
|
||
.toolbar { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
height: 60px; | ||
display: flex; | ||
align-items: center; | ||
background-color: var(--primary-color); | ||
color: white; | ||
font-weight: 600; | ||
font-size: 1.25rem; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
import { container } from 'tsyringe' | ||
import { LocalTodoRepository } from '../../features/todo/infrastructure/local-todo-repository' | ||
import { InjectionTokens } from './injection-tokens' | ||
|
||
container.registerSingleton(InjectionTokens.TODO_REPOSITORY, LocalTodoRepository) | ||
|
||
export { container } |
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,3 @@ | ||
export class InjectionTokens { | ||
static TODO_REPOSITORY = Symbol.for('TODO_REPOSITORY') | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/with-react/src/features/todo/application/complete-todo-cmd.ts
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 { Command, UseCaseKey } from '@archimedes/arch' | ||
import { inject, injectable } from 'tsyringe' | ||
import { InjectionTokens } from '../../../core/di/injection-tokens' | ||
import type { TodoRepository } from '../domain/todo-repository' | ||
|
||
@UseCaseKey('CompleteTodoCmd') | ||
@injectable() | ||
export class CompleteTodoCmd extends Command<{ id: number; isCompleted: boolean }> { | ||
constructor(@inject(InjectionTokens.TODO_REPOSITORY) private readonly todoRepository: TodoRepository) { | ||
super() | ||
} | ||
|
||
internalExecute({ id, isCompleted }: { id: number; isCompleted: boolean }): Promise<void> { | ||
return this.todoRepository.update(id, { completed: isCompleted }) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/with-react/src/features/todo/application/create-todo-cmd.ts
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,17 @@ | ||
import { Command, UseCaseKey } from '@archimedes/arch' | ||
import { inject, injectable } from 'tsyringe' | ||
import { InjectionTokens } from '../../../core/di/injection-tokens' | ||
import { NewTodo } from '../domain/new-todo' | ||
import type { TodoRepository } from '../domain/todo-repository' | ||
|
||
@UseCaseKey('CreateTodoCmd') | ||
@injectable() | ||
export class CreateTodoCmd extends Command<NewTodo> { | ||
constructor(@inject(InjectionTokens.TODO_REPOSITORY) private readonly todoRepository: TodoRepository) { | ||
super() | ||
} | ||
|
||
internalExecute(newTodo: NewTodo): Promise<void> { | ||
return this.todoRepository.create(newTodo) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/with-react/src/features/todo/application/get-todo-qry.ts
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,17 @@ | ||
import { Query, UseCaseKey } from '@archimedes/arch' | ||
import { inject, injectable } from 'tsyringe' | ||
import { InjectionTokens } from '../../../core/di/injection-tokens' | ||
import { Todo } from '../domain/todo' | ||
import type { TodoRepository } from '../domain/todo-repository' | ||
|
||
@UseCaseKey('GetTodoQry') | ||
@injectable() | ||
export class GetTodoQry extends Query<Todo | undefined, number> { | ||
constructor(@inject(InjectionTokens.TODO_REPOSITORY) private readonly todoRepository: TodoRepository) { | ||
super() | ||
} | ||
|
||
internalExecute(id: number): Promise<Todo | undefined> { | ||
return this.todoRepository.get(id) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/with-react/src/features/todo/application/get-todos-qry.ts
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,18 @@ | ||
import { InvalidateCache, Query, UseCaseKey } from '@archimedes/arch' | ||
import { inject, injectable } from 'tsyringe' | ||
import { InjectionTokens } from '../../../core/di/injection-tokens' | ||
import { Todo } from '../domain/todo' | ||
import type { TodoRepository } from '../domain/todo-repository' | ||
|
||
@UseCaseKey('GetTodosQry') | ||
@InvalidateCache | ||
@injectable() | ||
export class GetTodosQry extends Query<Todo[]> { | ||
constructor(@inject(InjectionTokens.TODO_REPOSITORY) private readonly todoRepository: TodoRepository) { | ||
super() | ||
} | ||
|
||
internalExecute(): Promise<Todo[]> { | ||
return this.todoRepository.getAll() | ||
} | ||
} |
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,3 @@ | ||
import { Todo } from './todo' | ||
|
||
export type NewTodo = Omit<Todo, 'id'> |
9 changes: 9 additions & 0 deletions
9
examples/with-react/src/features/todo/domain/todo-repository.ts
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,9 @@ | ||
import { NewTodo } from './new-todo' | ||
import { Todo } from './todo' | ||
|
||
export interface TodoRepository { | ||
getAll(): Promise<Todo[]> | ||
get(id: number): Promise<Todo | undefined> | ||
create(newTodo: NewTodo): Promise<void> | ||
update(id: number, todo: Partial<Todo>): Promise<void> | ||
} |
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,5 @@ | ||
export interface Todo { | ||
id: number | ||
title: string | ||
completed: boolean | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/with-react/src/features/todo/infrastructure/http-todo-repository.ts
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,29 @@ | ||
import { NewTodo } from '../domain/new-todo' | ||
import { Todo } from '../domain/todo' | ||
import { TodoRepository } from '../domain/todo-repository' | ||
import { injectable } from 'tsyringe' | ||
|
||
@injectable() | ||
export class HttpTodoRepository implements TodoRepository { | ||
private static TODO_URL = 'https://jsonplaceholder.typicode.com/todos' | ||
private static TODO_BY_ID_URL = (id: number) => `${HttpTodoRepository.TODO_URL}/${id}` | ||
|
||
constructor() {} | ||
async get(id: number): Promise<Todo | undefined> { | ||
const response = await fetch(HttpTodoRepository.TODO_BY_ID_URL(id)) | ||
return response.json() | ||
} | ||
|
||
async getAll(): Promise<Todo[]> { | ||
const response = await fetch(HttpTodoRepository.TODO_URL) | ||
return response.json() | ||
} | ||
|
||
async create(newTodo: NewTodo): Promise<void> { | ||
await fetch(HttpTodoRepository.TODO_URL, { method: 'POST', body: JSON.stringify(newTodo) }) | ||
} | ||
|
||
async update(id: number, todo: Partial<Todo>): Promise<void> { | ||
await fetch(HttpTodoRepository.TODO_BY_ID_URL(id), { method: 'PATCH', body: JSON.stringify(todo) }) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
examples/with-react/src/features/todo/infrastructure/local-todo-repository.ts
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,27 @@ | ||
import { injectable } from 'tsyringe' | ||
import { NewTodo } from '../domain/new-todo' | ||
import { Todo } from '../domain/todo' | ||
import { TodoRepository } from '../domain/todo-repository' | ||
|
||
@injectable() | ||
export class LocalTodoRepository implements TodoRepository { | ||
private readonly todos = new Map<number, Todo>() | ||
|
||
async getAll(): Promise<Todo[]> { | ||
return Array.from(this.todos.values()) | ||
} | ||
|
||
async get(id: number): Promise<Todo | undefined> { | ||
return this.todos.get(id) | ||
} | ||
|
||
async create(newTodo: NewTodo): Promise<void> { | ||
const newId = this.todos.size + 1 | ||
this.todos.set(newId, { ...newTodo, id: newId }) | ||
} | ||
|
||
async update(id: number, todo: Partial<Todo>): Promise<void> { | ||
const previousTodo = this.todos.get(id) | ||
this.todos.set(id, { ...previousTodo, ...todo } as Todo) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/with-react/src/features/todo/ui/create-todo/create-todo.module.css
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 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
gap: 1rem; | ||
margin-top: 72px; | ||
} | ||
|
||
.actions { | ||
display: flex; | ||
gap: 1rem; | ||
} |
35 changes: 35 additions & 0 deletions
35
examples/with-react/src/features/todo/ui/create-todo/create-todo.tsx
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,35 @@ | ||
import { FC, useState } from 'react' | ||
import { Link, useNavigate } from 'react-router-dom' | ||
import { useDi } from '../../../../use-di' | ||
import { CreateTodoCmd } from '../../application/create-todo-cmd' | ||
import styles from './create-todo.module.css' | ||
|
||
export const CreateTodo: FC = () => { | ||
const [title, setTitle] = useState('') | ||
const createTodoCmd = useDi(CreateTodoCmd) | ||
const navigate = useNavigate() | ||
|
||
const saveTodo = async () => { | ||
await createTodoCmd.execute({ | ||
title, | ||
completed: false | ||
}) | ||
navigate('/') | ||
} | ||
|
||
return ( | ||
<div className={styles['container']}> | ||
<h2>CREATE TODO</h2> | ||
|
||
<label> | ||
Title | ||
<input type="text" value={title} onChange={e => setTitle(e.target.value)} /> | ||
</label> | ||
|
||
<div className={styles['actions']}> | ||
<Link to="/">Back</Link> | ||
<button onClick={saveTodo}>Save</button> | ||
</div> | ||
</div> | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/with-react/src/features/todo/ui/todo-list/todo-list.module.css
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,34 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
gap: 1rem; | ||
margin-top: 72px; | ||
} | ||
|
||
li { | ||
margin-bottom: 1rem; | ||
} | ||
|
||
.todo { | ||
border-radius: 4px; | ||
border: 1px solid #eee; | ||
background-color: #fefefe; | ||
padding: 1rem; | ||
display: flex; | ||
cursor: pointer; | ||
font-size: 1rem; | ||
font-weight: normal; | ||
color: #111; | ||
} | ||
|
||
.completed { | ||
text-decoration: line-through; | ||
background-color: var(--primary-color); | ||
color: white; | ||
} | ||
|
||
.actions { | ||
display: flex; | ||
gap: 1rem; | ||
} |
Oops, something went wrong.