diff --git a/src/components/SimpleDashboard.tsx b/src/components/SimpleDashboard.tsx index 86d0ac4..18d61ad 100644 --- a/src/components/SimpleDashboard.tsx +++ b/src/components/SimpleDashboard.tsx @@ -24,6 +24,9 @@ const SimpleDashboard = () => { const [searchBetaModalQuery, setSearchBetaModalQuery] = useState(''); const [logEntries, setLogEntries] = useState([]); const [isSaving, setIsSaving] = useState(false); + const [timeMachine, setTimeMachine] = useState('week'); + const [randomTimeMachineEntry, setRandomTimeMachineEntry] = + useState(null); // const [inboxEntries, setInboxEntries] = useState([]); const { user, isLoaded } = useUser(); @@ -97,6 +100,101 @@ const SimpleDashboard = () => { return entry; }; + // for time machine, fetch entries from the past week, month, or year depending on the timeMachine state and select a random entry from the fetched entries + /* + + const fetchRecords = useCallback( + async (date: Date) => { + // convert date to form 2024-01-01 + const year = date.getFullYear(); + const month = date.getMonth() + 1; + let monthString = month.toString(); + // put a 0 in front of month if it is less than 10 + if (month < 10) { + monthString = `0${month}`; + } + const day = date.getDate(); + let dayString = day.toString(); + if (day < 10) { + dayString = `0${day}`; + } + const dateString = `${year}-${monthString}-${dayString}`; + setSelectedDay(dateString); + setLoading(true); + + // run a post request to fetch records for that date at /api/daily + const response = await fetch('/api/daily', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ date: dateString }), + }); + const responseData = await response.json(); + + // console.log('Fetched records:', responseData); + // set entries to the mapped data + setEntries(responseData.data); + setDateSelected(date); + setLoading(false); + }, + [setSelectedDay, setLoading, setEntries], + ); + */ + const fetchTimeMachineEntries = async () => { + try { + const date = new Date(); + switch (timeMachine) { + case 'week': + date.setDate(date.getDate() - 7); + break; + case 'month': + date.setMonth(date.getMonth() - 1); + break; + case 'year': + date.setFullYear(date.getFullYear() - 1); + break; + default: + break; + } + // use daily endpoint to fetch entries from the past week, month, or year + + // convert date to form 2024-01-01 + const year = date.getFullYear(); + const month = date.getMonth() + 1; + let monthString = month.toString(); + // put a 0 in front of month if it is less than 10 + if (month < 10) { + monthString = `0${month}`; + } + const day = date.getDate(); + let dayString = day.toString(); + if (day < 10) { + dayString = `0${day}`; + } + const dateString = `${year}-${monthString}-${dayString}`; + + const response = await fetch('/api/daily', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ date: dateString }), + }); + const responseData = await response.json(); + console.log('Time Machine entries:', responseData); + // math random number between 0 and the length of the responseData.data array + const randomIndex = Math.floor(Math.random() * responseData.data.length); + setRandomTimeMachineEntry(responseData.data[randomIndex]); + } catch (error) { + console.error('Error fetching time machine entries:', error); + } + }; + + useEffect(() => { + fetchTimeMachineEntries(); + }, [timeMachine]); + // const fetchInboxEntries = async () => { // try { // setIsLoading(true); @@ -149,6 +247,7 @@ const SimpleDashboard = () => { await handleRandom(); }; fetchEntry(); + fetchTimeMachineEntries(); }, []); // get log entries @@ -215,7 +314,12 @@ const SimpleDashboard = () => { , {firstLastName.firstName}!
-

{randomEntry ? randomEntry.data : 'Loading...'}

+ + {randomEntry ? randomEntry.data : 'Loading...'} + {randomEntry && randomEntry.metadata.author && ( <> { /> {randomEntry && ( <> - + */}