Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
plugins: [
'react',
],
ignorePatterns: ['build/', 'node_modules/'],
rules: {
indent: ['error', 2],
'no-trailing-spaces': 'error',
Expand Down
14 changes: 4 additions & 10 deletions src/reducers/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,10 @@ const { actions, reducer } = createSlice({
},

reducers: {
setStudyGroups(state, { payload: { groups, tag } }) {
setStudyGroups(state, { payload: groups }) {
return {
...state,
groups: tag ? groups.reduce((studies, group) => {
if (group.tags.includes(tag)) {
return [...studies, group];
}

return studies;
}, []) : groups,
groups,
};
},

Expand Down Expand Up @@ -167,9 +161,9 @@ export const {
} = actions;

export const loadStudyGroups = (tag) => async (dispatch) => {
const groups = await getStudyGroups();
const groups = await getStudyGroups(tag);

dispatch(setStudyGroups({ groups, tag }));
dispatch(setStudyGroups(groups));
};

export const loadStudyGroup = (id) => async (dispatch) => {
Expand Down
40 changes: 10 additions & 30 deletions src/reducers/slice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,17 @@ describe('reducer', () => {
});

describe('setStudyGroups', () => {
context('with tag', () => {
it('get study groups list with tags filtered', () => {
const initialState = {
groups: [],
};

const state = reducer(
initialState,
setStudyGroups({ groups: STUDY_GROUPS, tag: 'JavaScript' }),
);

expect(state.groups).toHaveLength(1);
});
});

context('without tag', () => {
it("get study groups list doesn't tags filtered", () => {
const initialState = {
groups: [],
};
it('get study groups list', () => {
const initialState = {
groups: [],
};

const state = reducer(
initialState,
setStudyGroups({ groups: STUDY_GROUPS, tag: '' }),
);
const state = reducer(
initialState,
setStudyGroups(STUDY_GROUPS),
);

expect(state.groups).toHaveLength(2);
});
expect(state.groups).toHaveLength(2);
});
});

Expand Down Expand Up @@ -315,10 +298,7 @@ describe('async actions', () => {

const actions = store.getActions();

expect(actions[0]).toEqual(setStudyGroups({
groups: [],
tag: undefined,
}));
expect(actions[0]).toEqual(setStudyGroups([]));
});
});

Expand Down
12 changes: 10 additions & 2 deletions src/services/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { db, auth } from './firebase';

export const getStudyGroups = async () => {
const response = await db.collection('groups').get();
const branchGetGroups = (tag) => {
if (tag) {
return db.collection('groups').where('tags', 'array-contains', tag).get();
}

return db.collection('groups').get();
};

export const getStudyGroups = async (tag) => {
const response = await branchGetGroups(tag);

const groups = response.docs.map((doc) => ({
...doc.data(),
Expand Down