Skip to content

Commit

Permalink
Merge pull request #49 from dsc-sookmyung/feature/search
Browse files Browse the repository at this point in the history
[#48] feat: implement api fetch
  • Loading branch information
hee-suh authored Mar 23, 2022
2 parents 0ce5053 + 993a7c6 commit 07caced
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
40 changes: 33 additions & 7 deletions react-native/screens/SearchResultScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { useState, useEffect } from 'react';
import { StyleSheet, View, ImageBackground, Dimensions } from 'react-native';
import { StyleSheet, View, ImageBackground, Dimensions, Alert } from 'react-native';
import Swiper from 'react-native-swiper';

import SwipeUpDown from 'react-native-swipe-up-down';
import BottomDrawer from '../components/BottomDrawer';
import type { Navigation, Notice } from '../types';
import { useAuth } from '../contexts/Auth';
import { useNavigation, StackActions } from '@react-navigation/native';


const { width } = Dimensions.get('window');

Expand All @@ -21,17 +23,19 @@ interface SearchResultScreenProps {
}

export default function SearchResultScreen(props: SearchResultScreenProps) {
const auth = useAuth();
const navigation = useNavigation();

const [imageUri, setImageUri] = useState("../assets/images/calendar.png");
const [notice, setNotice] = useState<Notice>({id: 1, cid: 2, date: "", saved_titles: [], results: []});
const [notice, setNotice] = useState<Notice>({id: 1, date: "", saved_titles: [], results: []});
const [showKorean, setShowKorean] = useState<boolean>(false);
const [isFullDrawer, setFullDrawer] = useState<boolean>(false);

useEffect(() => {
// TODO: Fetch API
// search/{props.route.params.id}
// TODO: Fetch API
// mockup data
setNotice({
id: 1,
cid: 1,
date: "2022-02-10",
saved_titles: [
"17th Graduation Ceremony",
Expand All @@ -55,7 +59,29 @@ export default function SearchResultScreen(props: SearchResultScreenProps) {
korean: "개학일은 3월 2일이며, 개학식에 참여하고자 하는 학부모님께서는 10시까지 강당으로 오시기 바랍니다."
}]
})
}, [])

if (auth?.authData?.jwt_token) {
fetch(`http://localhost:8080/search/${props.route.params.id}`, {
method: 'GET',
headers: {
'JWT_TOKEN': auth.authData.jwt_token
},
redirect: 'follow'
})
.then(response => response.json())
.then(data => setNotice(data))
.catch(function (error) {
console.log(error.response.status) // 401
console.log(error.response.data.error) //Please Authenticate or whatever returned from server
if(error.response.status==401) {
//redirect to login
Alert.alert("The session has expired. Please log in again.");
auth.signOut();
navigation.dispatch(StackActions.popToTop())
}
});
}
}, [auth])

const handleKorean = (): void => {
setShowKorean(!showKorean);
Expand Down
34 changes: 27 additions & 7 deletions react-native/screens/SearchScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, TouchableHighlight } from 'react-native';
import { StyleSheet, Text, View, TouchableHighlight, Alert } from 'react-native';
import type { Navigation, Notice } from '../types';
import SearchedNotice from '../components/SearchedNotice';
import SearchBar from 'react-native-elements/dist/searchbar/SearchBar-ios';
import DateTimePickerModal from "react-native-modal-datetime-picker"
import { Column } from 'native-base';
import { useAuth } from '../contexts/Auth';
import { StackActions } from '@react-navigation/native';


export default function SearchScreen({ navigation }: Navigation) {
const auth = useAuth(); // TODO: get notices by send header(`auth.AuthData`) to server
const auth = useAuth();

const [search, setSearch] = useState<string>('');
const [filteredNotices, setFilteredNotices] = useState<Notice[]>(
[
{
id: 1,
cid: 1,
date: "2022-02-19",
saved_titles: [
"17th Graduation Ceremony",
Expand All @@ -25,7 +24,6 @@ export default function SearchScreen({ navigation }: Navigation) {
},
{
id: 1,
cid: 1,
date: "2022-02-10",
saved_titles: [
"17th Graduation Ceremony",
Expand All @@ -38,7 +36,6 @@ export default function SearchScreen({ navigation }: Navigation) {
[
{
id: 1,
cid: 1,
date: "2022-02-19",
saved_titles: [
"17th Graduation Ceremony",
Expand All @@ -47,7 +44,6 @@ export default function SearchScreen({ navigation }: Navigation) {
},
{
id: 1,
cid: 1,
date: "2022-02-10",
saved_titles: [
"17th Graduation Ceremony",
Expand All @@ -58,6 +54,30 @@ export default function SearchScreen({ navigation }: Navigation) {
const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
const [searchDate, setSearchDate] = useState<string>("Click calendar icon to select date.");

useEffect(() => {
if (auth?.authData?.jwt_token) {
fetch('http://localhost:8080/search', {
method: 'GET',
headers: {
'JWT_TOKEN': auth.authData.jwt_token
},
redirect: 'follow'
})
.then(response => response.json())
.then(data => setNotices(data))
.catch(function (error) {
console.log(error.response.status) // 401
console.log(error.response.data.error) //Please Authenticate or whatever returned from server
if(error.response.status==401) {
//redirect to login
Alert.alert("The session has expired. Please log in again.");
auth.signOut();
navigation.dispatch(StackActions.popToTop())
}
});
}
}, [auth])

const showDatePicker = () => {
setDatePickerVisibility(true);
};
Expand Down
1 change: 0 additions & 1 deletion react-native/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ interface Result {

interface Notice {
id: number,
cid: number,
date: string,
saved_titles: string[],
results?: Result[]
Expand Down

0 comments on commit 07caced

Please sign in to comment.