Skip to content

Commit

Permalink
Merge pull request #172 from kookmin-sw/feature/fe/#108-noticeSearch
Browse files Browse the repository at this point in the history
[FE] 공지사항 검색기능 구현
  • Loading branch information
kevinmj12 authored May 8, 2024
2 parents fa6b239 + 6c61dec commit 3c8450e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
54 changes: 50 additions & 4 deletions front/capstone_front/lib/screens/notice/notice_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class _NoticeScreenState extends State<NoticeScreen> {
var hasNext = true;
var itemCount = 0;
var language = 'KO';
bool isSearchMode = false;

void loadNotices(int lastCursor, String language) async {
try {
Expand All @@ -42,6 +43,24 @@ class _NoticeScreenState extends State<NoticeScreen> {
}
}

void loadNoticesByWord(int lastCursor, String language, String word) async {
try {
NoticesResponse res = await NoticeService.getNoticesByWord(
lastCursor, selectedItem, language, word);
setState(() {
hasNext = res.hasNext;
if (hasNext) {
cursor = res.lastCursorId!;
}
notices.addAll(res.notices);
itemCount += res.notices.length;
});
} catch (e) {
print(e);
throw Exception('error');
}
}

void initLanguageAndLoadNotices() async {
const storage = FlutterSecureStorage();
String? storedLanguage = await storage.read(key: 'language');
Expand Down Expand Up @@ -72,7 +91,11 @@ class _NoticeScreenState extends State<NoticeScreen> {
title: TextField(
controller: _controller,
onChanged: (text) {
setState(() {});
if (text.trim() == "") {
setState(() {
isSearchMode = false;
});
}
},
decoration: InputDecoration(
hintText: "검색어를 입력하세요",
Expand All @@ -84,7 +107,16 @@ class _NoticeScreenState extends State<NoticeScreen> {
? IconButton(
onPressed: () {
_controller.clear();
setState(() {});
setState(() {
setState(() {
cursor = 0;
hasNext = true;
itemCount = 0;
notices = [];
isSearchMode = false;
});
loadNotices(cursor, language);
});
},
icon: const Icon(
Icons.cancel,
Expand All @@ -104,7 +136,16 @@ class _NoticeScreenState extends State<NoticeScreen> {
Icons.search,
color: Theme.of(context).primaryColor,
),
onPressed: () => {},
onPressed: () {
setState(() {
cursor = 0;
hasNext = true;
itemCount = 0;
notices = [];
isSearchMode = true;
});
loadNoticesByWord(cursor, language, _controller.text);
},
),
],
),
Expand Down Expand Up @@ -190,7 +231,12 @@ class _NoticeScreenState extends State<NoticeScreen> {
Expanded(
child: RefreshIndicator(
onRefresh: () async {
loadNotices(0, language);
setState(() {
notices = [];
});
isSearchMode
? loadNoticesByWord(0, language, _controller.text)
: loadNotices(0, language);
},
child: ListView.separated(
itemCount: notices.length,
Expand Down
34 changes: 34 additions & 0 deletions front/capstone_front/lib/services/notice_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,38 @@ class NoticeService {
throw Exception('Failed to load noticeDetail');
}
}

static Future<NoticesResponse> getNoticesByWord(
int cursor, String type, String language, String word) async {
var query = 'type=$type&language=$language&cursor=$cursor&word=$word';
final url = Uri.parse('$baseUrl/announcement/search?$query');
final response = await http.get(url);

List<NoticeModel> noticeInstances = [];

final String decodedBody = utf8.decode(response.bodyBytes);
final Map<String, dynamic> jsonMap = jsonDecode(decodedBody);
if (response.statusCode == 200) {
var apiSuccessResponse = ApiSuccessResponse.fromJson(jsonMap);
var res = apiSuccessResponse.response;
var notices = res['announcements'];

for (var notice in notices) {
noticeInstances.add(NoticeModel.fromJson(notice));
}

var result = NoticesResponse(
notices: noticeInstances,
lastCursorId: res['lastCursorId'],
hasNext: res['hasNext'],
);

return result;
} else {
var apiFailResponse = ApiFailResponse.fromJson(jsonMap);
print(apiFailResponse.message);
print('Request failed with status: ${response.statusCode}.');
throw Exception('Failed to load notices');
}
}
}

0 comments on commit 3c8450e

Please sign in to comment.