-
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.
feat: Adds functionality for the button
- Loading branch information
1 parent
b884eca
commit ae8b992
Showing
10 changed files
with
262 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
import { useState, useEffect } from 'react' | ||
import { CountdownContainer, Separator } from './styles' | ||
import { differenceInSeconds } from 'date-fns' | ||
|
||
export default function CountDown() { | ||
const [amountSecondsPassed, setAmountSecondsPassed] = useState<number>(0) | ||
const totalSeconds = activeCycle ? activeCycle.minutesAmount * 60 : 0 | ||
|
||
useEffect(() => { | ||
let interval: number | ||
|
||
if (activeCycle) { | ||
interval = setInterval(() => { | ||
const secondsDifference = differenceInSeconds( | ||
new Date(), | ||
activeCycle.startDate, | ||
) | ||
|
||
if (secondsDifference >= totalSeconds) { | ||
setCycles((state) => | ||
state.map((cycle) => { | ||
if (cycle.id === activeCycle.id) { | ||
const now = new Date() | ||
return { ...cycle, finishedDate: now } | ||
} else { | ||
return cycle | ||
} | ||
}), | ||
) | ||
setAmountSecondsPassed(totalSeconds) | ||
clearInterval(interval) | ||
} else { | ||
setAmountSecondsPassed(secondsDifference) | ||
} | ||
}, 1000) | ||
} | ||
|
||
return () => { | ||
clearInterval(interval) | ||
} | ||
}, [activeCycle, totalSeconds, activeCycleId]) | ||
|
||
return ( | ||
<CountdownContainer> | ||
<span>{minutes[0]}</span> | ||
<span>{minutes[1]}</span> | ||
<Separator>:</Separator> | ||
<span>{seconds[0]}</span> | ||
<span>{seconds[1]}</span> | ||
</CountdownContainer> | ||
) | ||
} |
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,24 @@ | ||
import { styled } from 'styled-components' | ||
|
||
export const CountdownContainer = styled.div` | ||
font-family: 'Roboto-Mono', monospace; | ||
font-size: 10rem; | ||
line-height: 8rem; | ||
color: ${(props) => props.theme['gray-100']}; | ||
display: flex; | ||
gap: 1rem; | ||
span { | ||
background: ${(props) => props.theme['gray-700']}; | ||
padding: 2rem 1rem; | ||
border-radius: 8px; | ||
} | ||
` | ||
|
||
export const Separator = styled.div` | ||
padding: 2rem 0; | ||
color: ${(props) => props.theme['green-500']}; | ||
width: 4rem; | ||
overflow: hidden; | ||
display: flex; | ||
justify-content: center; | ||
` |
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,54 @@ | ||
import { FormContainer, TaskInput, MinutesAmountInput } from './styles' | ||
import { useForm } from 'react-hook-form' | ||
import { ICycle } from '../../pages/Home/interfaces' | ||
import * as zod from 'zod' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { newCycleFormValidationSchema } from './formValidations' | ||
|
||
interface INewCyclesForm { | ||
activeCycle: ICycle | undefined | ||
} | ||
|
||
type formType = zod.infer<typeof newCycleFormValidationSchema> | ||
|
||
export default function NewCyclesForm({ activeCycle }: INewCyclesForm) { | ||
const { register, handleSubmit, formState, reset, watch } = useForm<formType>( | ||
{ | ||
resolver: zodResolver(newCycleFormValidationSchema), | ||
defaultValues: { | ||
minutesAmount: 0, | ||
task: '', | ||
}, | ||
}, | ||
) | ||
return ( | ||
<FormContainer> | ||
<label htmlFor="task">Going to work with</label> | ||
<TaskInput | ||
type="text" | ||
id="task" | ||
placeholder="Give a name for the task" | ||
list="task-suggestions" | ||
{...register('task')} | ||
disabled={!!activeCycle} | ||
/> | ||
<datalist id="task-suggestions"> | ||
<option value="Projeto 1" /> | ||
<option value="Projeto 2" /> | ||
<option value="Projeto 3" /> | ||
<option value="Projeto 4" /> | ||
</datalist> | ||
<label htmlFor="minutesAmount">for</label> | ||
<MinutesAmountInput | ||
type="number" | ||
id="minutesAmount" | ||
placeholder="00" | ||
step={1} | ||
required | ||
disabled={!!activeCycle} | ||
{...register('minutesAmount', { valueAsNumber: true })} | ||
/> | ||
<span>minutes</span> | ||
</FormContainer> | ||
) | ||
} |
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,44 @@ | ||
import { styled } from 'styled-components' | ||
|
||
export const FormContainer = styled.div` | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
gap: 0.5rem; | ||
color: ${(props) => props.theme['gray-100']}; | ||
font-size: 1.125rem; | ||
font-weight: bold; | ||
flex-wrap: wrap; | ||
` | ||
|
||
const BaseInput = styled.input` | ||
background: transparent; | ||
height: 2.5rem; | ||
border: 0; | ||
border-bottom: 2px solid ${(props) => props.theme['gray-500']}; | ||
font-weight: bold; | ||
font-size: 1.125rem; | ||
padding: 0 0.5%; | ||
color: ${(props) => props.theme['gray-100']}; | ||
&:focus { | ||
box-shadow: none; | ||
border-color: ${(props) => props.theme['green-500']}; | ||
} | ||
&::placeholder { | ||
color: ${(props) => props.theme['gray-500']}; | ||
} | ||
` | ||
|
||
export const TaskInput = styled(BaseInput)` | ||
flex: 1; | ||
&::-webkit-calendar-picker-indicator { | ||
display: none !important; | ||
} | ||
` | ||
|
||
export const MinutesAmountInput = styled(BaseInput)` | ||
width: 4rem; | ||
` |
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
Oops, something went wrong.