Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date formatting #132

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ interface AnswerTableRowProps {

function AnswerTableRow({ time, selectedIcon, text }: AnswerTableRowProps) {
return (
<div className="my-2 mx-auto grid max-w-[800px] grid-cols-6 rounded-md bg-white p-3 shadow-sm">
<div className="col-span-2 hidden xsm:block">{time}</div>
<div className="my-2 mx-auto grid max-w-[800px] grid-cols-8 rounded-md bg-white p-3 shadow-sm">
<div className="col-span-3 hidden text-sm xsm:block">{time}</div>
<div className="col-span-2 mt-[1px] ml-[4px] mr-2 flex justify-center xsm:col-span-1 xsm:justify-start">
<Emoji unified={selectedIcon} size={22} />
</div>
<div className="col-span-4 break-words text-left xsm:col-span-3">
<div className="col-span-6 break-words text-left xsm:col-span-4">
{text || '-'}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/features/surveys/components/DataCard/DataCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface DataCardProps {

export default function DataCard({ title, value }: DataCardProps) {
return (
<div className="m-1 w-full rounded-lg border border-zinc-200 bg-white py-2 px-6 shadow-md sm:w-1/2 md:w-[200px]">
<div className="m-1 w-full rounded-lg border border-zinc-200 bg-white py-2 px-4 shadow-md sm:w-1/2 md:w-[220px]">
<h3 className="mb-1 font-semibold">{title}</h3>
{value}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/features/surveys/components/SurveyRow/SurveyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function SurveyRow({
<div title={question} className="w-36 truncate text-left">
{question}
</div>
<div className="flex items-center space-x-2">
<div className="hidden items-center space-x-2 text-sm xsm:flex">
<div>{createDate}</div>
</div>
</div>
Expand Down
9 changes: 5 additions & 4 deletions src/features/surveys/managers/surveyResultsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { useRouter } from 'next/router';
import toast from 'react-hot-toast';
import { BarChartData } from 'features/surveys/components/BarChart/BarChart';
import { AnswerData } from 'features/surveys/interfaces/AnswerData';
import { formatFirebaseDateWithHours } from 'shared/utilities/convertTime';
import {
formatDateDistance,
formatFirebaseDateWithHours,
} from 'shared/utilities/convertTime';
import useCopyToClipboard from 'shared/hooks/useCopyToClipboard';
import { db } from 'firebaseConfiguration';

Expand Down Expand Up @@ -64,9 +67,7 @@ export const useSurveyResultsManager = () => {
const data = answersData.docs.map((doc) => ({
...doc.data(),
id: doc.id,
answerDate: formatFirebaseDateWithHours(
doc.data().answerDate as Timestamp
),
answerDate: formatDateDistance(doc.data().answerDate as Timestamp),
})) as AnswerData[];

if (displayMessages) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/surveys/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Timestamp } from 'firebase/firestore';

import Image from 'next/image';
import Head from 'next/head';
import { formatFirebaseDateWithoutHours } from 'shared/utilities/convertTime';
import { formatDateDistance } from 'shared/utilities/convertTime';
import withAnimation from 'shared/HOC/withAnimation';
import withProtectedRoute from 'shared/HOC/withProtectedRoute';
import Header from 'shared/components/Header/Header';
Expand Down Expand Up @@ -44,7 +44,7 @@ function SurveyListPage() {
key={doc.id}
id={doc.id}
question={survey.title}
createDate={formatFirebaseDateWithoutHours(
createDate={formatDateDistance(
survey.createDate as Timestamp
)}
></SurveyRow>
Expand Down
25 changes: 22 additions & 3 deletions src/shared/utilities/convertTime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { formatDistance } from 'date-fns';
import { Timestamp } from 'firebase/firestore';

const formatFirebaseDateWithHours = (date: Timestamp | undefined) => {
return (
date?.toDate().toLocaleString('pl-PL', {
date?.toDate().toLocaleString(undefined, {
year: 'numeric',
month: 'numeric',
day: 'numeric',
Expand All @@ -14,12 +15,30 @@ const formatFirebaseDateWithHours = (date: Timestamp | undefined) => {

const formatFirebaseDateWithoutHours = (date: Timestamp | undefined) => {
return (
date?.toDate().toLocaleString('pl-PL', {
date?.toDate().toLocaleString(undefined, {
year: 'numeric',
month: 'numeric',
day: 'numeric',
}) ?? ''
);
};

export { formatFirebaseDateWithHours, formatFirebaseDateWithoutHours };
const formatDateDistance = (date: Timestamp | undefined) => {
const dateObject = date?.toDate();

if (!dateObject) return '';

const distance = formatDistance(dateObject, new Date(), {
addSuffix: true,
includeSeconds: true,
});

const formattedDistance = distance.replace(/^less than /, '');
return formattedDistance;
};

export {
formatFirebaseDateWithHours,
formatFirebaseDateWithoutHours,
formatDateDistance,
};