-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
214 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Button, | ||
Checkbox, | ||
FormControl, | ||
FormControlLabel, | ||
InputLabel, | ||
MenuItem, | ||
Select, | ||
TextField, | ||
Box, | ||
Tooltip, | ||
} from '@mui/material'; | ||
|
||
|
||
function CheckInForm({ userId, checkInId, update }) { | ||
const [checkInTime, setCheckInTime] = useState(''); | ||
const [frequency, setFrequency] = useState('daily'); | ||
const [notify, setNotify] = useState(false); | ||
|
||
|
||
const handleSubmit = async (event) => { | ||
event.preventDefault(); | ||
const url = update ? `/api/checkIn/update/${checkInId}` : '/api/checkIn/schedule'; | ||
const method = update ? 'patch' : 'post'; | ||
const data = { user_id: userId, check_in_time: checkInTime, frequency, notify }; | ||
console.log('Submitting:', data); | ||
try { | ||
const response = await axios[method](url, data); | ||
console.log('Success:', response.data.message); | ||
// Optionally reset form or handle next steps | ||
} catch (error) { | ||
console.error('Error:', error.response?.data || error); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 4, padding: 3, borderRadius: 2, boxShadow: 3 }}> | ||
<TextField | ||
id="datetime-local" | ||
label="Check-in Time" | ||
type="datetime-local" | ||
fullWidth | ||
value={checkInTime} | ||
onChange={e => setCheckInTime(e.target.value)} | ||
sx={{ marginBottom: 3 }} | ||
InputLabelProps={{ | ||
shrink: true, | ||
}} | ||
required | ||
helperText="Select the date and time for your check-in." | ||
/> | ||
<FormControl fullWidth sx={{ marginBottom: 3 }}> | ||
<InputLabel id="frequency-label">Frequency</InputLabel> | ||
<Select | ||
labelId="frequency-label" | ||
id="frequency" | ||
value={frequency} | ||
label="Frequency" | ||
onChange={e => setFrequency(e.target.value)} | ||
> | ||
<MenuItem value="daily">Daily</MenuItem> | ||
<MenuItem value="weekly">Weekly</MenuItem> | ||
<MenuItem value="monthly">Monthly</MenuItem> | ||
</Select> | ||
<Tooltip title="Choose how often you want the check-ins to occur"> | ||
<i className="fas fa-info-circle" /> | ||
</Tooltip> | ||
</FormControl> | ||
<FormControlLabel | ||
control={<Checkbox checked={notify} onChange={e => setNotify(e.target.checked)} color="primary" />} | ||
label="Notify me" | ||
sx={{ marginBottom: 2 }} | ||
/> | ||
<Button type="submit" fullWidth variant="contained" color="primary" sx={{ mt: 2, mb: 2, padding: '10px 0' }}> | ||
{update ? 'Update Check-In' : 'Schedule Check-In'} | ||
</Button> | ||
</Box> | ||
); | ||
} | ||
|
||
CheckInForm.propTypes = { | ||
userId: PropTypes.string.isRequired, | ||
checkInId: PropTypes.string, | ||
update: PropTypes.bool.isRequired, | ||
}; | ||
|
||
export default CheckInForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useState, useEffect} from 'react'; | ||
import axios from 'axios'; | ||
import { useParams } from 'react-router-dom'; | ||
import { List, ListItem, ListItemText, Paper, Typography } from '@mui/material'; | ||
|
||
function CheckInsList() { | ||
const { userId } = useParams(); // Assuming 'user' has 'userId' | ||
const [checkIns, setCheckIns] = useState([]); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(''); | ||
|
||
useEffect(() => { | ||
const fetchCheckIns = async () => { | ||
if (!userId) { | ||
setError('User not logged in'); | ||
return; | ||
} | ||
setLoading(true); | ||
try { | ||
const response = await axios.get(`/api/checkIn/retrieveAll?user_id=${userId}`); | ||
console.log("API Response:", response.data); // Confirm what you receive | ||
|
||
// Validate if data is an array and has the correct structure | ||
if (Array.isArray(response.data) && response.data.every(item => item._id && item._id.$oid && item.check_in_time && item.check_in_time.$date)) { | ||
const formattedCheckIns = response.data.map(checkIn => ({ | ||
...checkIn, | ||
_id: checkIn._id.$oid, | ||
check_in_time: new Date(checkIn.check_in_time.$date).toLocaleString(), // Convert date from MongoDB format to a readable string | ||
})); | ||
setCheckIns(formattedCheckIns); | ||
} else { | ||
console.error('Data received is not in expected array format:', response.data); | ||
setError('Unexpected data format'); | ||
} | ||
setLoading(false); | ||
} catch (err) { | ||
console.error("Error during fetch:", err); | ||
setError(err.message); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchCheckIns(); | ||
}, [userId]); | ||
|
||
|
||
if (!userId) return <Typography variant="h6">Please log in to see your check-ins.</Typography>; | ||
if (loading) return <Typography variant="h6">Loading...</Typography>; | ||
if (error) return <Typography variant="h6">Error: {error}</Typography>; | ||
|
||
return ( | ||
<Paper style={{ margin: 16, padding: 16 }}> | ||
<Typography variant="h5" style={{ marginBottom: 16 }}>Your Check-Ins</Typography> | ||
<List> | ||
{checkIns.length > 0 ? checkIns.map(checkIn => ( | ||
<ListItem key={checkIn._id} divider> | ||
<ListItemText | ||
primary={`Check-In Time: ${checkIn.check_in_time}`} | ||
secondary={`Frequency: ${checkIn.frequency}`} | ||
/> | ||
</ListItem> | ||
)) : <ListItem><ListItemText primary="No check-ins found" /></ListItem>} | ||
</List> | ||
</Paper> | ||
); | ||
} | ||
|
||
export default CheckInsList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters