Skip to content

Commit

Permalink
Merge pull request #60 from pudding0803/main
Browse files Browse the repository at this point in the history
refactor the codes in Bus.js
  • Loading branch information
pudding0803 authored Apr 30, 2023
2 parents 697aece + 73237a1 commit 0a953db
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 155 deletions.
12 changes: 6 additions & 6 deletions config/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 30 additions & 30 deletions controller/Active.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -142,15 +142,15 @@ 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();
const uploadTask = await uploadBytes(storageRef, blob);
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();
Expand All @@ -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 });
Expand All @@ -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;
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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);

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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 = {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -553,23 +553,23 @@ 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) => {
console.log(err);
});
}
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) => {
console.log(err);
});
}

await deleteDoc(doc(db, 'actives', deleteDocId));
await deleteDoc(doc(db, 'activities', deleteDocId));
console.log('deleteOneActive Successful');
}

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 14 additions & 14 deletions controller/Activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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),
});
Expand All @@ -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),
});
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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) => {
Expand All @@ -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 = [];
Expand All @@ -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) => {
Expand All @@ -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 = [];
Expand Down
Loading

0 comments on commit 0a953db

Please sign in to comment.