This file contains hidden or 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
| const useNormalizedApi = () => { | |
| let db = useDB(); | |
| return { | |
| ... | |
| fetchTodos: async () => { | |
| let todos = await api.fetchTodos(); | |
| let { result, entities } = normalize( | |
| todos, | |
| apiSchemas.fetchTodosResponseSchema |
This file contains hidden or 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
| const useNormalizedApi = () => { | |
| let db = useDB(); | |
| return { | |
| ... | |
| updateTodo: async (id, payload) => { | |
| let todo = await api.updateTodo(id, payload); | |
| let { result, entities } = normalize( | |
| todo, | |
| apiSchemas.updateTodoResponseSchema |
This file contains hidden or 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
| const useNormalizedApi = () => { | |
| let db = useDB(); | |
| return { | |
| ... | |
| addTodo: async (text) => { | |
| let todo = await api.addTodo(text); | |
| let { result, entities } = normalize( | |
| todo, | |
| apiSchemas.addTodoResponseSchema |
This file contains hidden or 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
| const useNormalizedApi = () => { | |
| let db = useDB(); | |
| return { | |
| ... | |
| deleteTodo: async (id) => { | |
| let todo = await api.deleteTodo(id); | |
| let { result, entities } = normalize( | |
| todo, | |
| apiSchemas.deleteTodoResponseSchema |
This file contains hidden or 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
| const useNormalizedApi = () => { | |
| let db = useDB(); | |
| return { | |
| ... | |
| updateTodo: async (id, payload, oldTodo) => { | |
| // Let's assume the update will be successful | |
| let normalizedTodoData = normalize(payload, TodoSchema); | |
| db.mergeEntities(normalizedTodoData.entities); | |
| try { |
This file contains hidden or 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
| const queries = { | |
| getTodoById: (id) => { | |
| return { | |
| schema: TodoSchema, | |
| value: id | |
| }; | |
| } | |
| }; | |
| const TodoDetailComponent = (props) => { |
This file contains hidden or 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
| let todosResponse = { | |
| "status": 200, | |
| "body": [{ | |
| "id": 32, | |
| "text": "Buy cheese", | |
| "completed": false, | |
| "author": { | |
| "id": 38, | |
| "name": "Leanne Graham", | |
| "email": "[email protected]" |
This file contains hidden or 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
| import { schema } from 'normalizr'; | |
| const UserSchema = new schema.Entity( | |
| "User", | |
| {}, | |
| { idAttribute: "id" } | |
| ); | |
| const TodoSchema = new schema.Entity( | |
| "Todo", |
This file contains hidden or 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
| import { normalize } from 'normalizr'; | |
| let normalizeData = normalize(todosResponse, responseSchema); | |
| console.log(normalizedData); | |
| // Outputs: | |
| // { | |
| // "entities": { | |
| // "User": { | |
| // "38": { |
This file contains hidden or 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
| import { denormalize } from 'normalizr'; | |
| let denormalizedData = denormalize( | |
| normalizedData.result, | |
| responseSchema, | |
| normalizedData.entities | |
| ); | |
| console.log(denormalizedData); | |
| // Outputs |