Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Oct 6, 2024
1 parent a6a98bc commit eb6290c
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions docs/documentation/capabilities/state-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ Take for example this `todos` state store:
type UUID = `${string}-${string}-${string}-${string}-${string}`

export interface Todo {
id: UUID
name: string
description: string
status: "done" | "pending" | "removed"
dateCreated: Date
dateLastUpdated: Date
id: UUID
name: string
description: string
status: 'done' | 'pending' | 'removed'
dateCreated: Date
dateLastUpdated: Date
}

const [todos, updateTodos] = state<Todo[]>([]);
const [todos, updateTodos] = state<Todo[]>([])

export const todoList = todos;
export const todoList = todos
```

As you can see, we can create a file dedicated to manage a particular state we want to use in multiple places in our application.
Expand All @@ -45,10 +45,10 @@ We don't have to worry about subscription when it comes to rendering this data,
import {todoList} from "./stores/todos"

const App = () => {

const renderTodo = () => {...}
// html will handle all subscribing and

// html will handle all subscribing and
// unsubscribing from todoList state
return html`
<ul id="todos">
Expand All @@ -61,23 +61,23 @@ const App = () => {
Easy enough, to perform something whenever this list changes, we can just use the [effect](../state/effect.md).

```javascript
import {todoList} from "./stores/todos"
import { todoList } from './stores/todos'

const defaultTodosByStatus = {done: [], pending: [], removed: []};
const defaultTodosByStatus = { done: [], pending: [], removed: [] }

const [todosByStatus, updateTodosByStatus] = state(defaultTodosByStatus)

// effect will handle subscribing to the todoList state
const unsubFromEffect = effect(() => {
updateTodosByStatus(
todoList().reduce((acc, todo)=> {
if(!acc[todo.status]) {
todoList().reduce((acc, todo) => {
if (!acc[todo.status]) {
acc[todo.status] = []
}

acc[todo.status].push(todo)
return acc;

return acc
}, defaultTodosByStatus)
)
})
Expand All @@ -102,9 +102,9 @@ export const createTodo = (name: string) => {
description: "",
status: "pending",
dateCreated,
dateLastUpdated: dateCreated
dateLastUpdated: dateCreated
}

updateTodos(prev => [...prev, todo])
}

Expand All @@ -119,7 +119,7 @@ export const updateTodo = (id: UUID, data: Partial<Todo>) => {
dateLastUpdated: new Date()
}
}

return todo;
}))
}
Expand All @@ -136,12 +136,13 @@ export const clearTodos = () => {
```

These actions can be whatever you want. They can
- store data in `localstorage` or `indexedDB`;
- be asynchronous;
- call servers APIs to save data;
- perform validations;
- map the data;
- etc

- store data in `localstorage` or `indexedDB`;
- be asynchronous;
- call servers APIs to save data;
- perform validations;
- map the data;
- etc

Data storage and state management does not have to be complex and all you need from here is use the APIs readily available in the environment to do whatever you want.

Expand Down

0 comments on commit eb6290c

Please sign in to comment.