Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
allow for basic $list usage with strings (fixes #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
glortho committed Jun 2, 2017
1 parent 6b07abd commit becca27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/api/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ const methodizeResource = ( fetch, props ) => ( memo, key ) => {
const $reset = id => () => api.fetchOne( id );

const addRestMethods = model => {
const id = getIdFromModel( model );
model.$delete = $delete( id );
model.$update = $update( id );
model.$clear = $clear( id );
model.$reset = $reset( id );
if ( typeof model === 'object' ) {
const id = getIdFromModel( model );
model.$delete = $delete( id );
model.$update = $update( id );
model.$clear = $clear( id );
model.$reset = $reset( id );
}
return model;
};

Expand Down
31 changes: 21 additions & 10 deletions src/api/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ export default function initApiStore( url, schema, store = stateTree ) {
getRequestsData: path =>
methods.getRequests([ path, 'data' ]),

getRequestsFormat: path =>
methods.getRequests([ path, 'format' ]),

setRequests: ( data, path ) =>
methods.setState( data, methods.requestsPath( path ) ),

setRequestsData: ( path, data ) =>
methods.setRequests( data, [ path, 'data' ] ),

setRequestsFromat: ( path, data ) =>
methods.setRequests( data, [ path, 'format' ] ),

getPending: path =>
methods.getRequests([ path, 'pending' ]),

Expand Down Expand Up @@ -122,12 +128,12 @@ export default function initApiStore( url, schema, store = stateTree ) {
getCollection: ( path = `/${resourceType}` ) => {
const collection = methods.getRequestsData( path );
if ( collection ) {
const models = methods.getModels();
return collection.reduce(( memo, id ) => {
const model = models.get( id );
if ( model ) return memo.push( model );
return memo;
}, List());
if ( methods.getRequestsFormat( path ) === 'string' ) {
return collection;
} else {
const models = methods.getModels();
return collection.map( id => models.get( id ) || Map() );
}
} else {
return null;
}
Expand All @@ -136,10 +142,15 @@ export default function initApiStore( url, schema, store = stateTree ) {
setCollection: ( data = List(), path = `/${resourceType}` ) => {
methods.setState(
( methods.getState() || Map() ).withMutations( map => {
const dict = data.reduce(( memo, item ) => ({ ...memo, [getIdFromModel( item )]: item }), {});
const collection = List( Object.keys( dict ) );
map.update( 'models', ( models = Map() ) => models.mergeDeep( dict ) );
map.setIn( methods.requestsPath([ path, 'data' ]), collection );
if ( data.length && typeof data[0] !== 'object' ) {
map.setIn( methods.requestsPath([ path, 'data' ]), data );
map.setIn( methods.requestsPath([ path, 'format' ]), 'string' );
} else {
const dict = data.reduce(( memo, item ) => ({ ...memo, [getIdFromModel( item )]: item }), {});
const collection = List( Object.keys( dict ) );
map.update( 'models', ( models = Map() ) => models.mergeDeep( dict ) );
map.setIn( methods.requestsPath([ path, 'data' ]), collection );
}
})
);
return methods.getCollection( path );
Expand Down

0 comments on commit becca27

Please sign in to comment.