Skip to content

Commit

Permalink
Merge pull request #108 from acheung-94/bugfix/validate-dates
Browse files Browse the repository at this point in the history
Bugfix/validate dates
  • Loading branch information
shjang1025 authored May 1, 2024
2 parents 8f81659 + b41d002 commit 34584e1
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 59 deletions.
11 changes: 6 additions & 5 deletions backend/app/models/Pet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ import mongoose, { Schema } from 'mongoose'
const petSchema = Schema({
name: {
type: String,
required: true
required: [true, 'Must provide a name.']
},
dob: {
type: Date,
required: true
required: [true, 'Must provide a birth date.'],
max: [Date.now, 'Pet birthdate can\'t be in the future.']
},
sex: {
type: String,
lowercase: true,
lowercase: [true, 'Must provide a sex.'],
trim: true,
match: /(male|female|unknown)/
},
species: {
type: String,
required: true
required: [true, 'Must provide a species.']
},
color: {
type: String,
required: true
required: [true, 'Must provide a color.']
},
breed: {
type: String,
Expand Down
8 changes: 5 additions & 3 deletions backend/app/models/Reminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import mongoose, { Schema } from 'mongoose'
const reminderSchema = Schema({
title: {
type: String,
required: true,
required: [true, 'Must select a title.'],
maxLength: 32
},
dueDate: {
type: Date,
required: true
required: [true, 'Must enter a date.'],
min: [Date.now, 'Due date can\'t be in the past.']
},
performDate: {
type: Date,
required: false
required: false,
max: [Date.now, 'Perform date can\'t be in the future.']
},
description: {
type: String,
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/components/NewPetFormModal/NewPetFormModal.css
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@

.input-label {
padding-bottom: 5px;
box-sizing: border-box;
display: flex;
flex-direction: column;
}

.input-label input{
Expand Down Expand Up @@ -217,4 +220,9 @@ input[type="file"] {
border:2px solid rgb(205, 205, 205);
border-radius: 16px;
font-size: 16px;
}
.errors{
font-size: 12px;
font-style: italic;
color: #f34607;
}
52 changes: 34 additions & 18 deletions frontend/src/components/NewPetFormModal/NewPetFormModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
const [photo, setPhoto] = useState(initialPetData ? initialPetData.photo : null)
const [imageUpdated, setImageUpdated] = useState(false)
const [filePreview, setFilePreview] = useState(initialPetData?.imageUrl ?? '')
const [petErrors, setPetErrors] = useState({})
const dispatch = useDispatch();
const location = useLocation()
const {pathname} = location
Expand Down Expand Up @@ -49,8 +50,15 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
}
}
dispatch(updatePet(data, petId))
setEditModalState(null)
setImageUpdated(false)
.then( () => {
setEditModalState(null)
setImageUpdated(false)
})
.catch( async res => {
let errors = await res.json()
setPetErrors(errors.errors)
})

}

const handleCreateSubmit = (e) => {
Expand Down Expand Up @@ -80,22 +88,25 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
data.append('image', photo);
}

// if( /dashboard\/?$/.test(pathname)) {
dispatch(createPet(data))
setModalState(null)
// }

setName('')
setDob('')
setSex('')
setSpecies('')
setColor('')
setBreed('')
setMicrochipNum('')
setInsurancePolicyId('')
setWeight('')
setPhoto(null)
setFilePreview('')
.then( () => {
setModalState(null)
setName('')
setDob('')
setSex('')
setSpecies('')
setColor('')
setBreed('')
setMicrochipNum('')
setInsurancePolicyId('')
setWeight('')
setPhoto(null)
setFilePreview('')
})
.catch( async res => {
let errors = await res.json()
setPetErrors(errors.errors)
})
}


Expand All @@ -110,6 +121,7 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
<div className='name-input-label'>
<span>Name<span className="required">· required</span></span>
</div>
{petErrors.name && <span className="errors">{petErrors.name.message}</span> }
<input placeholder='Name'
type='text' value={name} onChange={e => setName(e.target.value)} />
</label>
Expand All @@ -118,6 +130,7 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
<div className='dob-input-label'>
<span>Date of Birth<span className="required">· required</span></span>
</div>
{petErrors.dob && <span className="errors">{petErrors.dob.message}</span> }
<input placeholder='Date of birth'
type='date' value={dob} onChange={e => setDob(e.target.value)} />
</label>
Expand All @@ -126,6 +139,7 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
<div className='sex-select-label'>
<span>Sex<span className="required">· required</span></span>
</div>
{petErrors.sex && <span className="errors">{petErrors.sex.message}</span> }
<select
className="sex-select"
placeholder='Sex'
Expand All @@ -138,12 +152,13 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
<option value="unknown" id="unknown">Unknown</option>

</optgroup>
</select>
</select>
</label>
<label className="input-label">
<div className='species-input-label'>
<span>Species<span className="required">· required</span></span>
</div>
{petErrors.species && <span className="errors">{petErrors.species.message}</span> }
<select className='species-select'
value={species}
onChange={e => setSpecies(e.target.value)}>
Expand All @@ -159,6 +174,7 @@ const NewPetForm = ({modalState, setModalState, editModalState, setEditModalStat
<div className='color-input-label'>
<span>Color<span className="required">· required</span></span>
</div>
{petErrors.color && <span className="errors">{petErrors.color.message}</span> }
<input placeholder='Color'
type='text' value={color} onChange={e => setColor(e.target.value)} />
</label>
Expand Down
60 changes: 39 additions & 21 deletions frontend/src/components/ReminderFormModal/ReminderFormModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,44 @@ const ReminderFormModal = ({modalState, setModalState, pet, reminder={}}) => {
pet: pet._id
}

modalState === 'edit' ?
dispatch(updateReminder(reminderInfo)) :
dispatch(createReminder(reminderInfo))
.then(() => {
setErrors({})
if (modalState === 'edit') {
dispatch(updateReminder(reminderInfo))
.then( () => {
setModalState(null)
setType('')
setTitle('')
setDue('')
setPerformDate('')
setDescription('')
setLocation('')
})
.catch(async res =>{
let data = await res.json()
setErrors(data.errors)
.catch( async (res) => {
let errors = await res.json()

setErrors(errors.errors)
})

}else {
dispatch(createReminder(reminderInfo))
.then( () => {
setModalState(null)
setType('')
setTitle('')
setDue('')
setPerformDate('')
setDescription('')
setLocation('')
})
.catch( async (res) => {
let errors = await res.json()
setErrors(errors.errors)
console.log(errors.errors)

if(Object.keys(errors).length !== 0) {
setModalState(null)
})
}
setType('')
setTitle('')
setDue('')
setPerformDate('')
setDescription('')
setLocation('')
}

useEffect(() => {
},[errors])


const reminderForm = () => (
<>
<label className="input-label">
Expand All @@ -139,10 +152,11 @@ const ReminderFormModal = ({modalState, setModalState, pet, reminder={}}) => {
<div className='title-select-label'>
<span>Title<span className="required">· required</span></span>
</div>
{errors.title && <div className='reminder-error'>* {errors.title.split(' ').slice(1).join(' ')}</div>}
{errors.title && <div className='reminder-error'>* {errors.title}</div>}
<select
className="title-select"
value={title}
onFocus={() => setErrors( old => ({...old, title:null}))}
onChange={e => setTitle(e.target.value)}>
<optgroup>
<option disabled value=""> {`Select ${modalState !== 'edit' && modalState}`} </option>
Expand All @@ -152,21 +166,25 @@ const ReminderFormModal = ({modalState, setModalState, pet, reminder={}}) => {
</optgroup>
</select>
</label>

<label className="input-label">
<div className='duedate-input-label'>
<span>Due Date<span className="required">· required</span></span>
</div>
{errors.dueDate && <div className='reminder-error'>* {errors.dueDate.split(' ').slice(1).join(' ')}</div>}
{errors.dueDate && <div className='reminder-error'>* {errors.dueDate}</div>}
<input placeholder='Due Date'
onFocus={() => setErrors( old => ({...old, dueDate:null}))}
type={ type === 'appointment' ? 'datetime-local' : 'date'} value={due} onChange={e => setDue(e.target.value)} />
</label>

<label className="input-label">
<div className='perform-date-input-label'>
<span>Perform Date</span>
</div>
<input placeholder='Perform Date'
type={ type === 'appointment' ? 'datetime-local' : 'date'} value={performDate} onChange={e => setPerformDate(e.target.value)} />
</label>

<label className='input-label'>
<div className='description-input-label'>
<span>Description</span>
Expand Down
8 changes: 2 additions & 6 deletions frontend/src/store/petReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export const createPet = petInfo => async dispatch => {

if (res.ok) {
return dispatch(receivePet(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
}else {
throw res
}
}
Expand All @@ -67,9 +65,7 @@ export const updatePet = (petInfo, petId) => async dispatch => {
const res = await putPet(petInfo, petId)
if (res.ok) {
return dispatch(receivePet(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
}else {
throw res
}
}
Expand Down
8 changes: 2 additions & 6 deletions frontend/src/store/reminderReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,15 @@ export const createReminder = (reminderInfo) => async dispatch => {

if (res.ok) {
return dispatch(receiveReminder(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
}else {
throw res
}
}
export const updateReminder = (reminderInfo) => async dispatch => {
const res = await editReminder(reminderInfo)
if (res.ok){
return dispatch(receiveReminder(await res.json()))
} else if (res.status === 422) {
return await res.json()
} else {
}else {
throw res
}
}
Expand Down

0 comments on commit 34584e1

Please sign in to comment.