Fakultas Ilmu Komputer UI

Commit 6d49cb4c authored by Ahmad Izzudin Alifyandra's avatar Ahmad Izzudin Alifyandra
Browse files

Merge branch 'service-ecosystem' into 'PBI-13-Create_Ecosystem_Page'

Add firebase service ecosystem functions

See merge request !82
parents 66bb9b6f d5434722
Pipeline #86209 passed with stage
in 9 minutes and 9 seconds
......@@ -3,13 +3,11 @@ import React from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { Provider as StoreProvider } from "react-redux";
import useCachedResources from "./src/hooks/useCachedResources";
import useColorScheme from "./src/hooks/useColorScheme";
import Navigation from "./src/navigation";
import store from "./src/redux/store";
export default function App() {
const isLoadingComplete = useCachedResources();
const colorScheme = useColorScheme();
if (!isLoadingComplete) {
return null;
} else {
......
......@@ -39,6 +39,7 @@ export default {
appId: process.env.appId,
measurementId: process.env.measurementId,
env: process.env.env,
cloudFunctionsUrl: process.env.cloudFunctionsUrl,
},
},
};
......@@ -5,10 +5,14 @@ const FirebaseConfig = {
apiKey: Constants.manifest.extra?.apiKey || process.env.apiKey,
authDomain: Constants.manifest.extra?.authDomain || process.env.authDomain,
projectId: Constants.manifest.extra?.projectId || process.env.projectid,
storageBucket: Constants.manifest.extra?.storageBucket || process.env.storageBucket,
messagingSenderId: Constants.manifest.extra?.messagingSenderId || process.env.messagingSenderId,
appId: Constants.manifest.extra?.appId || process.env.appId,
measurementId: Constants.manifest.extra?.measurementId || process.env.measurementId,
storageBucket:
Constants.manifest.extra?.storageBucket || process.env.storageBucket,
messagingSenderId:
Constants.manifest.extra?.messagingSenderId ||
process.env.messagingSenderId,
appId: Constants.manifest.extra?.appId || process.env.appId,
measurementId:
Constants.manifest.extra?.measurementId || process.env.measurementId,
};
export default FirebaseConfig;
import Constants from "expo-constants";
export const CLOUD_FUNCTIONS_URL = (Constants.manifest.extra
?.cloudFunctionsUrl || process.env.cloudFunctionsUrl) as string;
......@@ -4,3 +4,5 @@ import type { RootState, AppDispatch } from "../redux/store";
// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export const useUser = () => useAppSelector((state) => state.user);
import { useNavigation } from "@react-navigation/core";
import * as React from "react";
import React from "react";
import { StyleSheet } from "react-native";
import { TouchableOpacity } from "react-native-gesture-handler";
import SmallButton from "../../components/button/SmallButton";
import Spacer from "../../components/Spacer/Spacer";
import { Text, View } from "../../components/Themed";
import Colors from "../../constants/Colors";
import { useUser } from "../../hooks/reduxHooks";
import ecosystemService from "../../service/firestore/ecosystem";
import { createEcosystem } from "../../service/functions/createEcosystem";
const BusinessEcosystemScreen = () => {
const nav = useNavigation();
const user = useUser();
return (
<View style={styles.container}>
......@@ -27,13 +31,7 @@ const BusinessEcosystemScreen = () => {
<View>
<TouchableOpacity
onPress={() => {
nav.navigate("Ecosystem", {
screen: "Recommended",
params: {
headerTitle: "Ekosistem Saya",
toFetch: "MyEcosystem",
},
});
nav.navigate("Ecosystem", { screen: "MyEcosystem" });
}}
>
<Text style={styles.link}>Ekosistem Saya</Text>
......@@ -65,13 +63,7 @@ const BusinessEcosystemScreen = () => {
<View>
<TouchableOpacity
onPress={() => {
nav.navigate("Ecosystem", {
screen: "Recommended",
params: {
headerTitle: "Paling Populer",
toFetch: "Popular",
},
});
nav.navigate("Ecosystem", { screen: "Popular" });
}}
>
<Text style={styles.link}>Paling Populer</Text>
......@@ -81,13 +73,7 @@ const BusinessEcosystemScreen = () => {
<View>
<TouchableOpacity
onPress={() => {
nav.navigate("Ecosystem", {
screen: "Recommended",
params: {
headerTitle: "Terbaru Minggu Ini",
toFetch: "Latest",
},
});
nav.navigate("Ecosystem", { screen: "Latest" });
}}
>
<Text style={styles.link}>Terbaru Minggu Ini</Text>
......@@ -103,6 +89,75 @@ const BusinessEcosystemScreen = () => {
<Text style={styles.link}>Peta Ekosistem</Text>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity
onPress={() => {
createEcosystem({
ecosystem: {
creatorId: "abc",
name: "cebetEco",
description: "abc",
categoryId: "A",
pic: "",
rating: 0,
raters: 0,
followerCount: 0,
visibility: "public",
},
ecosystemCategoryMembers: [
{
categoryMember: {
categoryId: "B",
type: "supplier",
},
ecosystemMemberships: [
{
userId: "7n8uc62bS6UGUl29fODxIp7PAdn2",
},
],
},
{
categoryMember: {
categoryId: "D",
type: "supplier",
},
ecosystemMemberships: [
{
userId: "abciuh3789489ho340hdf",
},
{
userId: "sdfbubsdyuibabciuh3789489ho340h df",
},
{
userId: "uihgybubsdyuibabciuh3789489ho340h df",
},
],
},
],
});
}}
>
<Text style={styles.link}>Create</Text>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity
onPress={() => {
ecosystemService.followEcosystem("FmclOe2dvRbIi7B2Ja5k", user.id);
}}
>
<Text style={styles.link}>Follow</Text>
</TouchableOpacity>
</View>
<View>
<TouchableOpacity
onPress={() => {
ecosystemService.unfollowEcosystem("FmclOe2dvRbIi7B2Ja5k", user.id);
}}
>
<Text style={styles.link}>Unfollow</Text>
</TouchableOpacity>
</View>
</View>
);
};
......
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IUsers } from "../../../types/firestore/users";
import { IEcosystemCategoryMember } from "../../../types/firestore/ecosystemCategoryMember";
import { IUser } from "../../../types/firestore/User";
import { getCategory } from "./getCategory";
export const fetchUsers = async (
ecosystemId: string,
type: string
): Promise<IUsers[]> => {
const db = firebase.firestore();
const ecosystemCategoryMembers: IEcosystemCategoryMember[] = [];
const ecosystemCategoryMemberSnap = await db
.collection("ecosystem_category_members_" + getEnv())
.where("type", "==", type)
.where("ecosystemId", "==", ecosystemId)
.get();
ecosystemCategoryMemberSnap.docs.forEach((doc) => {
const data: IEcosystemCategoryMember = {
...(doc.data() as IEcosystemCategoryMember),
};
ecosystemCategoryMembers.push(data);
});
const users: IUsers[] = [];
for (let i = 0; i < ecosystemCategoryMembers.length; i++) {
const userId = Object.keys(ecosystemCategoryMembers[i].members);
for (let j = 0; j < userId.length; j++) {
users.push({
userId: userId[j],
categoryId: ecosystemCategoryMembers[i].categoryId,
});
}
}
const promises: Promise<void>[] = [];
for (let i = 0; i < users.length; i++) {
const categoryId = users[i].categoryId;
const userId = users[i].userId;
const wrap = async () => {
const categoryName = await getCategory(categoryId);
const userSnap = await db.collection("users").doc(userId).get();
const userData = userSnap.data() as IUser;
users[i].category = categoryName;
users[i].image = userData.pic;
users[i].name = userData.firstName + " " + userData.lastName;
};
promises.push(wrap());
}
await Promise.all(promises);
return users;
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystemFollow } from "../../../types/firestore/ecosystemFollow";
export const followEcosystem = async (ecosystemId: string, userId: string) => {
const db = firebase.firestore();
const followDoc: IEcosystemFollow = {
userId: userId,
ecosystemId: ecosystemId,
};
return await db
.collection("ecosystem_follows_" + getEnv())
.doc(`${userId}_${ecosystemId}`)
.set(followDoc);
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystem } from "../../../types/firestore/ecosystems";
// TODO: implement pagination
export const getByCategory = async (id: string): Promise<IEcosystem[]> => {
const db = firebase.firestore();
const snap = await db
.collection("ecosystems_" + getEnv())
.where("categoryId", "==", id)
.limit(10)
.get();
const ecosystems: IEcosystem[] = [];
snap.docs.forEach((doc) => {
const data: IEcosystem = { ...(doc.data() as IEcosystem), id: doc.id };
ecosystems.push(data);
});
return ecosystems;
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystem } from "../../../types/firestore/ecosystems";
// TODO: implement pagination
export const getByCreated = async (userId: string): Promise<IEcosystem[]> => {
const db = firebase.firestore();
const snap = await db
.collection("ecosystems_" + getEnv())
.where("creatorId", "==", userId)
.limit(10)
.get();
const ecosystems: IEcosystem[] = [];
snap.docs.forEach((doc) => {
const data: IEcosystem = { ...(doc.data() as IEcosystem), id: doc.id };
ecosystems.push(data);
});
return ecosystems;
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystemFollow } from "../../../types/firestore/ecosystemFollow";
import { IEcosystem } from "../../../types/firestore/ecosystems";
// TODO: implement pagination
export const getByFollowed = async (userId: string): Promise<IEcosystem[]> => {
const db = firebase.firestore();
const followedEcosystemIdSnap = await db
.collection("ecosystem_follows_" + getEnv())
.where("userId", "==", userId)
.limit(10)
.get();
const ecosystemId: string[] = [];
followedEcosystemIdSnap.forEach((doc) => {
const data: IEcosystemFollow = {
...(doc.data() as IEcosystemFollow),
id: doc.id,
};
ecosystemId.push(data.ecosystemId);
});
const ecosystems: IEcosystem[] = [];
const promises: Promise<void>[] = [];
for (let i = 0; i < ecosystemId.length; i++) {
const wrap = async () => {
const ecosystemSnap = await db
.collection("ecosystems_" + getEnv())
.doc(ecosystemId[i])
.get();
const ecosystem = ecosystemSnap.data() as IEcosystem;
ecosystems.push(ecosystem);
};
promises.push(wrap());
}
await Promise.all(promises);
return ecosystems;
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystem } from "../../../types/firestore/ecosystems";
// TODO: implement pagination
export const getByPopularity = async (): Promise<IEcosystem[]> => {
const db = firebase.firestore();
const snap = await db
.collection("ecosystems_" + getEnv())
.orderBy("followerCount", "desc")
.limit(10)
.get();
const ecosystems: IEcosystem[] = [];
snap.docs.forEach((doc) => {
const data: IEcosystem = { ...(doc.data() as IEcosystem), id: doc.id };
ecosystems.push(data);
});
return ecosystems;
};
import firebase from "firebase";
import { ICategory } from "../../../types/firestore/categories";
export const getCategory = async (categoryId: string): Promise<string> => {
const db = firebase.firestore();
const snap = await db.collection("categories").doc(categoryId).get();
const category = snap.data() as ICategory;
return category.name;
};
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
import { IEcosystem } from "../../../types/firestore/ecosystems";
// TODO: implement pagination
export const getByMostRecent = async (): // startAfter: number
Promise<IEcosystem[]> => {
const db = firebase.firestore();
const snap = await db
.collection("ecosystems_" + getEnv())
.orderBy("timestamp", "desc")
.limit(10)
// .startAt(startAfter)
.get();
const ecosystems: IEcosystem[] = [];
snap.docs.forEach((doc) => {
const data: IEcosystem = { ...(doc.data() as IEcosystem), id: doc.id };
ecosystems.push(data);
});
return ecosystems;
};
import { fetchUsers } from "./fetchUsers";
import { getByCreated } from "./getByCreated";
import { getByMostRecent } from "./getMostRecent";
import { getByPopularity } from "./getByPopularity";
import { getCategories } from "../categories";
import { followEcosystem } from "./followEcosystem";
import { unfollowEcosystem } from "./unfollowEcosystem";
import { getByFollowed } from "./getByFollowed";
const ecosystemService = {
fetchUsers,
getByCreated,
getByMostRecent,
getByPopularity,
getCategories,
followEcosystem,
unfollowEcosystem,
getByFollowed,
};
export default ecosystemService;
import firebase from "firebase";
import { getEnv } from "../../../helpers/getEnv";
export const unfollowEcosystem = async (
ecosystemId: string,
userId: string
) => {
const db = firebase.firestore();
const followEcosystemId = (
await db
.collection("ecosystem_follows_" + getEnv())
.where("ecosystemId", "==", ecosystemId)
.where("userId", "==", userId)
.get()
).docs[0].id;
return await db
.collection("ecosystem_follows_" + getEnv())
.doc(followEcosystemId)
.delete();
};
import axios from "axios";
import { CLOUD_FUNCTIONS_URL } from "../../constants/urls";
import { getEnv } from "../../helpers/getEnv";
import { IEcosystemCategoryMember } from "../../types/firestore/ecosystemCategoryMember";
import { IEcosystemMembership } from "../../types/firestore/ecosystemMembership";
import { IEcosystem } from "../../types/firestore/ecosystems";
export type createEcosystemParams = {
ecosystem: IEcosystem;
ecosystemCategoryMembers: {
categoryMember: IEcosystemCategoryMember;
ecosystemMemberships: IEcosystemMembership[];
}[];
};
export const createEcosystem = async ({
ecosystem,
ecosystemCategoryMembers,
}: createEcosystemParams) => {
const res = await axios.post(
`${CLOUD_FUNCTIONS_URL}/createEcosystem`,
{
ecosystem: ecosystem,
ecosystemCategoryMembers: ecosystemCategoryMembers,
env: getEnv(),
},
{
headers: {
"Content-Type": "application/json",
},
}
);
console.log(res);
return res;
};
import { IEcosystem } from "../../firestore/ecosystems";
import { IEcosystemCategoryMember } from "../../firestore/ecosystemCategoryMember";
import { IEcosystemMembership } from "../../firestore/ecosystemMembership";
export type ICreateEcosystem = {
ecosystem: IEcosystem;
ecosystemCategoryMembers: [
{
categoryMember: IEcosystemCategoryMember;
ecosystemCategoryMemberships: IEcosystemMembership[];
}
];
};
export type IEcosystemCategoryMember = {
id?: string;
type: "supplier" | "customer" | "support" | "main";
ecosystemId: string;
ecosystemId?: string;
categoryId: string;
members?: {
[id: string]: true;
};
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment