From eec07ade6a396617b98e3397bb7e9266f071d80b Mon Sep 17 00:00:00 2001 From: Andrei Dumitrescu <5057797+andreidcm@users.noreply.github.com> Date: Sat, 28 Dec 2019 01:20:41 +0100 Subject: [PATCH] docs: Add WebSocket and automerge reference --- README.md | 107 +++++++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 2343a75..73a5758 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ # redux-list -> Treat Redux state slices as lists with a standard structure and behaviour +> Redux state slices as lists with a standard structure and behaviour --- @@ -28,7 +28,9 @@ * [x] **Aggregate**: Combine data coming from different sources (users from own api, tweet count from Twitter) * [x] **Race free**: List operations are sequential. If `update` is issued after `delete`, the `update` promise will wait for `delete` to finish -* [x] **It's Redux**: Treat your Redux state data as simple lists with common metadata helpers (isLoading, isUpdating etc.). +* [x] **It's Redux**: Treat Redux state data as simple lists with common metadata helpers (isLoading, isUpdating etc.) +* [ ] **Real time**: WebSockets support +* [ ] **Offline & [CRDT](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)**: Keep local changes while offline and sync with [automerge](https://github.com/automerge/automerge) ## Install @@ -44,18 +46,28 @@ npm install @mutantlove/redux-list import { buildList } from "@mutantlove/redux-list" const TodosList = buildList("PAGE__SECTION--TODOS", { - create: data => POST("/todos", data), - read: () => [{id: 1, title: "lorem ipsum"}], - readOne: () => { - id: 1, - title: "lorem ipsum", - body: "extended data that you dont need the first time round" - }, - update: (id, data) => PATCH(`/todos/${id}`, date), - delete: id => DELETE(`/todos/${id}`), + create: data => + POST("/todos", data), + + read: () => + GET("/todos"), + + readOne: id => + GET("/comments", { + query: { todoId: id }, + }).then(result => ({ + id, + comments: result, + })), + + update: (id, data) => + PATCH(`/todos/${id}`, date), + + delete: id => + DELETE(`/todos/${id}`), }) -export {TodosList} +export { TodosList } ``` `src/store.js` - Hook internal list reducers into the state store. @@ -71,22 +83,15 @@ const store = createStore( ) ``` -`src/todos.container.jsx` - Use the list's selector helpers to access the data. +`src/use-app-list.js` - Hook to simplify usage in Container components ```js -import React from "react" -import cx from "classnames" import { useDispatch, useSelector } from "react-redux" -import { useList } from "@mutantlove/redux-list" - -import { TodosList } from "./todos.state" +import { useList as useMutantList } from "@mutantlove/redux-list" -/** - * Helper hook to reduce the number of imports - */ -const useLocalList = list => { +const useList = list => { const dispatch = useDispatch() - const { selector, ...actions } = useList(list, dispatch) + const { selector, ...actions } = useMutantList(list, dispatch) return { selector: useSelector(selector), @@ -94,10 +99,22 @@ const useLocalList = list => { } } +export { useList } +``` + +`src/todos.container.jsx` - Use list's selector to access the data + +```js +import React from "react" +import cx from "classnames" + +import { useList } from "./use-list" +import { TodosList } from "./todos.state" + const TodosContainer = () => { const { selector: { items, isLoading }, - } = useLocalList(TodosList) + } = useList(TodosList) return (