Skip to content

Commit

Permalink
unhardcode businesses logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
joshualipton committed Apr 21, 2024
1 parent 3efc170 commit 4261488
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 32 deletions.
13 changes: 8 additions & 5 deletions src/components/BusinessDashboard/BusinessDashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './BusinessDashboard.module.css';
import { useBackend } from '../../contexts/BackendContext';
import { useAuth } from '../../contexts/AuthContext';
import { useEffect, useState } from 'react';
import {
Table,
Expand Down Expand Up @@ -46,6 +47,7 @@ import ViewDonationHistory from '../BusinessDonationHistory/ViewDonationHistory/
const BusinessDashboard = () => {
const [userName, setUserName] = useState('jit');
const { backend } = useBackend();
const { currentUser } = useAuth();
const navigate = useNavigate();
const [donationData, setDonationData] = useState([]);
const [notifClicked, setNotifClicked] = useState(null);
Expand Down Expand Up @@ -89,8 +91,6 @@ const BusinessDashboard = () => {
'"Homeless?" Card',
];

const businessID = 1;

const formatDate = timestamp => {
const parsedTimestamp = Number(timestamp);
const date = new Date(isNaN(parsedTimestamp) ? timestamp : parsedTimestamp * 1000);
Expand All @@ -114,16 +114,19 @@ const BusinessDashboard = () => {
// in the future will have to change (add parameter to component)
// so that we can display the dashboard for various businesses
// ***********************************************************************
const donationResponse = await backend.get('/donation/business/totals/3');
const businessIdResponse = await backend.get(`/businessUser/${currentUser.uid}`);
const businessId = businessIdResponse.data[0].id;

const donationResponse = await backend.get(`/donation/business/totals/${businessId}`);
setDonationData(donationResponse.data[0]);

const businessResponse = await backend.get(`/business/${businessID}`);
const businessResponse = await backend.get(`/business/${businessId}`);
setUserName(businessResponse.data[0]['contact_name']);

const priceResponse = await backend.get('/value');
setPriceData(priceResponse.data);

const reminderResponse = await backend.get(`/notification/${businessID}`);
const reminderResponse = await backend.get(`/notification/${businessId}`);
setReminderData(reminderResponse.data);
} catch (error) {
console.error('Error fetching data:', error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react';
import { useBackend } from '../../contexts/BackendContext';
import { useAuth } from '../../contexts/AuthContext';
import { SearchIcon, PlusSquareIcon, BellIcon, HamburgerIcon } from '@chakra-ui/icons';
import {
Text,
Expand Down Expand Up @@ -30,20 +31,22 @@ import {
const App = () => {
const { isOpen, onOpen, onClose } = useDisclosure();
const { backend } = useBackend();
const { currentUser } = useAuth();
const [data, setData] = useState([]);

const getData = async () => {
try {
const id = '5';
const response = await backend.get(`/notification/${id}`);
setData(response.data);
console.log(data);
} catch (error) {
console.error('Error fetching data:', error);
}
};

useEffect(() => {
const getData = async () => {
try {
const businessIdResponse = await backend.get(`/businessUser/${currentUser.uid}`);
const businessId = businessIdResponse.data[0].id;

const response = await backend.get(`/notification/${businessId}`);
setData(response.data);
} catch (error) {
console.error('Error fetching data:', error);
}
};

getData();
}, []);

Expand Down
31 changes: 26 additions & 5 deletions src/components/ContactUsForm/ContactUs.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useBackend } from '../../contexts/BackendContext';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useAuth } from '../../contexts/AuthContext';
import PropTypes from 'prop-types';
import { useNavigate } from 'react-router-dom';
import {
Expand Down Expand Up @@ -28,9 +29,9 @@ import {
} from '@chakra-ui/react';

const ContactUs = () => {
const business_ID = 0;
const senderID = 1;
const FPH_ID = 0;
const { backend } = useBackend();
const { currentUser } = useAuth();
const navigate = useNavigate();
const [text, setText] = useState('');
const [checkedItems, setCheckedItems] = useState([0, 0, 0, 0, 0, 0, 0, 0]);
Expand All @@ -47,8 +48,28 @@ const ContactUs = () => {
'Homeless Card 2',
];

const [businessId, setBusinessId] = useState(null);

useEffect(() => {
// Define an async function inside useEffect since useEffect cannot be asynchronous directly
const fetchBusinessId = async () => {
try {
// Fetch business ID from backend
const businessIdResponse = await backend.get(`/businessUser/${currentUser.uid}`);
const fetchedBusinessId = businessIdResponse.data[0].id;
// Set the fetched business ID in state
setBusinessId(fetchedBusinessId);
} catch (error) {
// Handle errors
console.error('Error fetching business ID:', error);
}
};

fetchBusinessId();
}, []);

const formData = {
business_id: business_ID,
business_id: FPH_ID,
message: null,
timestamp: new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' }),
been_dismissed: false,
Expand Down Expand Up @@ -119,7 +140,7 @@ const ContactUs = () => {
const setMessage = text => {
const textCopy = text;

const preMessage = `Business ID: ${senderID} is requesting:`;
const preMessage = `Business ID: ${businessId} is requesting:`;
const messageBody = checkedThingies.map((item, index) => `${item}: ${checkedItems[index]}\n`);
return `${preMessage}\n${messageBody}\nNotes:\n${textCopy}`;
};
Expand Down
31 changes: 24 additions & 7 deletions src/components/DonationForm/DonationForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import {
} from '@chakra-ui/react';
import { CloseIcon, DownloadIcon } from '@chakra-ui/icons';
import { useBackend } from '../../contexts/BackendContext';
import { useState } from 'react';
import { useAuth } from '../../contexts/AuthContext';
import { useEffect, useState } from 'react';
import { PropTypes } from 'prop-types';
import classes from './DonationForm.module.css';
import { useNavigate } from 'react-router-dom';

const DonationForm = () => {
const { backend } = useBackend();
const MAX_ID = 6; // This is the maximum business id in the database. This is a temporary solution since the Figma design does not include a business id in the form.
const { currentUser } = useAuth();
const labels = {
'Canned Cat Food': 'canned_cat_food_quantity',
'Dry Cat Food': 'dry_cat_food_quantity',
Expand All @@ -37,12 +38,13 @@ const DonationForm = () => {
};
const navigate = useNavigate();

const [businessId, setBusinessId] = useState(null);

const [formData, setFormData] = useState({
business_id: MAX_ID, // Since there's no business_id in the form and the database requires a business id (foreign key), the default for now is the maximum business id in the database (9)
business_id: businessId,
canned_cat_food_quantity: null,
canned_dog_food_quantity: null,
date: null,
donation_id: MAX_ID,
dry_cat_food_quantity: null,
dry_dog_food_quantity: null,
email: 'NULL',
Expand All @@ -52,6 +54,21 @@ const DonationForm = () => {
volunteer_hours: null,
});

useEffect(() => {
const fetchBusinessId = async () => {
try {
const businessIdResponse = await backend.get(`/businessUser/${currentUser.uid}`);
const fetchedBusinessId = businessIdResponse.data[0].id;
setBusinessId(fetchedBusinessId);
setFormData(prevState => ({ ...prevState, business_id: fetchedBusinessId }));
} catch (error) {
console.error('Error fetching business ID:', error);
}
};

fetchBusinessId();
}, []);

// const submitForm = async event => {
// event.preventDefault();
// console.log(formData);
Expand Down Expand Up @@ -103,13 +120,13 @@ const DonationForm = () => {
console.log(formData);

const fphNotificationData = {
business_id: formData.business_id,
message: `Business ID: ${formData.business_id} Donation Form Submission`,
business_id: businessId,
message: `Business ID: ${businessId} Donation Form Submission`,
timestamp: new Date().toLocaleString('en-US', {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
}),
been_dismissed: false,
// type: 'Submitted Form',
type: 'Submitted Form',
};
console.log(fphNotificationData);
await backend.post('/notification', fphNotificationData);
Expand Down
13 changes: 10 additions & 3 deletions src/components/EditContactInformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import {
} from '@chakra-ui/react';
import { useEffect, useState } from 'react';
import { useBackend } from '../contexts/BackendContext';
import { useAuth } from '../contexts/AuthContext';
import ReferenceGuide from '../components/ReferenceGuide.jsx';

const EditContactInformation = () => {
const BUSINESS_ID = 1; // hard coded business_id constant for development purposes
const { backend } = useBackend();
const { currentUser } = useAuth();
const [toastOpen, setToastOpen] = useState(false);
const toast = useToast();

Expand Down Expand Up @@ -55,6 +56,8 @@ const EditContactInformation = () => {
business_hours: '',
});

const [businessId, setBusinessId] = useState(null);

function showToast() {
if (!toastOpen) {
setToastOpen(true);
Expand All @@ -79,7 +82,7 @@ const EditContactInformation = () => {
const updateContactInfo = async () => {
try {
setInitialBusinessContactInfo(businessContactInfo);
await backend.put(`/business/${BUSINESS_ID}`, {
await backend.put(`/business/${businessId}`, {
name: businessContactInfo.businessName,
contact_name: `${businessContactInfo.firstName} ${businessContactInfo.lastName}`,
primary_phone: businessContactInfo.phoneNumber,
Expand Down Expand Up @@ -108,7 +111,11 @@ const EditContactInformation = () => {
useEffect(() => {
const getBusinessContactResponse = async () => {
try {
const businessContactResponse = await backend.get(`/business/${BUSINESS_ID}`);
const businessIdResponse = await backend.get(`/businessUser/${currentUser.uid}`);
const fetchedBusinessId = businessIdResponse.data[0].id;
setBusinessId(fetchedBusinessId);

const businessContactResponse = await backend.get(`/business/${fetchedBusinessId}`);
const businessContact = businessContactResponse.data[0];
const name = businessContact.contact_name.split(' ');
const firstName = name[0];
Expand Down
6 changes: 5 additions & 1 deletion src/components/OnBoarding/BusinessFormMaster.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SecondForm from './SecondForm';
import ThirdForm from './ThirdForm';
import FourthForm from './FourthForm';
import { useBackend } from '../../contexts/BackendContext';
import { useAuth } from '../../contexts/AuthContext';
import { Box, Text, SimpleGrid, Stack, Image, Flex } from '@chakra-ui/react';
import propTypes from 'prop-types';
import ICON1 from './icon1.png';
Expand All @@ -13,6 +14,7 @@ import ICON4 from './icon4.png';

const BusinessFormMaster = ({ setFormOpen }) => {
const { backend } = useBackend();
const { currentUser } = useAuth();
const [formData, setFormData] = useState({
businessName: '',
personFirstName: '',
Expand Down Expand Up @@ -108,7 +110,9 @@ const BusinessFormMaster = ({ setFormOpen }) => {

try {
if (formData['termsAndConditionsAccepted'] === true) {
await backend.post('/business', businessData);
const response = await backend.post('/business', businessData);
console.log(response);
await backend.post('businessUser', { id: response.data[0].id, uid: currentUser.uid });
await backend.post('/notification', notification_data);
nextStep();
}
Expand Down

0 comments on commit 4261488

Please sign in to comment.