Skip to content

Commit

Permalink
refactor: remove unused use effects
Browse files Browse the repository at this point in the history
  • Loading branch information
Manumartin95 committed Oct 4, 2023
1 parent f483bf4 commit 1aeb161
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ export const ActivityForm: FC<ActivityFormProps> = (props) => {
// TODO: Remove once there is a dedicated TimeInfo API
isRecentRole={showRecentRole}
timeInfo={role.timeInfo}
projectId={project?.id}
roleId={role.id}
userId={activity?.userId}
useDecimalTimeFormat={settings?.useDecimalTimeFormat}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { DateInterval } from '../../../../../../../../shared/types/date-interval
import { GetDaysForActivityDaysPeriodQry } from '../../../../application/get-days-for-activity-days-period-qry'
import { GetDaysForActivityNaturalDaysPeriodQry } from '../../../../application/get-days-for-activity-natural-days-period-qry'
import { Id } from '../../../../../../../../shared/types/id'
import { GetProjectRolesQry } from '../../../../../project-role/application/get-project-roles-qry'
import { TimeInfo } from '../../../../../project-role/domain/project-role-time-info'

interface Props {
// TODO: Remove once there is a dedicated TimeInfo API
timeInfo: TimeInfo
isRecentRole: boolean
projectId?: Id
userId?: Id
// TODO: Until here
roleId?: Id
Expand All @@ -29,39 +27,6 @@ export const DurationText: FC<Props> = (props) => {
const { start, end, useDecimalTimeFormat } = props
const { t } = useTranslation()
const [numberOfDays, setNumberOfDays] = useState<null | number>(null)
const [timeInfo, setTimeInfo] = useState<TimeInfo>({
timeUnit: TimeUnits.MINUTES,
maxTimeAllowed: {
byActivity: 0,
byYear: 0
},
userRemainingTime: 0
})

// TODO: Remove once there is a dedicated TimeInfo API
useEffect(() => {
if (props.isRecentRole) {
setTimeInfo(props.timeInfo)
}
}, [props.isRecentRole, props.timeInfo])

const { executeUseCase, isLoading } = useGetUseCase(GetProjectRolesQry)

// TODO: Remove once there is a dedicated TimeInfo API
useEffect(() => {
if (props.projectId !== undefined && props.roleId !== undefined) {
executeUseCase({
projectId: props.projectId,
userId: props.userId,
year: start.getFullYear()
}).then((x) => {
const find = x.find((role) => role.id === props.roleId)
if (find !== undefined) {
setTimeInfo(find.timeInfo)
}
})
}
}, [executeUseCase, props.projectId, props.roleId, props.userId, start])

const { isLoading: daysLoading, executeUseCase: getDaysForActivityDaysPeriodQry } = useGetUseCase(
GetDaysForActivityDaysPeriodQry
Expand All @@ -72,38 +37,38 @@ export const DurationText: FC<Props> = (props) => {

const formatTimePerTimeUnit = useCallback(
(timeToFormat: number) => {
return timeInfo.timeUnit === TimeUnits.MINUTES
return props.timeInfo.timeUnit === TimeUnits.MINUTES
? getDurationByMinutes(timeToFormat)
: getHumanizedDuration({
duration: timeToFormat,
abbreviation: true,
timeUnit: timeInfo.timeUnit
timeUnit: props.timeInfo.timeUnit
})
},
[timeInfo.timeUnit]
[props.timeInfo.timeUnit]
)

const duration = useMemo(() => {
const diffUnit = timeInfo.timeUnit === TimeUnits.MINUTES ? 'minute' : 'businessDay'
const diffUnit = props.timeInfo.timeUnit === TimeUnits.MINUTES ? 'minute' : 'businessDay'
const difference = chrono(start).diff(end, diffUnit)

return timeInfo.timeUnit === TimeUnits.MINUTES
return props.timeInfo.timeUnit === TimeUnits.MINUTES
? getDurationByMinutes(difference, useDecimalTimeFormat)
: getHumanizedDuration({
duration: numberOfDays || 0,
abbreviation: true,
timeUnit: timeInfo.timeUnit
timeUnit: props.timeInfo.timeUnit
})
}, [timeInfo.timeUnit, end, start, useDecimalTimeFormat, numberOfDays])
}, [props.timeInfo.timeUnit, end, start, useDecimalTimeFormat, numberOfDays])

useEffect(() => {
if (timeInfo.timeUnit === TimeUnits.MINUTES) {
if (props.timeInfo.timeUnit === TimeUnits.MINUTES) {
return
}
const dateInterval: DateInterval = { start, end }
if (timeInfo.timeUnit === TimeUnits.DAYS) {
if (props.timeInfo.timeUnit === TimeUnits.DAYS) {
getDaysForActivityDaysPeriodQry(dateInterval).then(setNumberOfDays)
} else if (timeInfo.timeUnit === TimeUnits.NATURAL_DAYS && props.roleId !== undefined) {
} else if (props.timeInfo.timeUnit === TimeUnits.NATURAL_DAYS && props.roleId !== undefined) {
getDaysForActivityNaturalDaysPeriodQry({ roleId: props.roleId, interval: dateInterval }).then(
setNumberOfDays
)
Expand All @@ -112,51 +77,47 @@ export const DurationText: FC<Props> = (props) => {
start,
end,
getDaysForActivityDaysPeriodQry,
timeInfo.timeUnit,
props.timeInfo.timeUnit,
props.roleId,
getDaysForActivityNaturalDaysPeriodQry
])

return (
<>
{!isLoading && (
<>
<Flex justify="space-between" w="100%" h="100%" mt="10px">
<span>{t('activity_form.duration')}</span>
{daysLoading || naturalDaysLoading ? (
<span>{t('accessibility.loading')}</span>
) : (
<span>{duration}</span>
)}
</Flex>
<Flex justify="space-between" w="100%" h="100%" mt="10px">
<span>{t('activity_form.duration')}</span>
{daysLoading || naturalDaysLoading ? (
<span>{t('accessibility.loading')}</span>
) : (
<span>{duration}</span>
)}
</Flex>

<Flex
w="100%"
position="absolute"
right="0"
top="38px"
alignItems="start"
flexDirection="column"
>
{timeInfo.maxTimeAllowed.byYear > 0 && (
<Text display="block" fontSize="xs" color="gray.500">
{t('activity_form.remainingByYear', {
remaining: formatTimePerTimeUnit(timeInfo.userRemainingTime),
maxAllowed: formatTimePerTimeUnit(timeInfo.maxTimeAllowed.byYear)
})}
</Text>
)}
<Flex
w="100%"
position="absolute"
right="0"
top="38px"
alignItems="start"
flexDirection="column"
>
{props.timeInfo.maxTimeAllowed.byYear > 0 && (
<Text display="block" fontSize="xs" color="gray.500">
{t('activity_form.remainingByYear', {
remaining: formatTimePerTimeUnit(props.timeInfo.userRemainingTime),
maxAllowed: formatTimePerTimeUnit(props.timeInfo.maxTimeAllowed.byYear)
})}
</Text>
)}

{timeInfo.maxTimeAllowed.byActivity > 0 && (
<Text display="block" fontSize="xs" color="gray.500">
{t('activity_form.remainingByActivity', {
maxAllowed: formatTimePerTimeUnit(timeInfo.maxTimeAllowed.byActivity)
})}
</Text>
)}
</Flex>
</>
)}
{props.timeInfo.maxTimeAllowed.byActivity > 0 && (
<Text display="block" fontSize="xs" color="gray.500">
{t('activity_form.remainingByActivity', {
maxAllowed: formatTimePerTimeUnit(props.timeInfo.maxTimeAllowed.byActivity)
})}
</Text>
)}
</Flex>
</>
)
}

0 comments on commit 1aeb161

Please sign in to comment.