-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
239 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.vscode | ||
|
||
# testing | ||
/coverage | ||
|
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 |
---|---|---|
@@ -1,9 +1,89 @@ | ||
import React from 'react'; | ||
import React, { useState, useMemo, useEffect } from 'react'; | ||
import { MdChevronLeft, MdChevronRight } from 'react-icons/md'; | ||
import { | ||
format, | ||
subDays, | ||
addDays, | ||
setHours, | ||
setMinutes, | ||
setSeconds, | ||
isBefore, | ||
isEqual, | ||
parseISO, | ||
} from 'date-fns'; | ||
import { utcToZonedTime } from 'date-fns-tz'; | ||
import pt from 'date-fns/locale/pt-BR'; | ||
import { Container, Time } from './styles'; | ||
import api from '~/services/api'; | ||
|
||
// import { Container } from './styles'; | ||
const range = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; | ||
|
||
export default function Dashboard() { | ||
api.get('appointments'); | ||
return <h1>Dashboard</h1>; | ||
const [schedule, setSchedule] = useState([]); | ||
const [date, setDate] = useState(new Date()); | ||
|
||
const dateFormatted = useMemo( | ||
() => format(date, "d 'de' MMMM", { locale: pt }), | ||
[date] | ||
); | ||
|
||
useEffect(() => { | ||
async function loadSchedule() { | ||
const response = await api.get('schedule', { | ||
params: { date }, | ||
}); | ||
|
||
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
||
const data = range.map(hour => { | ||
const checkDate = setSeconds(setMinutes(setHours(date, hour), 0), 0); | ||
const compareDate = utcToZonedTime(checkDate, timezone); | ||
|
||
return { | ||
time: `${hour}:00h`, | ||
past: isBefore(compareDate, new Date()), | ||
appointment: response.data.find(a => | ||
isEqual(parseISO(a.date), compareDate) | ||
), | ||
}; | ||
}); | ||
|
||
setSchedule(data); | ||
} | ||
|
||
loadSchedule(); | ||
}, [date]); | ||
|
||
function handlePrevDay() { | ||
setDate(subDays(date, 1)); | ||
} | ||
|
||
function handleNextDay() { | ||
setDate(addDays(date, 1)); | ||
} | ||
|
||
return ( | ||
<Container> | ||
<header> | ||
<button type="button" onClick={handlePrevDay}> | ||
<MdChevronLeft size={36} color="#fff" /> | ||
</button> | ||
<strong>{dateFormatted}</strong> | ||
<button type="button" onClick={handleNextDay}> | ||
<MdChevronRight size={36} color="#fff" /> | ||
</button> | ||
</header> | ||
|
||
<ul> | ||
{schedule.map(time => ( | ||
<Time key={time.time} past={time.past} available={!time.appointment}> | ||
<strong>{time.time}</strong> | ||
<span> | ||
{time.appointment ? time.appointment.user.name : 'Em aberto'} | ||
</span> | ||
</Time> | ||
))} | ||
</ul> | ||
</Container> | ||
); | ||
} |
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,92 @@ | ||
import React, { useState, useMemo, useEffect } from 'react'; | ||
import { | ||
format, | ||
subDays, | ||
addDays, | ||
setHours, | ||
setMinutes, | ||
setSeconds, | ||
isBefore, | ||
isEqual, | ||
parseISO, | ||
} from 'date-fns'; | ||
import pt from 'date-fns/locale/pt'; | ||
import { utcToZonedTime } from 'date-fns-tz'; | ||
import { MdChevronLeft, MdChevronRight } from 'react-icons/md'; | ||
import api from '~/services/api'; | ||
|
||
import { Container, Time } from './styles'; | ||
|
||
const range = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; | ||
|
||
export default function Dashboard() { | ||
const [schedule, setSchedule] = useState([]); | ||
const [date, setDate] = useState(new Date()); | ||
|
||
const dateFormatted = useMemo( | ||
() => format(date, "d 'de' MMMM", { locale: pt }), | ||
[date] | ||
); | ||
|
||
useEffect(() => { | ||
async function loadSchedule() { | ||
const response = await api.get('schedules', { | ||
params: { date }, | ||
}); | ||
|
||
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
|
||
const data = range.map(hour => { | ||
const checkDate = setSeconds(setMinutes(setHours(date, hour), 0), 0); | ||
const compareDate = utcToZonedTime(checkDate, timezone); | ||
|
||
return { | ||
time: `${hour}:00h`, | ||
past: isBefore(compareDate, new Date()), | ||
appointment: response.data.find(a => | ||
isEqual(setSeconds(setMinutes(parseISO(a.date), 0), 0), compareDate) | ||
), | ||
}; | ||
}); | ||
|
||
console.log(data); | ||
|
||
setSchedule(data); | ||
} | ||
|
||
loadSchedule(); | ||
}, [date]); | ||
|
||
function handlePrevDay() { | ||
setDate(subDays(date, 1)); | ||
} | ||
|
||
function handleNextDay() { | ||
setDate(addDays(date, 1)); | ||
} | ||
|
||
return ( | ||
<Container> | ||
<header> | ||
<button type="button" onClick={handlePrevDay}> | ||
<MdChevronLeft size={36} color="#fff" /> | ||
</button> | ||
<strong>{dateFormatted}</strong> | ||
<button type="button" onClick={handleNextDay}> | ||
<MdChevronRight size={36} color="#fff" /> | ||
</button> | ||
</header> | ||
|
||
<ul> | ||
{schedule.map(time => ( | ||
<Time key={time.time} past={time.past} available={!time.appointment}> | ||
<strong>{time.time}</strong> | ||
<span> | ||
{time.appointment ? time.appointment.user.name : 'Em aberto'} | ||
</span> | ||
</Time> | ||
))} | ||
</ul> | ||
</Container> | ||
); | ||
} |
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,54 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.div` | ||
max-width: 600px; | ||
margin: 50px auto; | ||
display: flex; | ||
flex-direction: column; | ||
header { | ||
display: flex; | ||
align-self: center; | ||
align-items: center; | ||
button { | ||
border: 0; | ||
background: none; | ||
} | ||
strong { | ||
color: #fff; | ||
font-size: 24px; | ||
margin: 0 15px; | ||
} | ||
} | ||
ul { | ||
display: grid; | ||
grid-template-columns: repeat(2, 1fr); | ||
grid-gap: 15px; | ||
margin-top: 30px; | ||
} | ||
`; | ||
|
||
export const Time = styled.li` | ||
padding: 20px; | ||
border-radius: 4px; | ||
background: #fff; | ||
opacity: ${props => (props.past ? 0.6 : 1)}; | ||
strong { | ||
display: block; | ||
color: ${props => (props.available ? '#999' : '#7159c1')}; | ||
font-size: 20px; | ||
font-weight: normal; | ||
} | ||
span { | ||
display: block; | ||
margin-top: 3px; | ||
color: ${props => (props.available ? '#666' : '#7159c1')}; | ||
} | ||
`; |
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