Skip to content

Commit

Permalink
Implemented adding references
Browse files Browse the repository at this point in the history
  • Loading branch information
hkam0006 committed Sep 12, 2024
1 parent c3099e6 commit e359855
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 8 deletions.
13 changes: 13 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const useAddPreviousTenancy = () => {
const addPreviousTenancy = useCallback(async (renter_id, previous_tenancy_address, previous_tenancy_contact_name, previous_tenancy_contact_email, previous_tenancy_contact_phone) => {
if (renter_id && previous_tenancy_address && previous_tenancy_contact_name && previous_tenancy_contact_email && previous_tenancy_contact_phone) {
const { data, error } = await supabase
.from('PREVIOUSTENANCY')
.from('PREVIOUS-TENANCY')
.insert([{
renter_id,
previous_tenancy_address,
Expand Down
33 changes: 33 additions & 0 deletions frontend/src/mutators/Renter Employer/useAddRenterEmployer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { supabase } from "../../supabase"
import { useCallback } from 'react'

const useAddRenterEmployer = () => {
const addRenterEmployer = useCallback(async (
renter_id,
renter_employment_id,
renter_employer_name,
renter_employer_phone_number,
renter_employer_email,
) => {
if (renter_id && renter_employment_id && renter_employer_name && renter_employer_phone_number && renter_employer_email) {
const { data, error } = await supabase
.from('RENTER-EMPLOYER')
.insert([{
renter_id,
renter_employment_id,
renter_employer_name,
renter_employer_phone_number,
renter_employer_email,
}])
.select('*')
if (error) {
throw error
}
return data
}
}, [])

return addRenterEmployer
}

export default useAddRenterEmployer
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const useGetRenterEmploymentsByRenterID = (renter_id) =>{
useEffect(() => {
const fetchRenterEmployments = async () => {
const { data, error } = await supabase
.from("RENTER EMPLOYMENT")
.from("RENTER-EMPLOYMENT")
.select("*")
.eq("renter_id", renter_id)
.order("renter_employment_end", { ascending: true, nullsFirst: true })
Expand Down
232 changes: 232 additions & 0 deletions frontend/src/renter-components/references/AddReferenceModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import React, { useState } from 'react'
import { Modal, Fade, Typography, Box, Stack, IconButton, TextField, FormControl, InputLabel, Select, MenuItem, Button} from '@mui/material'
import Backdrop from '@mui/material/Backdrop';
import useAddPreviousTenancy from '../../mutators/Previous Tenancy/useAddPreviousTenancy';
import { useSelector } from 'react-redux';
import useGetRenterEmploymentsByRenterID from '../../queries/Renter Employment/useGetRenterEmploymentsByRenterID';
import useAddRenterEmployer from '../../mutators/Renter Employer/useAddRenterEmployer';

const style = {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 500,
bgcolor: 'background.paper',
boxShadow: 24,
p: 4,
};

const AddReferenceModal = ({onClose}) => {
const renterId = useSelector(state => state.user.currentUser?.renter_id)
const addPreviousTenancy = useAddPreviousTenancy()
const addRenterEmployer = useAddRenterEmployer()
const fetchedEmployments = useGetRenterEmploymentsByRenterID(renterId)
const [formData, setFormData] = useState({
type: "",
name: "",
email: "",
phone: "",
address: "",
employer_id: ""
})

const [formError, setFormError] = useState({
type: "",
name: "",
email: "",
phone: "",
address: "",
employer_id: ""
})

const validateForm = (name, value, blur) => {
if (blur){
if (value.length === 0){
const message = "Field cannot be empty"
setFormError((prev) => ({
...prev,
[name]: message
}))
} else {
setFormError((prev) => ({
...prev,
[name]: ""
}))
}
}
}

const handleFormChange = (e, blur) => {
const {value, name} = e.target;
validateForm(name, value, blur)
setFormData((prev) => ({
...prev,
[name]: value
}))
}

const handleSubmitForm = () => {
validateForm("name", formData.name, true)
validateForm("email", formData.email, true)
validateForm("phone", formData.phone, true)
validateForm("type", formData.type, true)
if (formData.type === 'rental'){
validateForm("address", formData.address, true)
}
if (formData.type === 'employer'){
validateForm("employer_id", formData.employer_id, true)
}

if (formData.type === 'rental'){
// handle adding rental reference
addPreviousTenancy(
renterId,
formData.address,
formData.name,
formData.phone,
formData.email
).then(() => {
onClose()
}).catch(() => {
setFormError((prev) => ({
...prev,
phone: "Error adding rental reference"
}))
})
} else if (formData.type === 'employer'){
// handle adding employer reference
addRenterEmployer(
renterId,
formData.employer_id,
formData.name,
formData.phone,
formData.email
).then(() => {
onClose()
}).catch(() => {
setFormError((prev) => ({
...prev,
phone: "Error adding employer reference"
}))
})
}
}

return (
<Modal
aria-labelledby="add-reference"
aria-describedby="modal-for-adding-reference-information"
open={true}
onClose={onClose}
closeAfterTransition
slots={{ backdrop: Backdrop }}
slotProps={{
backdrop: {
timeout: 500,
},
}}
>
<Fade in={true}>
<Box sx={style}>
<Stack direction='row' justifyContent='space-between' >
<Typography variant='h5'>Add Reference</Typography>
<IconButton sx={{borderRadius: 10}} onClick={onClose}>&times;</IconButton>
</Stack>
<Typography variant="p" component="p" gutterBottom>
You must have this person’s consent to provide their personal information and be contacted by us during business hours.
</Typography>
<Stack spacing={2} mt={2}>
<Stack>
<FormControl fullWidth>
<InputLabel id="demo-simple-select-label">Type</InputLabel>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={formData.type}
name="type"
label='Type'
onChange={(e) => handleFormChange(e, true)}
>
<MenuItem value=""></MenuItem>
<MenuItem value="rental">Previous Tenancy</MenuItem>
<MenuItem value="employer">Employment</MenuItem>
</Select>
</FormControl>
{Boolean(formError.type) && <Typography color='error'>{formError.type}</Typography>}
</Stack>
{formData.type === 'rental' && <Stack>
<TextField
name='address'
label='Address'
value={formData.address}
onChange={(e) => handleFormChange(e, false)}
onBlur={(e) => handleFormChange(e, true)}
/>
{Boolean(formError.address) && <Typography color='error'>{formError.address}</Typography>}
</Stack>}
{formData.type === 'employer' && <Stack>
<FormControl fullWidth>
<InputLabel id="demo-simple-select-label">Company</InputLabel>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={formData.employer_id}
name="employer_id"
label='Employer'
onChange={(e) => handleFormChange(e, true)}
>
<MenuItem value=""></MenuItem>
{fetchedEmployments.map((employer) => {
return (
<MenuItem
key={employer.renter_employment_id}
value={employer.renter_employment_id}
>
{employer.renter_employment_location}
</MenuItem>
)
})}
</Select>
</FormControl>
{Boolean(formError.employer_id) && <Typography color='error'>{formError.employer_id}</Typography>}
</Stack>}
<Stack>
<TextField
label='Name'
name='name'
onChange={(e) => handleFormChange(e, false)}
onBlur={(e) => handleFormChange(e, true)}
/>
{Boolean(formError.name) && <Typography color='error'>{formError.name}</Typography>}
</Stack>
<Stack>
<TextField
label='Email'
name='email'
onChange={(e) => handleFormChange(e, false)}
onBlur={(e) => handleFormChange(e, true)}
/>
{Boolean(formError.email) && <Typography color='error'>{formError.email}</Typography>}
</Stack>
<Stack>
<TextField
label='Phone'
name="phone"
onChange={(e) => handleFormChange(e, false)}
onBlur={(e) => handleFormChange(e, true)}
/>
{Boolean(formError.phone) && <Typography color='error'>{formError.phone}</Typography>}
</Stack>
<Stack direction='row' spacing={1} sx={{justifyContent: "flex-end"}}>
<Button variant='outlined' color='error'>Cancel</Button>
<Button variant='contained' onClick={handleSubmitForm}>Add</Button>
</Stack>
</Stack>
</Box>
</Fade>
</Modal>
)
}

export default AddReferenceModal
4 changes: 2 additions & 2 deletions frontend/src/renter-components/references/ReferenceModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const ReferenceModal = ({onClose, reference}) => {
<IconButton onClick={onClose}>&times;</IconButton>
</Stack>
<Stack spacing={2} mt={2}>
<Stack sx={{display: !information.hasOwnProperty("address") ? 'none' : "show",}}>
{information.hasOwnProperty("address") && <Stack>
<TextField
onChange={(e) => handleFormChange(e, false)}
onBlur={(e) => handleFormChange(e, true)}
Expand All @@ -214,7 +214,7 @@ const ReferenceModal = ({onClose, reference}) => {
sx={disabled_text_sx}
/>
<Box sx={{display: Boolean(errorInformation.address) ? "show" : "hidden" }} ><Typography color="error">{errorInformation.address}</Typography></Box>
</Stack>
</Stack>}
<Stack>
<TextField
onChange={(e) => handleFormChange(e, false)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import ReferenceModal from './ReferenceModal';
import useSubscribeTableByRenterID from '../../subscribers/useSubscribeTableByRenterID';
import useRemovePreviousTenancy from '../../mutators/Previous Tenancy/useRemovePreviousTenancyByID';
import useRemoveRenterEmployer from '../../mutators/Renter Employer/useRemoveRenterEmployerbyID';
import AddReferenceModal from './AddReferenceModal';

const TenantReferences = () => {
const renterId = useSelector(state => state.user.currentUser?.renter_id)
const [loading, setLoading] = useState(true)
const [editModal, setEditModal] = useState()
const [addModal, setAddModal] = useState(false)
const [employerReferences, setEmployerReferences] = useState([])
const [rentalHistoryReferences, setRentalHistoryReferences] = useState([])
const removePreviousTenancy = useRemovePreviousTenancy();
Expand Down Expand Up @@ -95,16 +97,14 @@ const TenantReferences = () => {
return (
<NavigationMenu>
{Boolean(editModal) && <ReferenceModal onClose={() => setEditModal(null)} reference={editModal}/>}
{addModal && <AddReferenceModal onClose={() => setAddModal(false)}/>}
<div style={{ padding: "20px", marginTop: "64px" }}>
<Stack direction='row' gap={2} sx={{alignItems: "center"}}>
<Typography variant="h4" component="h1">
Tenant References
</Typography>
<Button variant='contained'>Add References</Button>
<Button variant='contained' onClick={() => setAddModal(true)}>Add References</Button>
</Stack>
<Typography variant="p" component="p" gutterBottom>
You must have this person’s consent to provide their personal information and be contacted by us during business hours.
</Typography>

{/* Employer References Section */}
<Box sx={{mb: 1}}>
Expand Down

0 comments on commit e359855

Please sign in to comment.