Skip to content

Commit

Permalink
fix(client): 🐛 attempt to fix the one-off date issue
Browse files Browse the repository at this point in the history
use utc functions from dayjs to correspond to the same date on the server
  • Loading branch information
AmruthPillai committed Jul 31, 2022
1 parent 1c19062 commit 5197f95
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 25 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"i18n-ally.namespace": true,
"i18n-ally.pathMatcher": "{locale}/{namespaces}.{ext}",
"i18n-ally.sortKeys": true,
"scss.validate": false
"scss.validate": false,
"conventionalCommits.scopes": [
"client"
]
}
2 changes: 1 addition & 1 deletion client/components/build/RightSidebar/sections/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Settings = () => {
const dateConfig: DateConfig = useMemo(() => get(resume, 'metadata.date'), [resume]);

const isDarkMode = useMemo(() => theme === 'dark', [theme]);
const exampleString = useMemo(() => `Eg. ${dayjs().format(dateConfig.format)}`, [dateConfig.format]);
const exampleString = useMemo(() => `Eg. ${dayjs().utc().format(dateConfig.format)}`, [dateConfig.format]);
const themeString = useMemo(() => (isDarkMode ? 'Matte Black Everything' : 'As bright as your future'), [isDarkMode]);

const { mutateAsync: loadSampleDataMutation } = useMutation<Resume, ServerError, LoadSampleDataParams>(
Expand Down
2 changes: 1 addition & 1 deletion client/components/shared/ResumeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const ResumeInput: React.FC<Props> = ({ type = 'text', label, path, className, m
renderInput={(params) => <TextField {...params} error={false} className={className} />}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && onChangeValue('');
date && dayjs(date).isValid() && onChangeValue(date.toISOString());
date && dayjs(date).utc().isValid() && onChangeValue(date.toISOString());
}}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion client/modals/builder/sections/AwardModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const AwardModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
2 changes: 1 addition & 1 deletion client/modals/builder/sections/CertificateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const CertificateModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
4 changes: 2 additions & 2 deletions client/modals/builder/sections/CustomModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const CustomModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand All @@ -174,7 +174,7 @@ const CustomModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
4 changes: 2 additions & 2 deletions client/modals/builder/sections/EducationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const EducationModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand All @@ -197,7 +197,7 @@ const EducationModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
4 changes: 2 additions & 2 deletions client/modals/builder/sections/ProjectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const ProjectModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand All @@ -167,7 +167,7 @@ const ProjectModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
2 changes: 1 addition & 1 deletion client/modals/builder/sections/PublicationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const PublicationModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
4 changes: 2 additions & 2 deletions client/modals/builder/sections/VolunteerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const VolunteerModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand All @@ -164,7 +164,7 @@ const VolunteerModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
4 changes: 2 additions & 2 deletions client/modals/builder/sections/WorkModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const WorkModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand All @@ -164,7 +164,7 @@ const WorkModal: React.FC = () => {
views={['year', 'month', 'day']}
onChange={(date: Date | null, keyboardInputValue: string | undefined) => {
isEmpty(keyboardInputValue) && field.onChange('');
date && dayjs(date).isValid() && field.onChange(date.toISOString());
date && dayjs(date).utc().isValid() && field.onChange(date.toISOString());
}}
renderInput={(params) => (
<TextField
Expand Down
6 changes: 3 additions & 3 deletions client/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const dateFormatOptions: string[] = [
'YYYY',
];

export const getRelativeTime = (timestamp: dayjs.ConfigType): string => dayjs(timestamp).toNow(true);
export const getRelativeTime = (timestamp: dayjs.ConfigType): string => dayjs(timestamp).utc().toNow(true);

export const formatDateString = (date: string | DateRange, formatStr: string): string | null => {
const presentString = i18n?.t<string>('common.date.present') ?? '';
Expand All @@ -28,9 +28,9 @@ export const formatDateString = (date: string | DateRange, formatStr: string): s

// If `date` is a string
if (isString(date)) {
if (!dayjs(date).isValid()) return null;
if (!dayjs(date).utc().utc().isValid()) return null;

return dayjs(date).format(formatStr);
return dayjs(date).utc().format(formatStr);
}

// If `date` is a DateRange
Expand Down
2 changes: 2 additions & 0 deletions client/wrappers/DateWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import utc from 'dayjs/plugin/utc';
import { useRouter } from 'next/router';
import { useEffect } from 'react';

const DateWrapper: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => {
const { locale } = useRouter();

useEffect(() => {
dayjs.extend(utc);
dayjs.extend(relativeTime);

// Locales
Expand Down
15 changes: 9 additions & 6 deletions server/src/integrations/integrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@reactive-resume/schema';
import csv from 'csvtojson';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { readFile, unlink } from 'fs/promises';
import { cloneDeep, get, isEmpty, merge } from 'lodash';
import StreamZip from 'node-stream-zip';
Expand All @@ -28,7 +29,9 @@ import { ResumeService } from '@/resume/resume.service';

@Injectable()
export class IntegrationsService {
constructor(private resumeService: ResumeService) {}
constructor(private resumeService: ResumeService) {
dayjs.extend(utc);
}

async linkedIn(userId: number, path: string): Promise<ResumeEntity> {
let archive: StreamZip.StreamZipAsync;
Expand All @@ -39,7 +42,7 @@ export class IntegrationsService {
const resume: Partial<Resume> = cloneDeep(defaultState);

// Basics
const timestamp = dayjs().format(FILENAME_TIMESTAMP);
const timestamp = dayjs().utc().format(FILENAME_TIMESTAMP);
merge<Partial<Resume>, DeepPartial<Resume>>(resume, {
name: `Imported from LinkedIn (${timestamp})`,
slug: `imported-from-linkedin-${timestamp}`,
Expand Down Expand Up @@ -269,7 +272,7 @@ export class IntegrationsService {
const resume: Partial<Resume> = cloneDeep(defaultState);

// Metadata
const timestamp = dayjs().format(FILENAME_TIMESTAMP);
const timestamp = dayjs().utc().format(FILENAME_TIMESTAMP);
merge<Partial<Resume>, DeepPartial<Resume>>(resume, {
name: `Imported from JSON Resume (${timestamp})`,
slug: `imported-from-json-resume-${timestamp}`,
Expand Down Expand Up @@ -604,7 +607,7 @@ export class IntegrationsService {
const resume: Partial<Resume> = cloneDeep(jsonResume);

// Metadata
const timestamp = dayjs().format(FILENAME_TIMESTAMP);
const timestamp = dayjs().utc().format(FILENAME_TIMESTAMP);
merge<Partial<Resume>, DeepPartial<Resume>>(resume, {
name: `Imported from Reactive Resume (${timestamp})`,
slug: `imported-from-reactive-resume-${timestamp}`,
Expand All @@ -625,7 +628,7 @@ export class IntegrationsService {
const resume: Partial<Resume> = cloneDeep(defaultState);

// Metadata
const timestamp = dayjs().format(FILENAME_TIMESTAMP);
const timestamp = dayjs().utc().format(FILENAME_TIMESTAMP);
merge<Partial<Resume>, DeepPartial<Resume>>(resume, {
name: `Imported from Reactive Resume V2 (${timestamp})`,
slug: `imported-from-reactive-resume-v2-${timestamp}`,
Expand Down Expand Up @@ -948,6 +951,6 @@ export class IntegrationsService {
}

private parseDate = (date: string): string => {
return isEmpty(date) ? '' : dayjs(date).toISOString();
return isEmpty(date) ? '' : dayjs(date).utc().toISOString();
};
}

0 comments on commit 5197f95

Please sign in to comment.