diff --git a/client/coral-admin/src/containers/Streams/Streams.js b/client/coral-admin/src/containers/Streams/Streams.js index 53db75c89..5d8508f33 100644 --- a/client/coral-admin/src/containers/Streams/Streams.js +++ b/client/coral-admin/src/containers/Streams/Streams.js @@ -12,21 +12,24 @@ import { TableHeader } from 'react-mdl'; +const limit = 25; + class Streams extends Component { state = { - searchTerm: '', - sortBy: 'newest', + search: '', + sort: 'desc', statusFilter: 'all', statusMenus: {} } componentDidMount () { - this.props.fetchAssets(0, 25, '', 'desc'); + this.props.fetchAssets(0, limit, '', this.state.sortBy); } onSettingChange = (setting) => (e) => { this.setState({[setting]: e.target.value}); + this.props.fetchAssets(0, limit, this.state.search, this.state.sort); } renderDate = (date) => { @@ -104,8 +107,8 @@ class Streams extends Component { value={sortBy} childContainer='div' onChange={this.onSettingChange('sortBy')}> - {lang.t('streams.newest')} - {lang.t('streams.oldest')} + {lang.t('streams.newest')} + {lang.t('streams.oldest')} diff --git a/routes/api/assets/index.js b/routes/api/assets/index.js index a602d7fd0..a661d64e8 100644 --- a/routes/api/assets/index.js +++ b/routes/api/assets/index.js @@ -12,22 +12,40 @@ router.get('/', (req, res, next) => { skip = 0, sort = 'asc', field = 'created_at', + filter = 'all', search = '' } = req.query; + const assets = (filter, search) => { + switch(filter) { + case 'open': + return Asset.search(search) + .find({ + $or: [ + {closedAt: null}, + {closedAt: {$gt: Date.now()}} + ] + }); + case 'closed': + return Asset.search(search) + .find({ + closedAt: {$lt: Date.now()} + }); + default: + return Asset.search(search); + } + }; + // Find all the assets. Promise.all([ - Asset - .search(search) + assets(filter, search) .sort({[field]: (sort === 'asc') ? 1 : -1}) .skip(parseInt(skip)) .limit(parseInt(limit)), - Asset - .search(search) + assets(filter, search) .count() ]) .then(([result, count]) => { - // Send back the asset data. res.json({ result, diff --git a/tests/routes/api/assets/index.js b/tests/routes/api/assets/index.js index 8e94c81fc..da011bf7b 100644 --- a/tests/routes/api/assets/index.js +++ b/tests/routes/api/assets/index.js @@ -18,12 +18,14 @@ describe('/api/v1/assets', () => { url: 'https://coralproject.net/news/asset1', title: 'Asset 1', description: 'term1', - id: '1' + id: '1', + closedAt: Date.now() }, { url: 'https://coralproject.net/news/asset2', title: 'Asset 2', - description: 'term2' + description: 'term2', + closedAt: null } ]); }); @@ -81,6 +83,38 @@ describe('/api/v1/assets', () => { }); }); + it('should return only closed assets', () => { + return chai.request(app) + .get('/api/v1/assets?filter=closed') + .set(passport.inject({roles: ['admin']})) + .then((res) => { + const body = res.body; + + expect(body).to.have.property('count', 1); + expect(body).to.have.property('result'); + + const assets = body.result; + + expect(assets[0]).to.have.property('title', 'Asset 1'); + }); + }); + + it('should return only opened assets', () => { + return chai.request(app) + .get('/api/v1/assets?filter=open') + .set(passport.inject({roles: ['admin']})) + .then((res) => { + const body = res.body; + + expect(body).to.have.property('count', 1); + expect(body).to.have.property('result'); + + const assets = body.result; + + expect(assets[0]).to.have.property('title', 'Asset 2'); + }); + }); + }); describe('#put', () => {