Skip to content

Commit

Permalink
[#48] feat: client display/update counts
Browse files Browse the repository at this point in the history
  • Loading branch information
junglesub committed Jan 26, 2022
1 parent 1479fc6 commit c4bd38e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions client/src/commons/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const SEARCH_ACTIONS = {
START_SEARCH: 'startSearch',
FINISH_SEARCH: 'finishSearch',
REFLECT_BOOKMARKS: 'reflectBookmarks',
REFLECT_COUNTS: 'reflectCounts',
};

export const SNACKBAR_ACTIONS = {
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/SearchSection/LectureCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function LectureCard({
>
{/* <Typography className={classes.countText}>10</Typography> */}
<Box className={classes.countText}>
<Typography>10</Typography>
<Typography>{lecture.count?.add || ''}</Typography>
</Box>
</Button>
</Tooltip>
Expand All @@ -130,7 +130,7 @@ export default function LectureCard({
>
{/* <Typography className={classes.countText}>10</Typography> */}
<Box className={classes.countText}>
<Typography>10</Typography>
<Typography>{lecture.count?.bookmark || ''}</Typography>
</Box>
</Button>
</Tooltip>
Expand All @@ -143,7 +143,7 @@ export default function LectureCard({
>
{/* <Typography className={classes.countText}>10</Typography> */}
<Box className={classes.countText}>
<Typography>10</Typography>
<Typography>{lecture.count?.spike || ''}</Typography>
</Box>
</Button>
</Tooltip>
Expand Down
14 changes: 14 additions & 0 deletions client/src/hooks/useSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ function searchReducer(state, { type, payload }) {
};
}

case SEARCH_ACTIONS.REFLECT_COUNTS: {
const { lectureId, count } = payload;
const { searchResults } = state;
const idx = searchResults.findIndex((lecture) => lecture.id === lectureId);
if (idx === -1) return state; // 못 찾으면 더 이상 진행 안함

searchResults[idx] = new Lecture(searchResults[idx]).updateCount(count);

return {
...state,
searchResults,
};
}

default:
return state;
}
Expand Down
17 changes: 13 additions & 4 deletions client/src/models/Lecture.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Axios } from '../lib/axios';
import { isIn } from '../utils/helper';

export default class Lecture {
constructor(raw, bookmarks = [], spikes = [], counts = {}) {
constructor(raw, bookmarks = [], spikes = []) {
this.id = raw.id;
this.gubun = raw.gubun;
this.code = raw.code;
Expand All @@ -22,26 +22,35 @@ export default class Lecture {
this.isBookmarked = isIn(raw, bookmarks, 'id');
this.isSpike = isIn(raw, spikes, 'id');
this.isAdded = false;
this.count = raw.count;
}

static getSearchResults = async (search, page) =>
await Axios().get(`/search?search=${search}${page ? `&page=${page}` : ''}`);

updateCount(count) {
this.count = {
...this.count,
...count,
};
return this;
}
}

export class BookmarkedLecture extends Lecture {
constructor(raw, spikes) {
return { ...super(raw, [], spikes), isBookmarked: true };
return super({ ...raw, isBookmarked: true }, [], spikes);
}
}

export class TimetableLecture extends Lecture {
constructor(raw) {
return { ...super(raw), isAdded: true };
return super({ ...raw, isAdded: true });
}
}

export class SpikeLecture extends Lecture {
constructor(raw) {
return { ...super(raw), isSpike: true };
return super({ ...raw, isSpike: true });
}
}
33 changes: 30 additions & 3 deletions client/src/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export default function HomePage({ logout }) {
User.addSpikeLecture(lecture.id).then((res) => {
userDispatch({
type: USER_ACTIONS.ADD_SPIKE_LECTURE,
payload: { lecture: new SpikeLecture(lecture) },
payload: { lecture: new SpikeLecture(lecture).updateCount(res.data.count) },
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId: lecture.id, count: res.data.count },
});
});
};
Expand All @@ -103,14 +107,22 @@ export default function HomePage({ logout }) {
type: USER_ACTIONS.DELETE_SPIKE_LECTURE,
payload: { lectureId: lecture.id },
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId: lecture.id, count: res.data.count },
});
});
};

const handleBookmarkLectureClick = (lecture) => {
User.bookmarkLecture(lecture.id).then((res) => {
userDispatch({
type: USER_ACTIONS.BOOKMARK_LECTURE,
payload: { lecture: new BookmarkedLecture(lecture) },
payload: { lecture: new BookmarkedLecture(lecture).updateCount(res.data.count) },
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId: lecture.id, count: res.data.count },
});
});
};
Expand All @@ -121,6 +133,10 @@ export default function HomePage({ logout }) {
type: USER_ACTIONS.UNBOOKMARK_LECTURE,
payload: { lectureId: lecture.id },
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId: lecture.id, count: res.data.count },
});
});
};

Expand All @@ -142,7 +158,14 @@ export default function HomePage({ logout }) {
Timetable.addLecture(timetableId, lecture.id).then((res) => {
userDispatch({
type: USER_ACTIONS.ADD_LECTURE_TO_TIMETABLE,
payload: { timetableId, lecture: new TimetableLecture(lecture) },
payload: {
timetableId,
lecture: new TimetableLecture(lecture).updateCount(res.data.count),
},
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId: lecture.id, count: res.data.count },
});
});
};
Expand All @@ -154,6 +177,10 @@ export default function HomePage({ logout }) {
type: USER_ACTIONS.DELETE_LECTURE_FROM_TIMETABLE,
payload: { timetableId, lectureId },
});
searchDispatch({
type: SEARCH_ACTIONS.REFLECT_COUNTS,
payload: { lectureId, count: res.data.count },
});
closeModal();
});
};
Expand Down

0 comments on commit c4bd38e

Please sign in to comment.