From bb7cc6cf4a26868b896449a6f1f3d1ed543dafba Mon Sep 17 00:00:00 2001 From: pudding0803 Date: Thu, 27 Apr 2023 21:41:05 +0800 Subject: [PATCH 1/2] refactor the codes in Bus.js --- controller/Bus.js | 142 ++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 81 deletions(-) diff --git a/controller/Bus.js b/controller/Bus.js index 79637a1..dcff7e5 100644 --- a/controller/Bus.js +++ b/controller/Bus.js @@ -1,9 +1,9 @@ -import url from 'url'; +import { URLSearchParams } from 'url'; import axios from 'axios'; import qs from 'qs'; async function getAuthorizationHeader() { - const parameter = { + const parameters = { grant_type: 'client_credentials', client_id: '110502542-f4ef0225-f7f3-49d7', client_secret: 'b8ba09f0-1be8-4a1f-abc3-f944c69b89ce', @@ -14,7 +14,7 @@ async function getAuthorizationHeader() { const res = await axios({ method: 'POST', url: authUrl, - data: qs.stringify(parameter), + data: qs.stringify(parameters), headers: { 'content-type': 'application/x-www-form-urlencoded' }, }); const accesstoken = res.data; @@ -32,10 +32,10 @@ function getBusTime(data) { const nowM = new Date().getMinutes(); const now = nowH * 60 + nowM; const dataTime = now - ((data.UpdateTime[11] - '0') * 600 + (data.UpdateTime[12] - '0') * 60 + (data.UpdateTime[14] - '0') * 10 + (data.UpdateTime[15] - '0')); - if (data.EstimateTime != null) { + if (data.EstimateTime) { if (data.EstimateTime === 120) busTime = '即將進站'; else if (data.EstimateTime <= 60) busTime = '進站中'; - else busTime = `${Math.floor((data.EstimateTime - dataTime) / 60)}分鐘後`; + else busTime = `${Math.floor((data.EstimateTime - dataTime) / 60)} 分鐘後`; } else if (data.StopStatus === 2) busTime = '不停靠'; else if (data.StopStatus === 3 || data.NextBusTime === undefined) busTime = '末班駛離'; else if (data.StopStatus === 1) { @@ -56,78 +56,62 @@ function busTime9025(StopSequence, Direction) { let busTime; if (Direction === 0) { if (today === 0 || today === 6) { - if (now >= 1080)busTime = '末班駛離'; - else if (now >= 450)busTime = `${Math.floor((1080 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${((1080 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 437)busTime = `${Math.floor((450 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((450 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 420)busTime = `${Math.floor((437 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((437 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + if (now >= 1080) busTime = '末班駛離'; + else if (now >= 450) busTime = `${Math.floor((1080 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${((1080 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 437) busTime = `${Math.floor((450 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((450 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 420) busTime = `${Math.floor((437 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((437 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; else busTime = `${Math.floor((420 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((420 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; - } else if (now >= 1070)busTime = '末班駛離'; - else if (now >= 1020)busTime = `${Math.floor((1070 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((1070 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 960)busTime = `${Math.floor((1020 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((1020 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 720)busTime = `${Math.floor((960 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((960 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + } else if (now >= 1070) busTime = '末班駛離'; + else if (now >= 1020) busTime = `${Math.floor((1070 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((1070 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 960) busTime = `${Math.floor((1020 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((1020 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 720) busTime = `${Math.floor((960 + chart[0][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((960 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; else busTime = `${Math.floor((720 + chart[0][StopSequence - 1]) / 60).toString().padStart(2, '0')}:${Math.floor((720 + chart[0][StopSequence]) % 60).toString().padStart(2, '0')}`; } else if (today === 0 || today === 6) { - if (now >= 1140)busTime = '末班駛離'; - else if (now >= 1090)busTime = `${Math.floor((1140 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1140 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 1020)busTime = `${Math.floor((1090 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1090 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 450)busTime = `${Math.floor((1020 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1020 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + if (now >= 1140) busTime = '末班駛離'; + else if (now >= 1090) busTime = `${Math.floor((1140 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1140 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 1020) busTime = `${Math.floor((1090 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1090 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 450) busTime = `${Math.floor((1020 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${((1020 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; else busTime = `${Math.floor((450 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((450 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; - } else if (now >= 750)busTime = '末班駛離'; - else if (now >= 470)busTime = `${Math.floor((750 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((750 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 430)busTime = `${Math.floor((470 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((470 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; - else if (now >= 397)busTime = `${Math.floor((430 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((430 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + } else if (now >= 750) busTime = '末班駛離'; + else if (now >= 470) busTime = `${Math.floor((750 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((750 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 430) busTime = `${Math.floor((470 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((470 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; + else if (now >= 397) busTime = `${Math.floor((430 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((430 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; else busTime = `${Math.floor((397 + chart[1][StopSequence]) / 60).toString().padStart(2, '0')}:${Math.floor((397 + chart[1][StopSequence]) % 60).toString().padStart(2, '0')}`; return busTime; } -function state9025(StopSequence, Direction) { - if (Direction === 1) { - if (StopSequence === 1) return '松山機場'; - if (StopSequence === 2) return '臺北大學(臺北校區)'; - if (StopSequence === 3) return '行天宮'; - if (StopSequence === 4) return '高雙里'; - if (StopSequence === 5) return '中央大學警衛室'; - if (StopSequence === 6) return '中央大學依仁堂'; - if (StopSequence === 7) return '祐民醫院'; - if (StopSequence === 8) return '新明國中'; - if (StopSequence === 9) return '舊社'; - return '第一銀行'; +function state9025(stopSequence, direction) { + const stops = ['松山機場', '臺北大學(臺北校區)', '行天宮', '高雙里', '中央大學警衛室', '中央大學依仁堂', '祐民醫院', '新明國中', '舊社', '第一銀行'] + if (direction === 1) { + return stops[stopSequence]; + } else { + return stops[9 - stopSequence]; } - if (StopSequence === 10) return '松山機場'; - if (StopSequence === 9) return '臺北大學(臺北校區)'; - if (StopSequence === 8) return '行天宮'; - if (StopSequence === 7) return '高雙里'; - if (StopSequence === 5) return '中央大學警衛室'; - if (StopSequence === 6) return '中央大學依仁堂'; - if (StopSequence === 4) return '祐民醫院'; - if (StopSequence === 3) return '新明國中'; - if (StopSequence === 2) return '舊社'; - return '中壢公車站'; } -function fun9025(parame, response) { - let i; let j; +function fun9025(param, response) { const output = []; const nowH = new Date().getHours(); const nowM = new Date().getMinutes(); const now = nowH * 60 + nowM; - let num; let busTime; let departTime = -1; let departState = 0; const l = response.data.length; - for (i = 0; i < l; i += 1) { + let num, busTime, departTime = -1, departState = 0; + const l = response.data.length; + for (let i = 0; i < l; i++) { num = response.data[i].StopSequence; if (num === departState) continue; - for (j = departState + 1; j < num; j += 1) { + for (let j = departState + 1; j < num; j++) { if (departTime === -1) { - busTime = busTime9025(j, parame.dir); + busTime = busTime9025(j, param.dir); } else { - let time = chart[parame.dir][j] - chart[parame.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); - if (time < chart[parame.dir][j] - chart[parame.dir][departState + 1]) { - time = chart[parame.dir][j] - chart[parame.dir][departState + 1]; + let time = chart[param.dir][j] - chart[param.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); + if (time < chart[param.dir][j] - chart[param.dir][departState + 1]) { + time = chart[param.dir][j] - chart[param.dir][departState + 1]; } if (time <= 2) busTime = '即將進站'; - else busTime = `${time}分鐘後`; + else busTime = `${time} 分鐘後`; } output.push({ - state: state9025(j, parame.dir), + state: state9025(j, param.dir), time: busTime, }); } @@ -135,70 +119,66 @@ function fun9025(parame, response) { if (response.data[i].A2EventType === 1 && !(num === 10 && stay > 1)) { busTime = '進站中'; } else if (departTime === -1) { - busTime = busTime9025(num, parame.dir); + busTime = busTime9025(num, param.dir); } else { - let time = chart[parame.dir][num] - chart[parame.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); - if (time < chart[parame.dir][num] - chart[parame.dir][departState + 1]) { - time = chart[parame.dir][num] - chart[parame.dir][departState + 1]; + let time = chart[param.dir][num] - chart[param.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); + if (time < chart[param.dir][num] - chart[param.dir][departState + 1]) { + time = chart[param.dir][num] - chart[param.dir][departState + 1]; } if (time <= 2) busTime = '即將進站'; - else busTime = `${time}分鐘後`; + else busTime = `${time} 分鐘後`; } output.push({ - state: state9025(num, parame.dir), + state: state9025(num, param.dir), time: busTime, }); departTime = response.data[i].GPSTime; departState = num; } - for (j = departState + 1; j <= 10; j += 1) { + for (let j = departState + 1; j <= 10; j++) { if (departTime === -1) { - busTime = busTime9025(j, parame.dir); + busTime = busTime9025(j, param.dir); } else { - let time = chart[parame.dir][j] - chart[parame.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); - if (time < chart[parame.dir][j] - chart[parame.dir][departState + 1]) { - time = chart[parame.dir][j] - chart[parame.dir][departState + 1]; + let time = chart[param.dir][j] - chart[param.dir][departState] - (now - ((departTime[11] - '0') * 600 + (departTime[12] - '0') * 60 + (departTime[14] - '0') * 10 + (departTime[15] - '0'))); + if (time < chart[param.dir][j] - chart[param.dir][departState + 1]) { + time = chart[param.dir][j] - chart[param.dir][departState + 1]; } if (time <= 2) busTime = '即將進站'; else busTime = `${time}分鐘後`; - }output.push({ - state: state9025(j, parame.dir), + } + output.push({ + state: state9025(j, param.dir), time: busTime, }); } - - // console.log(output2); return output; } -async function afun9025(parame) { +async function afun9025(param) { let Url; - if (parame.dir === 0) Url = "https://tdx.transportdata.tw/api/basic/v2/Bus/RealTimeNearStop/Streaming/InterCity/9025?%24select=GPSTime&%24filter=Direction%20eq%200%20and%20SubRouteName%2FEn%20eq%20'9025A'&%24orderby=StopSequence&%24top=30&%24format=JSON"; + if (param.dir === 0) Url = "https://tdx.transportdata.tw/api/basic/v2/Bus/RealTimeNearStop/Streaming/InterCity/9025?%24select=GPSTime&%24filter=Direction%20eq%200%20and%20SubRouteName%2FEn%20eq%20'9025A'&%24orderby=StopSequence&%24top=30&%24format=JSON"; else Url = "https://tdx.transportdata.tw/api/basic/v2/Bus/RealTimeNearStop/Streaming/InterCity/9025?%24select=GPSTime&%24filter=Direction%20eq%201%20and%20SubRouteName%2FEn%20eq%20'9025A'&%24orderby=StopSequence&%24top=30&%24format=JSON"; const response = await axios.get(Url, { headers: await getAuthorizationHeader(), }); - return fun9025(parame, response); + return fun9025(param, response); } -async function route(parame) { +async function route(param) { const APIBASE = 'https://tdx.transportdata.tw/api/basic/v2/Bus/EstimatedTimeOfArrival/City/Taoyuan'; - const str = `Direction eq ${parame.dir}`; + const str = `Direction eq ${param.dir}`; const data = { $select: 'StopStatus,EstimateTime,StopName,NextBusTime', $filter: str, $orderby: 'StopSequence', $format: 'JSON', }; - const URL = url.parse(`${APIBASE}/${parame.id}`, true); - console.log(URL); - URL.query = data; - console.log(URL); - const Url = url.format(URL); - console.log(Url); + const url = new URL(`${APIBASE}/${param.id}`); + url.search = new URLSearchParams(data).toString(); + const finalUrl = url.toString(); const output = []; - const response = await axios.get(Url, { + const response = await axios.get(finalUrl, { headers: await getAuthorizationHeader(), }); console.log(response.data); From 73237a1a339f040fcf109e64bc3339fca65c12c1 Mon Sep 17 00:00:00 2001 From: pudding0803 Date: Sun, 30 Apr 2023 15:53:47 +0800 Subject: [PATCH 2/2] change firebase apikey and format database names --- config/firebase.js | 12 +++---- controller/Active.js | 60 +++++++++++++++++------------------ controller/Activity.js | 28 ++++++++-------- controller/Bus.js | 4 +-- controller/Event.js | 8 ++--- controller/Message.js | 26 +++++++-------- controller/Profile.js | 4 +-- screens/Event/Send.jsx | 2 +- screens/Social/HomeScreen.jsx | 8 ++--- 9 files changed, 76 insertions(+), 76 deletions(-) diff --git a/config/firebase.js b/config/firebase.js index c59fa7a..4772647 100644 --- a/config/firebase.js +++ b/config/firebase.js @@ -4,12 +4,12 @@ import AsyncStorage from '@react-native-async-storage/async-storage'; // add firebase config const firebaseConfig = { - apiKey: 'AIzaSyA8GH6yj1i4gJM0H_ZTsurYG3Dqn4-nIS8', - authDomain: 'ncu-app-test.firebaseapp.com', - projectId: 'ncu-app-test', - storageBucket: 'ncu-app-test.appspot.com', - messagingSenderId: '739839700130', - appId: '1:739839700130:web:37591d0118a440488cfbfb', + apiKey: "AIzaSyDJKoHZXT8cwLqjI4Fi6feQLGVh1OlP1sY", + authDomain: "ncu-app-production.firebaseapp.com", + projectId: "ncu-app-production", + storageBucket: "ncu-app-production.appspot.com", + messagingSenderId: "532904519089", + appId: "1:532904519089:web:0d3ed0b4d76a020e9e4990" }; // initialize firebase diff --git a/controller/Active.js b/controller/Active.js index 80475fe..5ba90d5 100644 --- a/controller/Active.js +++ b/controller/Active.js @@ -131,7 +131,7 @@ async function addActive(active) { }; if (active.image1) { - const imageAddress = `actives/${imagePos(active.image1)}`; + const imageAddress = `activities/${imagePos(active.image1)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(active.image1); const blob = await response.blob(); @@ -142,7 +142,7 @@ async function addActive(active) { } if (active.image2) { - const imageAddress = `actives/${imagePos(active.image2)}`; + const imageAddress = `activities/${imagePos(active.image2)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(active.image2); const blob = await response.blob(); @@ -150,7 +150,7 @@ async function addActive(active) { item.imageUri2 = await getDownloadURL(uploadTask.ref); } if (active.image3) { - const imageAddress = `actives/${imagePos(active.image3)}`; + const imageAddress = `activities/${imagePos(active.image3)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(active.image3); const blob = await response.blob(); @@ -162,13 +162,13 @@ async function addActive(active) { item.cost = '免費free'; } const db = getFirestore(app); - const activesRef = query(collection(db, 'actives')); + const activesRef = query(collection(db, 'activities')); addDoc(activesRef, item).then(() => { console.log('Document has been added successfully'); }).catch((error) => { console.log(error); }); - const querySnapshot = await getDocs(collection(db, 'actives')); + const querySnapshot = await getDocs(collection(db, 'activities')); querySnapshot.forEach((doc1) => { if (doc1.data().name === item.name) { setDoc(doc(db, 'attendees', `${UserStudent}`, 'hostedEvent', `${doc1.id}`), {}, { merge: true }); @@ -188,7 +188,7 @@ async function updateActive(oldID, NEWactive) { const NEWitem = NEWactive; const db = getFirestore(app); - const activesRef = doc(db, `actives/${oldID}`); + const activesRef = doc(db, `activities/${oldID}`); const querySnapshot = await getDoc(activesRef); if (NEWactive.genre) { defaultRef = defaultLinks[values.indexOf(NEWactive.genre)].link; @@ -213,21 +213,21 @@ async function updateActive(oldID, NEWactive) { if (NEWactive.image2 === 'forward') { // 如果是第二張照片往前補 delete NEWitem.image1; delete NEWitem.image2; - const docRef = doc(db, 'actives', oldID); + const docRef = doc(db, 'activities', oldID); const data = { imageUri1: NEWactive.image1, imageUri2: deleteField(), }; await updateDoc(docRef, data, { merge: true }); } else { - const imageAddress = `actives/${imagePos(NEWactive.image1)}`; + const imageAddress = `activities/${imagePos(NEWactive.image1)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(NEWactive.image1); const blob = await response.blob(); const uploadTask = await uploadBytes(storageRef, blob); NEWitem.imageUri1 = await getDownloadURL(uploadTask.ref); if (querySnapshot.data().imageUri1 !== defaultLinks[values.indexOf(querySnapshot.data().genre)].link) { - const deleteRef = ref(storage, `actives/${querySnapshot.data().imageUri1.substr(-94, 41)}`); + const deleteRef = ref(storage, `activities/${querySnapshot.data().imageUri1.substr(-94, 41)}`); deleteObject(deleteRef).then(() => { console.log('origin image1 has been deleted!'); }).catch((err) => { @@ -241,7 +241,7 @@ async function updateActive(oldID, NEWactive) { if (NEWactive.image2 === 'removed') { delete NEWitem.image2; - const deleteRef = ref(storage, `actives/${querySnapshot.data().imageUri2.substr(-94, 41)}`); + const deleteRef = ref(storage, `activities/${querySnapshot.data().imageUri2.substr(-94, 41)}`); deleteObject(deleteRef).then(() => { console.log('origin image2 has been deleted!'); }).catch((err) => { @@ -250,20 +250,20 @@ async function updateActive(oldID, NEWactive) { } else if (NEWactive.image3 === 'forward') { delete NEWitem.image2; delete NEWitem.image3; - const docRef = doc(db, 'actives', oldID); + const docRef = doc(db, 'activities', oldID); const data = { imageUri2: NEWactive.image2, imageUri3: deleteField(), }; await updateDoc(docRef, data, { merge: true }); } else if (NEWactive.image2 !== undefined) { - const imageAddress = `actives/${imagePos(NEWactive.image2)}`; + const imageAddress = `activities/${imagePos(NEWactive.image2)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(NEWactive.image2); const blob = await response.blob(); const uploadTask = await uploadBytes(storageRef, blob); NEWitem.imageUri2 = await getDownloadURL(uploadTask.ref); - const deleteRef = ref(storage, `actives/${querySnapshot.data().imageUri2.substr(-94, 41)}`); + const deleteRef = ref(storage, `activities/${querySnapshot.data().imageUri2.substr(-94, 41)}`); deleteObject(deleteRef).then(() => { console.log('origin image2 has been deleted!'); }).catch((err) => { @@ -274,20 +274,20 @@ async function updateActive(oldID, NEWactive) { if (NEWactive.image3 === 'removed') { delete NEWitem.image3; - const deleteRef = ref(storage, `actives/${querySnapshot.data().imageUri3.substr(-94, 41)}`); + const deleteRef = ref(storage, `activities/${querySnapshot.data().imageUri3.substr(-94, 41)}`); deleteObject(deleteRef).then(() => { console.log('origin image3 has been deleted!'); }).catch((err) => { console.log(err); }); } else if (NEWactive.image3 !== undefined) { - const imageAddress = `actives/${imagePos(NEWactive.image3)}`; + const imageAddress = `activities/${imagePos(NEWactive.image3)}`; const storageRef = ref(storage, imageAddress); const response = await fetch(NEWactive.image3); const blob = await response.blob(); const uploadTask = await uploadBytes(storageRef, blob); NEWitem.imageUri3 = await getDownloadURL(uploadTask.ref); - const deleteRef = ref(storage, `actives/${querySnapshot.data().imageUri3.substr(-94, 41)}`); + const deleteRef = ref(storage, `activities/${querySnapshot.data().imageUri3.substr(-94, 41)}`); deleteObject(deleteRef).then(() => { console.log('origin image3 has been deleted!'); }).catch((err) => { @@ -310,7 +310,7 @@ async function updateActive(oldID, NEWactive) { async function getAllActive() { const db = getFirestore(app); - const activesRef = query(collection(db, 'actives'), orderBy('uploadTime', 'desc')); + const activesRef = query(collection(db, 'activities'), orderBy('uploadTime', 'desc')); const activeArray = []; const querySnapshot = await getDocs(activesRef); querySnapshot.forEach((doc1) => { @@ -336,7 +336,7 @@ async function getAllActive() { async function getGenreActive(genre) { const db = getFirestore(app); - const activesRef = query(collection(db, 'actives'), where('genre', '==', genre)); + const activesRef = query(collection(db, 'activities'), where('genre', '==', genre)); const GenreArray = []; const querySnapshot = await getDocs(activesRef); @@ -378,7 +378,7 @@ async function getParticipatedActive() { }); for (let i = 0; i < attendIDArray.length; i += 1) { - const refDoc = doc(db, `actives/${attendIDArray[i]}`); + const refDoc = doc(db, `activities/${attendIDArray[i]}`); const result = await getDoc(refDoc); if (new Date(toDateString(result.data().endTime)) > current) { activeArray.push({ @@ -414,7 +414,7 @@ async function getFinishedActive() { attendIDArray.push(attendID.id); }); for (let i = 0; i < attendIDArray.length; i += 1) { - const refDoc = doc(db, `actives/${attendIDArray[i]}`); + const refDoc = doc(db, `activities/${attendIDArray[i]}`); const result = await getDoc(refDoc); if (new Date(toDateString(result.data().endTime)) < current) { activeArray.push({ @@ -440,7 +440,7 @@ async function getFinishedActive() { async function getOneActive(id) { const db = getFirestore(app); - const activesDoc = doc(db, `actives/${id}`); + const activesDoc = doc(db, `activities/${id}`); const querySnapshot = await getDoc(activesDoc); const oneactive = { @@ -480,7 +480,7 @@ async function getOneActive(id) { async function getHangOutActive() { const db = getFirestore(app); - const activesRef = query(collection(db, 'actives'), where('genre', 'in', ['揪人遊戲', '揪人共乘', '揪人運動'])); + const activesRef = query(collection(db, 'activities'), where('genre', 'in', ['揪人遊戲', '揪人共乘', '揪人運動'])); const GenreArray = []; const querySnapshot = await getDocs(activesRef); querySnapshot.forEach((doc1) => { @@ -510,7 +510,7 @@ async function getHangOutActive() { async function getEventActive() { const db = getFirestore(app); - const activesRef = query(collection(db, 'actives'), where('genre', 'in', ['校園活動', '系上活動', '社團活動'])); + const activesRef = query(collection(db, 'activities'), where('genre', 'in', ['校園活動', '系上活動', '社團活動'])); const EventArray = []; const querySnapshot = await getDocs(activesRef); querySnapshot.forEach((doc1) => { @@ -540,11 +540,11 @@ async function getEventActive() { async function deleteOneActive(deleteDocId) { const db = getFirestore(app); - const activesRef = query(doc(db, 'actives', deleteDocId)); + const activesRef = query(doc(db, 'activities', deleteDocId)); const dltDoc = await getDoc(activesRef); if (dltDoc.data().imageUri1 !== defaultLinks[values.indexOf(dltDoc.data().genre)].link) { if (dltDoc.data().imageUri1) { - const uriRef1 = ref(storage, `actives/${dltDoc.data().imageUri1.substr(-94, 41)}`); + const uriRef1 = ref(storage, `activities/${dltDoc.data().imageUri1.substr(-94, 41)}`); deleteObject(uriRef1).then(() => { console.log('Image 1 has been deleted!'); }).catch((err) => { @@ -553,7 +553,7 @@ async function deleteOneActive(deleteDocId) { } } if (dltDoc.data().imageUri2) { - const uriRef2 = ref(storage, `actives/${dltDoc.data().imageUri2.substr(-94, 41)}`); + const uriRef2 = ref(storage, `activities/${dltDoc.data().imageUri2.substr(-94, 41)}`); deleteObject(uriRef2).then(() => { console.log('Image 2 has been deleted!'); }).catch((err) => { @@ -561,7 +561,7 @@ async function deleteOneActive(deleteDocId) { }); } if (dltDoc.data().imageUri3) { - const uriRef3 = ref(storage, `actives/${dltDoc.data().imageUri3.substr(-94, 41)}`); + const uriRef3 = ref(storage, `activities/${dltDoc.data().imageUri3.substr(-94, 41)}`); deleteObject(uriRef3).then(() => { console.log('Image 3 has been deleted!'); }).catch((err) => { @@ -569,7 +569,7 @@ async function deleteOneActive(deleteDocId) { }); } - await deleteDoc(doc(db, 'actives', deleteDocId)); + await deleteDoc(doc(db, 'activities', deleteDocId)); console.log('deleteOneActive Successful'); } @@ -667,7 +667,7 @@ async function getHostedEvent() { hostIDArray.push(doc1.id); }); for (let i = 0; i < hostIDArray.length; i += 1) { - const refDoc = doc(db, `actives/${hostIDArray[i]}`); + const refDoc = doc(db, `activities/${hostIDArray[i]}`); const result = await getDoc(refDoc); const num = await getTotalOfAttendees(result.id); eventArray.push({ @@ -737,7 +737,7 @@ async function getHostInfo(docID) { async function fuseSearchName(searchString) { const db = getFirestore(app); - const activesRef = query(collection(db, 'actives')); + const activesRef = query(collection(db, 'activities')); const activeArray = []; const querySnapshot = await getDocs(activesRef); diff --git a/controller/Activity.js b/controller/Activity.js index 2d879f5..3e38563 100644 --- a/controller/Activity.js +++ b/controller/Activity.js @@ -20,7 +20,7 @@ import { Alert } from 'react-native'; */ export async function getActivityInfo(activityID) { const db = firebase.firestore(); - const doc = await db.collection('Activities').doc(activityID).get(); + const doc = await db.collection('activities').doc(activityID).get(); if (!doc.exists) { // await Alert.alert('No event data found!'); @@ -43,7 +43,7 @@ export async function createAttendance(activityID) { if (numberLimit === attendees.length) { Alert.alert(`${name} has no vacancy now!`); } else { - const docRef = await db.collection('Activities').doc(activityID); + const docRef = await db.collection('activities').doc(activityID); docRef.update({ attendees: firebase.firestore.FieldValue.arrayUnion(uid), }); @@ -61,7 +61,7 @@ export async function deleteAttendance(activityID) { try { const db = firebase.firestore(); const { uid } = firebase.auth().currentUser; - const docRef = await db.collection('Activities').doc(activityID); + const docRef = await db.collection('activities').doc(activityID); docRef.update({ attendees: firebase.firestore.FieldValue.arrayRemove(uid), }); @@ -75,7 +75,7 @@ export async function createActivity(data) { try { const db = firebase.firestore(); const { uid } = firebase.auth().currentUser; - const res = await db.collection('Activities') + const res = await db.collection('activities') .add({ uid, tag: data.tag, @@ -99,7 +99,7 @@ export async function modifyActivity(data) { try { const db = firebase.firestore(); const { uid } = firebase.auth().currentUser; - const res = await db.collection('Activities') + const res = await db.collection('activities') .doc(data.id) .set({ uid, @@ -124,7 +124,7 @@ export async function modifyActivity(data) { export async function deleteActivity(activityID) { try { const db = firebase.firestore(); - const res = await db.collection('Activities').doc(activityID).delete(); + const res = await db.collection('activities').doc(activityID).delete(); return res; } catch (err) { @@ -137,8 +137,8 @@ export async function deleteActivity(activityID) { export async function getTagActivities(tagId) { try { const db = firebase.firestore(); - const activityRef = await db.collection('Activities'); - const activitiesQueryRes = await activityRef.where('ActivitiesTags', '==', tagId).get(); + const activityRef = await db.collection('activities'); + const activitiesQueryRes = await activityRef.where('activitiesTags', '==', tagId).get(); const activitiesRes = []; activitiesQueryRes.forEach((doc) => { const dataObj = doc.data(); @@ -155,10 +155,10 @@ export async function getTagActivities(tagId) { return null; } -export async function getAllActivities() { +export async function getAllactivities() { try { const db = firebase.firestore(); - const activityRef = await db.collection('Activities'); + const activityRef = await db.collection('activities'); const activityQueryRes = await activityRef.get(); const activityRes = []; activityQueryRes.forEach((doc) => { @@ -179,7 +179,7 @@ export async function getAllActivities() { export async function getUserActivities() { try { const db = firebase.firestore(); - const activityRef = await db.collection('Activities'); + const activityRef = await db.collection('activities'); const { uid } = firebase.auth().currentUser; const userActivityQueryRes = await activityRef.where('uid', '==', uid).get(); const userActivityRes = []; @@ -203,7 +203,7 @@ export async function getUserActivities() { export async function getTags() { try { const db = firebase.firestore(); - const activityTagRef = await db.collection('ActivitiesTags'); + const activityTagRef = await db.collection('activitiesTags'); const activityTagQueryRes = await activityTagRef.get(); const activityTagRes = []; activityTagQueryRes.forEach((doc) => { @@ -228,10 +228,10 @@ export async function getTags() { */ export async function querySearch(query, tagId = undefined) { const db = firebase.firestore(); - const itemRef = db.collection('Activities'); + const itemRef = db.collection('activities'); let queryRef = itemRef.where('name', '>=', query); if (tagId) { - queryRef = queryRef.where('ActivitiesTags', '==', tagId); + queryRef = queryRef.where('activitiesTags', '==', tagId); } const querySnapshot = await queryRef.get(); const resultArray = []; diff --git a/controller/Bus.js b/controller/Bus.js index dcff7e5..9a78809 100644 --- a/controller/Bus.js +++ b/controller/Bus.js @@ -83,9 +83,9 @@ function busTime9025(StopSequence, Direction) { function state9025(stopSequence, direction) { const stops = ['松山機場', '臺北大學(臺北校區)', '行天宮', '高雙里', '中央大學警衛室', '中央大學依仁堂', '祐民醫院', '新明國中', '舊社', '第一銀行'] if (direction === 1) { - return stops[stopSequence]; + return stops[stopSequence - 1]; } else { - return stops[9 - stopSequence]; + return stops[10 - stopSequence]; } } diff --git a/controller/Event.js b/controller/Event.js index b0acb3e..6f7b70d 100644 --- a/controller/Event.js +++ b/controller/Event.js @@ -64,7 +64,7 @@ async function checkSubscribe(eventId) { const db = firebase.firestore(); const { uid } = firebase.auth().currentUser; - const calendarRef = await db.collection('CalendarEvents'); + const calendarRef = await db.collection('calendar_events'); const calendarQueryRes = await calendarRef .where('uid', '==', uid) .where('eventId', '==', eventId) @@ -106,7 +106,7 @@ async function unsubscribe(eventId) { const db = firebase.firestore(); const { uid } = firebase.auth().currentUser; - const calendarRef = await db.collection('CalendarEvents'); + const calendarRef = await db.collection('calendar_events'); const calendarQueryRes = await calendarRef .where('uid', '==', uid) .where('eventId', '==', eventId) @@ -219,7 +219,7 @@ async function deleteEvent(eventID) { */ async function getSubscribedEvents() { const db = firebase.firestore(); - const subscriptionRef = await db.collection('CalendarEvents'); + const subscriptionRef = await db.collection('calendar_events'); const { uid } = firebase.auth().currentUser; const subscriptionQueryRes = await subscriptionRef .where('uid', '==', uid) @@ -259,7 +259,7 @@ async function isUserPrvilleged() { const db = firebase.firestore(); const user = firebase.auth().currentUser; if (user) { - const docRef = db.collection('Profiles').doc(user.uid); + const docRef = db.collection('profiles').doc(user.uid); const doc = await docRef.get(); return doc.data().eventPrivilege === true; } diff --git a/controller/Message.js b/controller/Message.js index de1a2cd..a6704dc 100644 --- a/controller/Message.js +++ b/controller/Message.js @@ -78,7 +78,7 @@ function imagePos(imageUri) { async function addMessage(messageData) { const chatroomId = messageData.id; const db = getFirestore(app); - const messageRef = query(collection(db, `chatroom/${chatroomId}/messages`)); + const messageRef = query(collection(db, `chatrooms/${chatroomId}/messages`)); const item = { sender: messageData.sender, type: messageData.type, @@ -127,7 +127,7 @@ async function getNewestMessage(chatroom) { }; // 獲取最後一則消息 limit(1) - const q = query(collection(db, `chatroom/${chatroom.id}/messages`), orderBy('sendTime', 'desc'), limit(1)); + const q = query(collection(db, `chatrooms/${chatroom.id}/messages`), orderBy('sendTime', 'desc'), limit(1)); (await getDocs(q)).forEach((chat) => { if (chat.data() !== null) { if (chat.data().type === 'image') { @@ -178,7 +178,7 @@ async function getNewestMessage(chatroom) { async function deleteMessage(chatroomId, messageID) { const db = getFirestore(app); - const deleteRef = doc(db, `chatroom/${chatroomId}/messages/${messageID}`); + const deleteRef = doc(db, `chatrooms/${chatroomId}/messages/${messageID}`); const deletedDoc = await getDoc(deleteRef); if (deletedDoc.data().type === 'image') { @@ -198,7 +198,7 @@ async function deleteMessage(chatroomId, messageID) { async function findRelateChatroom(userUid) { const rooms = []; const db = getFirestore(app); - const q = query(collection(db, 'chatroom'), where('members', 'array-contains', userUid)); + const q = query(collection(db, 'chatrooms'), where('members', 'array-contains', userUid)); (await getDocs(q)).forEach((chatroom) => { rooms.push({ id: chatroom.id, @@ -218,7 +218,7 @@ async function addChatroom(other, user) { const relate = []; // 檢查現有 - const q = query(collection(db, 'chatroom'), where('members', 'array-contains', user)); + const q = query(collection(db, 'chatrooms'), where('members', 'array-contains', user)); (await getDocs(q)).forEach((chatroom) => { relate.push({ id: chatroom.id, @@ -236,9 +236,9 @@ async function addChatroom(other, user) { // 如果沒有, 就新增一個聊天室 if (exist === false) { - const newRef = doc(collection(db, 'chatroom')); + const newRef = doc(collection(db, 'chatrooms')); const snapshot = await getDoc(newRef); - setDoc(doc(db, `chatroom/${snapshot.id}`), { members: [user, other] }, { merge: true }); + setDoc(doc(db, `chatrooms/${snapshot.id}`), { members: [user, other] }, { merge: true }); returnID = snapshot.id; } return returnID; @@ -247,7 +247,7 @@ async function addChatroom(other, user) { // 監聽即時訊息 async function onSnap(chatroomId) { const db = getFirestore(app); - const dbRef = collection(db, `chatroom/${chatroomId}/messages`); + const dbRef = collection(db, `chatrooms/${chatroomId}/messages`); const message = []; onSnapshot(dbRef, (docsSnap) => { docsSnap.forEach((snap) => { @@ -271,13 +271,13 @@ async function countUnreadMessage(uid) { const db = getFirestore(app); const relate = []; - const q = query(collection(db, 'chatroom'), where('members', 'array-contains', uid)); + const q = query(collection(db, 'chatrooms'), where('members', 'array-contains', uid)); (await getDocs(q)).forEach((chatroom) => { relate.push(chatroom.id); }); const count = []; for (let i = 0; i < relate.length; i += 1) { - const messageRef = query(collection(db, `chatroom/${relate[i]}/messages`)); + const messageRef = query(collection(db, `chatrooms/${relate[i]}/messages`)); (await getDocs(messageRef)).forEach((result) => { if (result.data().read === false && result.data().sender !== uid.trim()) { count.push(result.id); @@ -289,11 +289,11 @@ async function countUnreadMessage(uid) { async function readMessage(uid, chatroomID) { const db = getFirestore(app); - const messageRef = query(collection(db, `chatroom/${chatroomID}/messages`)); + const messageRef = query(collection(db, `chatrooms/${chatroomID}/messages`)); (await getDocs(messageRef)).forEach((result) => { if (result.data().read === false && result.data().sender !== uid) { - setDoc(doc(db, `chatroom/${chatroomID}/messages/${result.id}`), { read: true }, { merge: true }); + setDoc(doc(db, `chatrooms/${chatroomID}/messages/${result.id}`), { read: true }, { merge: true }); } }); } @@ -333,7 +333,7 @@ async function Notification(notifymessage, eventID) { // 傳送消息 for (let i = 0; i < sendList.length; i += 1) { addChatroom(sendList[i], UserStudent).then((returnID) => { - addDoc(collection(db, `chatroom/${returnID}/messages`), item, { merge: true }).then(console.log('notify eveyone succeed')); + addDoc(collection(db, `chatrooms/${returnID}/messages`), item, { merge: true }).then(console.log('notify eveyone succeed')); }); } } diff --git a/controller/Profile.js b/controller/Profile.js index 89acd8f..3a80f35 100644 --- a/controller/Profile.js +++ b/controller/Profile.js @@ -6,7 +6,7 @@ async function getCurrentUserProfile() { async function getProfile(uid) { const db = firebase.firestore(); - const docRef = db.collection('Profiles').doc(uid); + const docRef = db.collection('profiles').doc(uid); const doc = await docRef.get(); if (doc.exists) { return doc.data(); @@ -16,7 +16,7 @@ async function getProfile(uid) { async function updateProfile(uid, data) { const db = firebase.firestore(); - db.collection('Profiles').doc(uid).set(data); + db.collection('profiles').doc(uid).set(data); } export default { diff --git a/screens/Event/Send.jsx b/screens/Event/Send.jsx index 70a990a..1ad0f46 100644 --- a/screens/Event/Send.jsx +++ b/screens/Event/Send.jsx @@ -46,7 +46,7 @@ function Send({ route, navigation }) { }); const db = getFirestore(getApp()); - const dbRef = collection(db, `chatroom/${chatroomId}/messages`); + const dbRef = collection(db, `chatrooms/${chatroomId}/messages`); // const [time, setTime] = useState(); const [getData, setGetData] = useState([]); diff --git a/screens/Social/HomeScreen.jsx b/screens/Social/HomeScreen.jsx index 451e875..883cc2c 100644 --- a/screens/Social/HomeScreen.jsx +++ b/screens/Social/HomeScreen.jsx @@ -7,7 +7,7 @@ import { } from 'react-native-paper'; import styles from './Styles'; import { - getAllActivities, getTags, getTagActivities, querySearch, + getAllactivities, getTags, getTagActivities, querySearch, } from '../../controller/Activity'; function HomeScreen({ navigation }) { @@ -31,7 +31,7 @@ function HomeScreen({ navigation }) { }).catch((err) => { throw err; }); - getAllActivities().then((res) => { + getAllactivities().then((res) => { setItems(res); }).catch((err) => { throw err; @@ -41,7 +41,7 @@ function HomeScreen({ navigation }) { const onRefresh = () => { setRefreshing(true); - getAllActivities().then((res) => { + getAllactivities().then((res) => { setItems(res); setPicked({}); setSearchQuery(''); @@ -74,7 +74,7 @@ function HomeScreen({ navigation }) { onPress={() => { if (tag.name === picked) { setPicked(undefined); - getAllActivities().then((res) => { + getAllactivities().then((res) => { setItems(res); }).catch((err) => { throw err;