Skip to content

Commit 17b2efd

Browse files
authored
Merge pull request #138 from rovercode/beta
Push to Production
2 parents c3b8349 + a621213 commit 17b2efd

12 files changed

Lines changed: 384 additions & 86 deletions

File tree

src/actions/__test__/program.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ describe('Program actions', () => {
5252
mock.restore();
5353
});
5454

55+
test('fetch featured programs', async () => {
56+
const mock = new MockAdapter(axios);
57+
const programs = [{
58+
id: 33,
59+
name: 'Unnamed_Design_3',
60+
content: '<xml><variables></variables></xml>',
61+
admin_tags: ['featured'],
62+
}];
63+
64+
mock.onGet('/api/v1/block-diagrams/', {
65+
params: {
66+
admin_tags: ['featured'],
67+
},
68+
}).reply(200, programs);
69+
70+
const action = fetchPrograms({
71+
params: {
72+
admin_tags: ['featured'],
73+
},
74+
});
75+
const { type } = action;
76+
const payload = await action.payload;
77+
78+
expect(type).toEqual('FETCH_FEATURED_PROGRAMS');
79+
expect(payload).toEqual(programs);
80+
mock.restore();
81+
});
82+
5583
test('remove program', async () => {
5684
const mock = new MockAdapter(axios);
5785

src/actions/program.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export const FETCH_USER_PROGRAMS = 'FETCH_USER_PROGRAMS';
1111
export const FETCH_USER_PROGRAMS_PENDING = `${FETCH_USER_PROGRAMS}_PENDING`;
1212
export const FETCH_USER_PROGRAMS_FULFILLED = `${FETCH_USER_PROGRAMS}_FULFILLED`;
1313
export const FETCH_USER_PROGRAMS_REJECTED = `${FETCH_USER_PROGRAMS}_REJECTED`;
14+
export const FETCH_FEATURED_PROGRAMS = 'FETCH_FEATURED_PROGRAMS';
15+
export const FETCH_FEATURED_PROGRAMS_PENDING = `${FETCH_FEATURED_PROGRAMS}_PENDING`;
16+
export const FETCH_FEATURED_PROGRAMS_FULFILLED = `${FETCH_FEATURED_PROGRAMS}_FULFILLED`;
17+
export const FETCH_FEATURED_PROGRAMS_REJECTED = `${FETCH_FEATURED_PROGRAMS}_REJECTED`;
1418
export const REMOVE_PROGRAM = 'REMOVE_PROGRAM';
1519
export const REMOVE_PROGRAM_PENDING = `${REMOVE_PROGRAM}_PENDING`;
1620
export const REMOVE_PROGRAM_FULFILLED = `${REMOVE_PROGRAM}_FULFILLED`;
@@ -22,6 +26,8 @@ export const fetchPrograms = (xhrOptions) => {
2226

2327
if (xhrOptions && xhrOptions.params && xhrOptions.params.user) {
2428
type = FETCH_USER_PROGRAMS;
29+
} else if (xhrOptions && xhrOptions.params && xhrOptions.params.admin_tags) {
30+
type = FETCH_FEATURED_PROGRAMS;
2531
}
2632

2733
return ({

src/components/ProgramCollection.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ProgramCollection extends Component {
2727
}
2828

2929
update = () => {
30-
const { onUpdate, owned } = this.props;
30+
const { onUpdate } = this.props;
3131
const {
3232
page,
3333
ordering,
@@ -45,7 +45,7 @@ class ProgramCollection extends Component {
4545
params.search = searchQuery;
4646
}
4747

48-
onUpdate(params, owned);
48+
onUpdate(params);
4949
}
5050

5151
toggleOrdering = (name) => {
@@ -81,11 +81,11 @@ class ProgramCollection extends Component {
8181

8282
render() {
8383
const {
84+
user,
8485
label,
8586
onProgramClick,
8687
onRemoveClick,
8788
programs,
88-
owned,
8989
intl,
9090
tag,
9191
} = this.props;
@@ -182,7 +182,7 @@ class ProgramCollection extends Component {
182182
</Card.Header>
183183
<Card.Meta>
184184
{
185-
owned ? (
185+
user.username === program.user.username ? (
186186
<FormattedMessage
187187
id="app.program_collection.mine"
188188
description="Label to indicate program owned by user"
@@ -193,9 +193,14 @@ class ProgramCollection extends Component {
193193
</Card.Meta>
194194
</Card.Content>
195195
<Card.Content extra>
196-
<Button primary id={program.id} data-owned={owned} onClick={onProgramClick}>
196+
<Button
197+
primary
198+
id={program.id}
199+
data-owned={user.username === program.user.username}
200+
onClick={onProgramClick}
201+
>
197202
{
198-
owned ? (
203+
user.username === program.user.username ? (
199204
<FormattedMessage
200205
id="app.program_collection.work"
201206
description="Button label to keep working on program"
@@ -211,7 +216,7 @@ class ProgramCollection extends Component {
211216
}
212217
</Button>
213218
{
214-
owned ? (
219+
user.username === program.user.username ? (
215220
<Button
216221
negative
217222
id={program.id}
@@ -251,13 +256,15 @@ class ProgramCollection extends Component {
251256
}
252257

253258
ProgramCollection.defaultProps = {
254-
owned: false,
255259
tag: {
256260
tags: [],
257261
},
258262
};
259263

260264
ProgramCollection.propTypes = {
265+
user: PropTypes.shape({
266+
username: PropTypes.string.isRequired,
267+
}).isRequired,
261268
programs: PropTypes.shape({
262269
next: PropTypes.string,
263270
previous: PropTypes.string,
@@ -278,7 +285,6 @@ ProgramCollection.propTypes = {
278285
})),
279286
}),
280287
label: PropTypes.string.isRequired,
281-
owned: PropTypes.bool,
282288
onProgramClick: PropTypes.func.isRequired,
283289
onRemoveClick: PropTypes.func.isRequired,
284290
onUpdate: PropTypes.func.isRequired,

src/components/ProgramList.js

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ProgramList extends Component {
4242
user: user.user_id,
4343
}).then(() => fetchPrograms({
4444
user__not: user.user_id,
45+
})).then(() => fetchPrograms({
46+
admin_tags: 'featured',
4547
})).then(() => fetchTags());
4648
}
4749

@@ -67,6 +69,9 @@ class ProgramList extends Component {
6769
}))
6870
.then(() => fetchPrograms({
6971
user__not: user.user_id,
72+
}))
73+
.then(() => fetchPrograms({
74+
admin_tags: 'featured',
7075
}));
7176
}
7277

@@ -87,32 +92,40 @@ class ProgramList extends Component {
8792
});
8893
}
8994

90-
fetch = (params, owned) => {
95+
fetchUserPrograms = (params) => {
9196
const { fetchPrograms, user } = this.props;
97+
fetchPrograms({
98+
user: user.user_id,
99+
...params,
100+
});
101+
}
92102

93-
if (owned) {
94-
fetchPrograms({
95-
user: user.user_id,
96-
...params,
97-
});
98-
} else {
99-
fetchPrograms({
100-
user__not: user.user_id,
101-
...params,
102-
});
103-
}
103+
fetchFeaturedPrograms = (params) => {
104+
const { fetchPrograms } = this.props;
105+
fetchPrograms({
106+
admin_tags: 'featured',
107+
...params,
108+
});
109+
}
110+
111+
fetchOtherPrograms = (params) => {
112+
const { fetchPrograms, user } = this.props;
113+
fetchPrograms({
114+
user__not: user.user_id,
115+
...params,
116+
});
104117
}
105118

106-
programSegment = (programs, tag, label, owned) => (
119+
programSegment = (programs, user, tag, label, onUpdate) => (
107120
<Segment raised style={{ margin: '10px 10% 10px 10%' }}>
108121
<ProgramCollection
109122
programs={programs}
123+
user={user}
110124
tag={tag}
111125
label={label}
112-
owned={owned}
113126
onProgramClick={this.loadProgram}
114127
onRemoveClick={this.showConfirm}
115-
onUpdate={this.fetch}
128+
onUpdate={onUpdate}
116129
/>
117130
</Segment>
118131
)
@@ -121,8 +134,10 @@ class ProgramList extends Component {
121134
const {
122135
intl,
123136
programs,
137+
user,
124138
tag,
125139
userPrograms,
140+
featuredPrograms,
126141
} = this.props;
127142
const {
128143
confirmOpen,
@@ -136,6 +151,12 @@ class ProgramList extends Component {
136151
defaultMessage: 'My Programs',
137152
});
138153

154+
const featuredProgramsHeader = intl.formatMessage({
155+
id: 'app.program_list.featured_programs',
156+
description: 'Header for all featured programs',
157+
defaultMessage: 'Featured Programs',
158+
});
159+
139160
const otherProgramsHeader = intl.formatMessage({
140161
id: 'app.program_list.other_programs',
141162
description: 'Header for finding other user\'s programs',
@@ -189,12 +210,19 @@ class ProgramList extends Component {
189210
{
190211
userPrograms === null
191212
? (<Loader active />)
192-
: this.programSegment(userPrograms, tag, myProgramsHeader, true)
213+
: this.programSegment(userPrograms, user, tag,
214+
myProgramsHeader, this.fetchUserPrograms)
215+
}
216+
{
217+
featuredPrograms === null
218+
? (<Loader active />)
219+
: this.programSegment(featuredPrograms, user, tag,
220+
featuredProgramsHeader, this.fetchFeaturedPrograms)
193221
}
194222
{
195223
programs === null
196224
? (<Loader active />)
197-
: this.programSegment(programs, tag, otherProgramsHeader, false)
225+
: this.programSegment(programs, user, tag, otherProgramsHeader, this.fetchOtherPrograms)
198226
}
199227
<Confirm
200228
header={dialogHeader}
@@ -223,6 +251,12 @@ ProgramList.defaultProps = {
223251
total_pages: 1,
224252
results: [],
225253
},
254+
featuredPrograms: {
255+
next: null,
256+
previous: null,
257+
total_pages: 1,
258+
results: [],
259+
},
226260
tag: {
227261
tags: [],
228262
},
@@ -237,6 +271,7 @@ ProgramList.propTypes = {
237271
clearProgram: PropTypes.func.isRequired,
238272
user: PropTypes.shape({
239273
user_id: PropTypes.number.isRequired,
274+
username: PropTypes.string.isRequired,
240275
}).isRequired,
241276
programs: PropTypes.shape({
242277
next: PropTypes.string,
@@ -266,6 +301,20 @@ ProgramList.propTypes = {
266301
}),
267302
),
268303
}),
304+
featuredPrograms: PropTypes.shape({
305+
next: PropTypes.string,
306+
previous: PropTypes.string,
307+
total_pages: PropTypes.number,
308+
results: PropTypes.arrayOf(
309+
PropTypes.shape({
310+
id: PropTypes.number.isRequired,
311+
name: PropTypes.string.isRequired,
312+
user: PropTypes.shape({
313+
username: PropTypes.string.isRequired,
314+
}).isRequired,
315+
}),
316+
),
317+
}),
269318
tag: PropTypes.shape({
270319
tags: PropTypes.arrayOf(PropTypes.shape({
271320
name: PropTypes.string,

0 commit comments

Comments
 (0)