Skip to content

Commit

Permalink
#3024: Remove unnecessary useEffect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cemalettin-work committed Dec 22, 2020
1 parent 70e36e4 commit 5171ff0
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions client/src/pages/reports/EngagementDateFormPartial.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FastField, Field } from "formik"
import { Report } from "models"
import moment from "moment"
import PropTypes from "prop-types"
import React, { useEffect, useState } from "react"
import React, { useState } from "react"
import { Col, ControlLabel, FormGroup, HelpBlock } from "react-bootstrap"
import Settings from "settings"
import utils from "utils"
Expand All @@ -28,18 +28,9 @@ const EngagementDatePartialFormWithDuration = ({
edit,
values
}) => {
const [isAllDay, setIsAllDay] = useState(true)
useEffect(() => {
if (!edit || !initialValues.engagementDate) {
setIsAllDay(true)
} else if (!isStartOfDay(initialValues.engagementDate)) {
setIsAllDay(false)
} else {
setIsAllDay(initialValues.duration === null)
}
// this logic should run only once when the component is mounted
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const [isAllDay, setIsAllDay] = useState(() =>
getInitalAllDayState(edit, initialValues)
)

return (
<FormGroup>
Expand Down Expand Up @@ -117,6 +108,16 @@ const EngagementDatePartialFormWithDuration = ({
)
}

function getInitalAllDayState(edit, initialValues) {
if (!edit || !initialValues.engagementDate) {
return true
} else if (!isStartOfDay(initialValues.engagementDate)) {
return false
} else {
return initialValues.duration === null
}
}

EngagementDatePartialFormWithDuration.propTypes = {
setFieldValue: PropTypes.func.isRequired,
setFieldTouched: PropTypes.func.isRequired,
Expand Down

0 comments on commit 5171ff0

Please sign in to comment.