Skip to content

Commit

Permalink
Remove momentjs, slight fix to parsePubDate utility
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Oct 27, 2023
1 parent e0c9e48 commit 4b5ba35
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"lucene": "^2.1.1",
"match-sorter": "^6.3.1",
"memoize-one": "^6.0.0",
"moment": "^2.29.4",
"next": "13.0.1",
"nprogress": "^0.2.0",
"prop-types": "^15.8.1",
Expand Down Expand Up @@ -148,6 +147,7 @@
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-storybook": "^0.6.13",
"express-validator": "^6.11.1",
"husky": "^8.0.0",
"identity-obj-proxy": "^3.0.0",
"iron-session": "^6.3.1",
"lint-staged": "^11.1.2",
Expand All @@ -164,8 +164,7 @@
"typescript": "^4.9.5",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.31.4",
"webpack": "^5.76.0",
"husky": "^8.0.0"
"webpack": "^5.76.0"
},
"volta": {
"node": "18.15.0",
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/components/FeedbackForms/MissingRecord/RecordPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import {
AlertStatus,
Button,
Checkbox,
CheckboxGroup,
Flex,
FormControl,
FormErrorMessage,
FormLabel,
HStack,
Input,
Stack,
Textarea,
Text,
Textarea,
useDisclosure,
CheckboxGroup,
} from '@chakra-ui/react';
import { PreviewModal, SimpleLink } from '@components';
import { IResourceUrl, useGetResourceLinks } from '@lib';
Expand All @@ -29,13 +29,13 @@ import { getDiffSections, getDiffString, processFormValues } from './DiffUtil';
import { KeywordsField } from './KeywordsField';
import { PubDateField } from './PubDateField';
import { ReferencesField } from './ReferencesField';
import { IAuthor, FormValues, IReference, DiffSection, IKeyword } from './types';
import { DiffSection, FormValues, IAuthor, IKeyword, IReference } from './types';
import { UrlsField } from './UrlsField';
import moment from 'moment';
import { DiffSectionPanel } from './DiffSectionPanel';
import { AxiosError } from 'axios';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { parsePublicationDate } from '@utils';

const collections: { value: Database; label: string }[] = [
{ value: 'astronomy', label: 'Astronomy and Astrophysics' },
Expand All @@ -45,6 +45,8 @@ const collections: { value: Database; label: string }[] = [

type State = 'idle' | 'loading-record' | 'loading-urls' | 'submitting' | 'preview';

const isInvalidPubDate = (pubdate: string) => parsePublicationDate(pubdate) === null;

const validationSchema = z
.object({
name: z.string().min(1, 'Name is required'),
Expand All @@ -71,7 +73,7 @@ const validationSchema = z
});
}

if (!moment(schema.pubDate, ['YYYY-MM', 'YYYY-MM-DD', 'YYYY-00', 'YYYY-00-00', 'YYYY-MM-00'], true).isValid()) {
if (isInvalidPubDate(schema.pubDate)) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ['pubDate'],
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ export const parsePublicationDate = (pubdate: string) => {
return null;
}

const regex = /^(?<year>\d{4})-(?<month>\d{2}|\d{1}|00)-(?<day>\d{2}|\d{1}|00)$/;
const match = pubdate.match(regex);
const regex = /^(\d{4})-(\d{2}|\d|00)-(\d{2}|\d|00)$/;
const match = RegExp(regex).exec(pubdate);

// if bad match, at least grab the year which should always be first 4 characters
return match
? (match.groups as { year: string; month: string; day: string })
return match && match.length === 4
? { year: match[1], month: match[2], day: match[3] }
: { year: pubdate.slice(0, 4), month: '00', day: '00' };
};

Expand Down

0 comments on commit 4b5ba35

Please sign in to comment.