Skip to content

Commit

Permalink
add equal method to advanced filter
Browse files Browse the repository at this point in the history
  • Loading branch information
CelleryLin committed Aug 27, 2024
1 parent e57f9ac commit fab3f92
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
3 changes: 2 additions & 1 deletion app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 62 additions & 27 deletions app/src/components/SelectorSetting/AllCourse.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,27 @@ class AllCourse extends Component {
* @returns {boolean} 如果匹配,返回 true
*/
applyTextFilter = (course, filterName, filter) => {
// console.log(filter);
const courseValue = course[courseDataNameMap[filterName]]?.toLowerCase();
const isInclude = filter.include === undefined ? true : filter.include;
const filterLogic = filter.filterLogic === undefined ? 'include' : filter.filterLogic; // equal, include, exclude
// 使用逗號或空格分割每個組
const filterGroups = filter.value.toLowerCase().split(/[,]/);

return filterGroups.some((group) => {
// 使用空格分割每個關鍵字
const keywords = group.trim().split(/\s+/);
return keywords.every((keyword) =>
isInclude
? courseValue.includes(keyword)
: !courseValue.includes(keyword),
);
return keywords.every((keyword) => {
if (filterLogic === 'equal') {
return courseValue === keyword;
}
if (filterLogic === 'include') {
return courseValue.includes(keyword);
}
if (filterLogic === 'exclude') {
return !courseValue.includes(keyword);
}
return false;
});
});
};

Expand All @@ -176,29 +184,56 @@ class AllCourse extends Component {
*/
applyTimeFilter = (course, filterName, filter) => {
// 檢查是否包含或排除
const isInclude = filter.include === undefined ? true : filter.include;
const filterLogic = filter.filterLogic === undefined ? 'include' : filter.filterLogic;
const isInclude = undefined;

if (filterName === '星期') {
// 檢查是否有任何一天匹配
const daysMatched = courseDayName.some((day) => {
return filter[day] && course[day];
});
return isInclude ? daysMatched : !daysMatched;
if (filterLogic !== 'equal') {
// 檢查是否有任何一天匹配
const daysMatched = courseDayName.some((day) => {
return filter[day] && course[day];
});
return filterLogic === 'include' ? daysMatched : !daysMatched;
}
else {
// 檢查是否有所有天匹配
const daysMatched = courseDayName.every((day) => {
return (filter[day] === true) === (course[day] !== '');
});
return daysMatched;
}

}

if (filterName === '節次') {
// 檢查是否有任何一節匹配
let periodsMatched = false;
courseDayName.forEach((day) => {
if (course[day]) {
periodsMatched =
periodsMatched ||
course[day].split('').some((period) => {
return filter[period];
});
}
});
return isInclude ? periodsMatched : !periodsMatched;
if (filterLogic !== 'equal') {
// 檢查是否有任何一節匹配
let periodsMatched = false;
courseDayName.forEach((day) => {
if (course[day]) {
periodsMatched =
periodsMatched ||
course[day].split('').some((period) => {
return filter[period];
});
}
});
return filterLogic === 'include' ? periodsMatched : !periodsMatched;
}
else {
// 檢查是否有所有節次匹配
let periodsMatched = true;
let filterPeriods = Object.keys(filter).filter((key) => key !== 'active' && key !== 'filterLogic' && filter[key]).sort().join('');
// console.log(filterPeriods);
courseDayName.forEach((day) => {
if (course[day]) {
periodsMatched =
periodsMatched &&
course[day].split('').sort().join('') === filterPeriods;
}
});
return periodsMatched;
}
}

throw new Error(
Expand All @@ -214,14 +249,14 @@ class AllCourse extends Component {
* @returns {boolean} 如果匹配,返回 true
*/
applyOptionFilter = (course, filterName, filter) => {
// equal method is not implemented
let isInclude = filter.filterLogic === 'include' ? 'include' : 'exclude';
const courseValue = course[courseDataNameMap[filterName]]?.toString();
let matched = Object.keys(filter).some((option) => {
if (option === 'active' || option === 'include') return false;
return filter[option] && courseValue === option;
});
return (filter.include === undefined ? true : filter.include)
? matched
: !matched;
return isInclude ? matched : !matched;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class FilterRow extends Component {
...this.props.advancedFilters,
[filterName]: {
...this.props.advancedFilters[filterName],
include: mode === 'include',
filterLogic: mode,
},
};

Expand All @@ -133,7 +133,7 @@ class FilterRow extends Component {
// 創建一個新的選中狀態對象,將所有選項設為選中
const selected = {
active: this.props.advancedFilters[filterName]?.active ?? false,
include: this.props.advancedFilters[filterName]?.include ?? true,
filterLogic: this.props.advancedFilters[filterName]?.filterLogic ?? 'include',
};
this.props.filterOptions[filterName].options.forEach((option) => {
selected[option] = true;
Expand All @@ -159,7 +159,7 @@ class FilterRow extends Component {
...this.props.advancedFilters,
[filterName]: {
active: this.props.advancedFilters[filterName]?.active ?? false,
include: this.props.advancedFilters[filterName]?.include ?? true,
filterLogic: this.props.advancedFilters[filterName]?.filterLogic ?? 'include',
},
};

Expand Down Expand Up @@ -235,7 +235,7 @@ class FilterRow extends Component {

const selected = advancedFilters[filterName] || {};
const textInput = advancedFilters[filterName]?.value || '';
const include = advancedFilters[filterName]?.include ?? true;
const filterLogic = advancedFilters[filterName]?.filterLogic ?? 'include';

return (
<StyledFilterRow>
Expand All @@ -246,8 +246,9 @@ class FilterRow extends Component {
onChange={(e) =>
this.handleFilterModeChange(filterName, e.target.value)
}
value={include ? 'include' : 'exclude'}
value={filterLogic}
>
<option value='equal'>等於</option>
<option value='include'>包含</option>
<option value='exclude'>不包含</option>
</Form.Select>
Expand Down

0 comments on commit fab3f92

Please sign in to comment.