Skip to content

Commit

Permalink
refactor: detailsContent
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayushsanjdev committed Oct 1, 2024
1 parent 89e1d4c commit fa6d4e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/components/taskDetails/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@/interfaces/taskDetails.type';
import useUserData from '@/hooks/useUserData';
import { STARTED_ON, ENDS_ON } from '@/constants/constants';
import { isValidDate } from '@/utils/isValidDate';

type StringNumberOrUndefined = string | number | undefined;

Expand All @@ -28,12 +29,12 @@ const DetailsContent: FC<DetailsContentProps> = ({
}) => {
const displayValue = renderedValue || value;

return (
<span
className={styles.detailValue}
style={{ color: color ?? 'black' }}
>
{isGitHubLink && value && gitHubIssueLink ? (
if (isGitHubLink && value && gitHubIssueLink) {
return (
<span
className={styles.detailValue}
style={{ color: color ?? 'black' }}
>
<a
className={styles.gitLink}
href={gitHubIssueLink}
Expand All @@ -44,7 +45,16 @@ const DetailsContent: FC<DetailsContentProps> = ({
>
{extractRepoName(value)}
</a>
) : isTimeDetail && value ? (
</span>
);
}

if (isTimeDetail && value) {
return (
<span
className={styles.detailValue}
style={{ color: color ?? 'black' }}
>
<Tooltip
content={formatDate(value)}
tooltipPosition={{
Expand All @@ -54,9 +64,16 @@ const DetailsContent: FC<DetailsContentProps> = ({
>
{tooltipActive ? formatDate(value) : getRelativeTime(value)}
</Tooltip>
) : (
displayValue
)}
</span>
);
}

return (
<span
className={styles.detailValue}
style={{ color: color ?? 'black' }}
>
{displayValue}
</span>
);
};
Expand All @@ -73,15 +90,7 @@ const Details: FC<TaskDetailsProps> = (props) => {
if (!isEditing) setNewEndOnDate('');
}, [isEditing]);

const isValidDate = (dateString: string) => {
// Validating the "YYYY-MM-DD" format strictly
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
return (
dateRegex.test(dateString) && !isNaN(new Date(dateString).getTime())
);
};

const handleBlurOfEndsOn = () => {
const handleEndsOnBlur = () => {
const isDateValid = isValidDate(newEndOnDate);
const endsOn = isDateValid
? new Date(`${newEndOnDate}`).getTime() / 1000
Expand Down Expand Up @@ -155,7 +164,7 @@ const Details: FC<TaskDetailsProps> = (props) => {
type="date"
name="endsOn"
onChange={(e) => setNewEndOnDate(e.target.value)}
onBlur={handleBlurOfEndsOn}
onBlur={handleEndsOnBlur}
value={
newEndOnDate ||
new Date(value as string).toLocaleDateString('en-CA')
Expand Down
5 changes: 5 additions & 0 deletions src/utils/isValidDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const isValidDate = (dateString: string) => {
// Validating the "YYYY-MM-DD" format strictly
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
return dateRegex.test(dateString) && !isNaN(new Date(dateString).getTime());
};

0 comments on commit fa6d4e8

Please sign in to comment.