Skip to content

Commit

Permalink
✨ type option / uniq reducer (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien JUIF committed Jul 17, 2017
1 parent b4d806d commit 88d92ed
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 143 deletions.
54 changes: 33 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import factory from 'trampss-redux-factory'

This factory takes four parameters, you can use one of these signatures :
- `factory(middlewares)(fieldKey)(path)(name)`
- `factory(middlewares)(fieldKey)(path)({ name, prefix })`
- `factory(middlewares)(fieldKey)(path)({ name, prefix, type })`

Parameters are :
- **middlewares** (optional), contain an object with `pre` and `post` fields. Both are an array of middlewares to apply before and after the `core` middleware.
Expand All @@ -67,7 +67,9 @@ Parameters are :
- **name** (mandatory), the reducer name (for instance: `todos`)
- it's used to generate actions types
- it's used to retrieve informations from selectors
- it can be an object : `{name, prefix}` where `prefix` is added to actions to avoid some collisions when there are two reducers with same name in two distincts pathes.
- it can be an object : `{name, prefix, type}` where :
- `prefix` is added to actions to avoid some collisions when there are two reducers with the same name in two distincts paths
- `type` can be `map` or `uniq` (default is `map`)

Example:
- this reducer will use `id` as key field
Expand All @@ -79,7 +81,32 @@ import factory from 'trampss-redux-factory'
export default factory()('id')('api.raw')('todos')
```

Data will be stored into `state.api.raw.todos`
Data will be stored into `state.api.raw.todos`.

### Types
Types are :
- `map` : your state is a hashmap, useful to bind your API to Redux with the following redux state model :
```es6
{
data: { <key1>: <instance1>, <key2>: <instance2> },
array: [<instance1>, <instance2>],
keys: [<key1>, <key2>],
initialized: true,

}
```

- `uniq` : your state is an object, simpler, with the following redux state model :
```es6
{
data: <instance>,
initialized: true,
}
```

Default type is `map`.

To see more informations about types, [go to the specific page](./TYPES.md).

### reducer
The previous factory returns a function which is a reducer.
Expand Down Expand Up @@ -116,16 +143,9 @@ export default store
The factory returns a function (this is the reducer) that also contains actions and selectors as fields.
Some generic actions are available. By now, it's not possible to add custom ones.

| function name | description | signature | generated action |
|---|---|---|---|
| `set` | set an array of instances of your resource | `set(<array>)` | `{ type: '@trampss/SET_TODOS', payload: <array> }` |
| `add` | add an instance of your resource | `add(<instance>)` | `{ type: '@trampss/ADD_TODOS', payload: <instance> }` |
| `update` | update an existing instance of your resource | `update(<instance>)` | `{ type: '@trampss/UPDATE_TODOS', payload: <instance> }` |
| `remove` | remove one instance of your resource by its key | `remove(<key>)` | `{ type: '@trampss/REMOVE_TODOS', payload: <key> }` |
| `reset` | reset the reducer (wipe all data) | `reset()` | `{ type: '@trampss/RESET_TODOS' }` |
To see them go to [TYPES.md](./TYPES.md).


Example, we set todos to our reducer:
Example, we set todos to our typed `map` reducer:
```es6
// import your reducer
// (created by tramps-redux-data-store factory)
Expand Down Expand Up @@ -155,15 +175,7 @@ dispatch(
The factory returns a function (this is the reducer) that also contains actions and selectors as fields.
Some generic selectors are available. By now, it's not possible to add custom ones.

| signature | description | comment |
|---|---|---|
| `get(<id>)(state)` | returns all data, or specific(s) one(s) (by key(s)) | <ul><li>if `<id>` is `undefined`, it returns all data</li><li>if `<id>` is an array, it returns all instances that match one of ids</li><li>in other cases, it returns the instance with its `id` that that match the parameter</li></ul> |
| `getBy(<propertyPath>, <value>)(state)` | get data specified by the field you want to filter with (take care, selectors are not memoized) | Example: `getBy('visible', true)(state)` returns all visible todos.
| `getKeys(state)` | returns all store keys (in array) | |
| `getAsArray(state)` | returns all data in array (raw) | |
| `getLength(state)` | returns number of stored instances | |
| `isInitialized(state)` | return true if the store has been initialized (by `add` or by `set` action) | |
| `getState(state)` | returns the global state of your reducer | The global state contains :<ul><li>`data`: key/value store</li><li>`array`: raw data</li><li>`keys`: keys array</li><li>`initialized`: boolean (set to true by `set` and `add` actions)</li></ul>
To see them go to [TYPES.md](./TYPES.md).

Example, we retrieve the todo with id `1`:
```es6
Expand Down
62 changes: 62 additions & 0 deletions TYPES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Types

- [map](#map)
- [uniq](#uniq)

## map
### state
```es6
{
data: { <key1>: <instance1>, <key2>: <instance2> },
array: [<instance1>, <instance2>],
keys: [<key1>, <key2>],
initialized: true,

}
```

### actions

| function name | description | signature | generated action |
|---|---|---|---|
| `set` | set an array of instances of your resource | `set(<array>)` | `{ type: '@trampss/SET_TODOS', payload: <array> }` |
| `add` | add an instance of your resource | `add(<instance>)` | `{ type: '@trampss/ADD_TODOS', payload: <instance> }` |
| `update` | update an existing instance of your resource | `update(<instance>)` | `{ type: '@trampss/UPDATE_TODOS', payload: <instance> }` |
| `remove` | remove one instance of your resource by its key | `remove(<key>)` | `{ type: '@trampss/REMOVE_TODOS', payload: <key> }` |
| `reset` | reset the reducer (wipe all data) | `reset()` | `{ type: '@trampss/RESET_TODOS' }` |

### selectors

| signature | description | comment |
|---|---|---|
| `get(<id>)(state)` | returns all data, or specific one(s) (by key(s)) | <ul><li>if `<id>` is `undefined`, it returns all data</li><li>if `<id>` is an array, it returns all instances that match one of the ids</li><li>in other cases, it returns the instance which `id` matches the parameter</li></ul> |
| `getBy(<propertyPath>, <value>)(state)` | get data specified by the field you want to filter with (take care, selectors are not memoized) | Example: `getBy('visible', true)(state)` returns all visible todos.
| `getKeys(state)` | returns all store keys (in array) | |
| `getAsArray(state)` | returns all data in array (raw) | |
| `getLength(state)` | returns number of stored instances | |
| `isInitialized(state)` | returns true if the store has been initialized (by `add` or by `set` action) | |
| `getState(state)` | returns the global state of your reducer | |

## uniq
### state
```es6
{
data: <instance>,
initialized: true,
}
```

### actions

| function name | description | signature | generated action |
|---|---|---|---|
| `set` | set the instance | `set(<instance>)` | `{ type: '@trampss/SET_TODOS', payload: <instance> }` |
| `reset` | reset the reducer (wipe all data) | `reset()` | `{ type: '@trampss/RESET_TODOS' }` |

### selectors

| signature | description | comment |
|---|---|---|
| `get()(state)` | returns data (instance) | |
| `isInitialized(state)` | returns true if the store has been initialized (by `set` action) | |
| `getState(state)` | returns the global state of your reducer | |
2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 88d92ed

Please sign in to comment.