Fakultas Ilmu Komputer UI

Commit e6fa1160 authored by Bagus Prabowo's avatar Bagus Prabowo
Browse files

Merge branch 'integrate-create-ecosystem-screen' into 'master'

Integrate create ecosystem function with frontend

See merge request !95
parents 89611697 ec7771fa
Pipeline #86254 passed with stage
in 16 minutes and 12 seconds
......@@ -7,6 +7,7 @@ import {
FlatList,
TouchableOpacity,
SafeAreaView,
Alert,
} from "react-native";
import Colors from "../constants/Colors";
import PlainForm from "../components/Forms/PlainForm";
......@@ -18,10 +19,16 @@ import Spacer from "../components/Spacer/Spacer";
import { ecosystemItem } from "../types/ListItems";
import { getCategoriesAsDdFormat } from "../helpers/ddConverter";
import { CreateEcosystemStackScreenProps } from "../types/navigation";
import { createEcosystem } from "../service/functions/createEcosystem";
import { useUser } from "../hooks/reduxHooks";
import { IEcosystemCategoryMember } from "../types/firestore/ecosystemCategoryMember";
import { IEcosystemMembership } from "../types/firestore/ecosystemMembership";
import { ActivityIndicator } from "react-native-paper";
const CreateEcosystemScreen = ({
route,
}: CreateEcosystemStackScreenProps<"CreateEcosystemScreen">) => {
const nav = useNavigation();
const user = useUser();
const [ecosystemName, setEcosystemName] = useState("");
const [ecosystemDetails, setEcosystemDetails] = useState("");
const [ecosystemCategory, setEcosystemCategory] = useState("");
......@@ -33,6 +40,8 @@ const CreateEcosystemScreen = ({
const [ecosystemMainBusiness, setEcosystemMainBusiness] =
useState<ecosystemItem>({});
const [isSubmitting, setIsSubmitting] = useState(false);
// const handlePickImage = () => {
// setIsLoading(true);
// pickImage().then((res) => {
......@@ -51,7 +60,6 @@ const CreateEcosystemScreen = ({
});
useEffect(() => {
console.log("test");
if (route.params?.id && route.params?.name) {
switch (route.params?.forGroup) {
case "supplier":
......@@ -116,6 +124,125 @@ const CreateEcosystemScreen = ({
}
};
const handleSubmit = async () => {
setIsSubmitting(true);
const ecosystemCategoryMembers: {
categoryMember: IEcosystemCategoryMember;
ecosystemMemberships: IEcosystemMembership[];
}[] = [];
const suppliers = Object.keys(ecosystemSupplier);
suppliers.forEach((categoryId) => {
const item = ecosystemSupplier[categoryId];
const member: IEcosystemCategoryMember = {
type: "supplier",
categoryId: categoryId,
};
const memberships: IEcosystemMembership[] = [];
Object.keys(item.members).forEach((userId) => {
memberships.push({
userId: userId,
});
});
ecosystemCategoryMembers.push({
categoryMember: member,
ecosystemMemberships: memberships,
});
});
const customer = Object.keys(ecosystemCustomer);
customer.forEach((categoryId) => {
const item = ecosystemCustomer[categoryId];
const member: IEcosystemCategoryMember = {
type: "customer",
categoryId: categoryId,
};
const memberships: IEcosystemMembership[] = [];
Object.keys(item.members).forEach((userId) => {
memberships.push({
userId: userId,
});
});
ecosystemCategoryMembers.push({
categoryMember: member,
ecosystemMemberships: memberships,
});
});
const support = Object.keys(ecosystemSupport);
support.forEach((categoryId) => {
const item = ecosystemSupport[categoryId];
const member: IEcosystemCategoryMember = {
type: "support",
categoryId: categoryId,
};
const memberships: IEcosystemMembership[] = [];
Object.keys(item.members).forEach((userId) => {
memberships.push({
userId: userId,
});
});
ecosystemCategoryMembers.push({
categoryMember: member,
ecosystemMemberships: memberships,
});
});
const mainBusiness = Object.keys(ecosystemMainBusiness);
mainBusiness.forEach((categoryId) => {
const item = ecosystemMainBusiness[categoryId];
const member: IEcosystemCategoryMember = {
type: "main",
categoryId: categoryId,
};
const memberships: IEcosystemMembership[] = [];
Object.keys(item.members).forEach((userId) => {
memberships.push({
userId: userId,
});
});
ecosystemCategoryMembers.push({
categoryMember: member,
ecosystemMemberships: memberships,
});
});
try {
const response = await createEcosystem({
ecosystem: {
name: ecosystemName,
description: ecosystemDetails,
followerCount: 0,
pic: "",
visibility: "public",
raters: 0,
rating: 0,
creatorId: user.id,
categoryId: ecosystemCategory,
},
ecosystemCategoryMembers: ecosystemCategoryMembers,
});
setIsSubmitting(false);
if (response.status === 200 || response.status === "200") {
nav.navigate("MainTab");
} else {
console.log(response);
Alert.alert("Error", "error creating new ecosystem");
}
} catch (e) {
setIsSubmitting(false);
console.log(e);
Alert.alert("Error", "error creating new ecosystem");
}
};
return (
<SafeAreaView>
<FlatList
......@@ -491,10 +618,19 @@ const CreateEcosystemScreen = ({
/>
)}
<Spacer variant="xl" />
{isSubmitting && (
<ActivityIndicator
size={"large"}
animating={true}
/>
)}
<Spacer variant="xl" />
<MainButton
text="lanjut"
colors="primary"
onPress={() => {}}
onPress={() => {
handleSubmit();
}}
/>
<Spacer variant="xl" />
<Spacer variant="xl" />
......
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