connect apply application to database

This commit is contained in:
Naytitorn Chaovirachot 2024-10-14 15:51:00 +07:00
parent 0d9a0821c0
commit a079900756

View File

@ -37,37 +37,82 @@ export default function Apply() {
"100K+", "100K+",
]; ];
// move to lib? // get industry list to display in dropdown
const fetchIndustry = async () => { const fetchIndustry = async () => {
let { data: BusinessType, error } = await supabase let { data: businessType, error } = await supabase
.from("business_type") .from("business_type")
.select("value"); .select("value");
if (!BusinessType) { if (!businessType) {
console.error(error); console.error(error);
} else { } else {
setIndustry(BusinessType.map((item) => item.value)); setIndustry(businessType.map((item) => item.value));
} }
}; };
useEffect(() => { useEffect(() => {
fetchIndustry(); fetchIndustry();
}, []); }, []);
const createFormat = () => ({ // get business id from business type
"Company Name: ": companyName, const getBusinessTypeID = async () => {
"Industry": selectedIndustry, const { data, error } = await supabase
"Money Raised to Date": moneyRaisedToDate, .from('business_type')
"Is in USA": isInUS, .select('id')
"Is for sale": isForSale, .eq('value', selectedIndustry)
"Is generating revenue": isGeneratingRevenue, .single();
"Business Pitch": businessPitch,
"Community Size": selectedCommunitySize, if (error) {
"Created At": new Date().toString() console.error('Error fetching business ID:', error);
}); return;
}
const submitApplication = () => { return data.id;
let format = createFormat(); };
alert(JSON.stringify(format));
// get current user id
const getUserID = async () => {
const { data: { user }, error } = await supabase.auth.getUser();
if (error || !user) {
console.error('Error fetching user:', error);
return;
}
return user.id;
}
// format input data as json
const createFormat = async () => {
return {
"company_name": companyName,
"business_type_id": await getBusinessTypeID(),
"raise_to_date": moneyRaisedToDate, //TODO change to money_raised_to_date in database and change type to number
"is_in_us": isInUS,
"is_for_sale": isForSale,
"is_generating_revenue": isGeneratingRevenue,
"pitch_deck_url": businessPitch,
"community_size": selectedCommunitySize,
"created_at": new Date(),
"user_id": await getUserID()
}
};
// insert into business_application database
const submitApplication = async () => {
let format = await createFormat();
// alert(JSON.stringify(format)) // debug message
const { data, error } = await supabase
.from('business_application')
.insert([format]);
if (error) {
// return div error here
console.error('Error inserting data:', error);
// alert("Error" + JSON.stringify(error));
} else {
alert("Data successfully submitted!")
}
} }
return ( return (