Skip to content

Commit

Permalink
🐛 Fixed potential type confusion and refactor style
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao committed Dec 13, 2023
1 parent dfba0ec commit 3517660
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions components/Group/AreaChips.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const StyledAreaChips = styled.ul`
display: flex;
flex-wrap: wrap;
margin-bottom: 16px;
gap: 12px 0;
`;

const AreaChips = () => {
Expand Down
2 changes: 1 addition & 1 deletion components/Group/GroupList/GroupCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function GroupCard({
</StyledText>
<StyledText>
<StyledLabel>適合階段</StyledLabel>
<span>{partnerEducationStep}</span>
<span>{partnerEducationStep || '皆可'}</span>
</StyledText>
</StyledInfo>
<StyledText lineClamp="2" fontSize="14px">
Expand Down
1 change: 0 additions & 1 deletion components/Group/GroupList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export const StyledGroupItem = styled.li`
const StyledGroupList = styled.ul`
display: flex;
flex-wrap: wrap;
justify-content: space-between;
`;

function GroupList() {
Expand Down
6 changes: 3 additions & 3 deletions pages/api/__mocks__/group.mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"photoURL": "https://images.unsplash.com/photo-1583526241256-cb18e8635e5b?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
"photoAlt": "封面圖",
"category": ["教育", "生活", "商業與社會創新", "綜合型學習資源"],
"partnerEducationStep": "大學",
"partnerEducationStep": "高中、大學",
"description": "作為一位數學和科學老師,我理解學生對於這些學科的挑戰和挫折感,當遇到問題,不知從何想起,迷路的感覺。",
"area": "南投縣",
"isGrouping": true
Expand All @@ -38,7 +38,7 @@
"photoURL": "",
"photoAlt": "",
"category": ["藝術", "自然科學", "生活"],
"partnerEducationStep": "其他",
"partnerEducationStep": null,
"description": "希望能像朋友,一起讀有興趣的科目每週1-2次見面練習這兩種,每次總時數2-3小時不限,希望你跟我一樣很想追求有效進步也不怕辛苦!一起讀日文也可以喔!",
"area": "嘉義縣、線上",
"isGrouping": true
Expand All @@ -49,7 +49,7 @@
"photoURL": "",
"photoAlt": "",
"category": ["人文社會", "運動/心理/醫學", "生活"],
"partnerEducationStep": "其他",
"partnerEducationStep": null,
"description": "我家小朋友14歲,目前常駐在上海,申請在家自學。目前自己規畫學習進度。主要有興趣做些TTS、Vits、Yolov的聲音、視覺項目。不知道有沒有同好一起來研究、交流!",
"area": "新竹縣、新竹市",
"isGrouping": true
Expand Down
21 changes: 13 additions & 8 deletions pages/api/group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import groupMockData from '../__mocks__/group.mock.json';

/**
* 根據傳入的條件進行篩選
* @param {string[]} itemValue - 資料的值
* @param {string[]} conditionValue - 篩選條件的值
* @param {null | string[]} itemArray - 資料的值
* @param {string | string[]} conditionValue - 篩選條件的值
* @returns {boolean} - 是否符合保留條件
*/
function filterBy(itemValue, conditionValue = []) {
function some(itemArray, conditionValue) {
const conditionArray = Array.isArray(conditionValue)
? conditionValue
: conditionValue?.split(',') || [];

return (
!conditionValue.length ||
conditionValue.every((cond) => itemValue.includes(cond))
!conditionArray.length ||
!Array.isArray(itemArray) ||
conditionArray.some((cond) => itemArray.includes(cond))
);
}

Expand Down Expand Up @@ -43,10 +48,10 @@ export default function handler(req, res) {
const end = Number(limit);
const filterData = groupMockData
.filter((item) => !q || item.title.includes(q))
.filter((item) => !edu || edu.includes(item.partnerEducationStep))
.filter((item) => !grouping || item.isGrouping === isGrouping)
.filter((item) => filterBy(item.area.split('、'), area?.split(',')))
.filter((item) => filterBy(item.category, category?.split(',')));
.filter((item) => some(item.area?.split('、'), area))
.filter((item) => some(item.category, category))
.filter((item) => some(item.partnerEducationStep?.split('、'), edu));

const items = filterData.slice(0, end);

Expand Down

0 comments on commit 3517660

Please sign in to comment.