Skip to content

Commit

Permalink
Feat: Replaced axios with fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
DhairyaMajmudar committed Jun 1, 2024
1 parent 73aec68 commit b1e68ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@docsearch/react": "3.5.2",
"axios": "1.6.0",
"classnames": "^2.3.1",
"feed": "^4.2.2",
"gray-matter": "^4.0.3",
Expand Down
13 changes: 9 additions & 4 deletions pages/community/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Card from '~/components/Card';
import Image from 'next/image';

/* eslint-disable */
import axios from 'axios';
import ical from 'node-ical';
import moment from 'moment-timezone';

Expand Down Expand Up @@ -45,8 +44,12 @@ export const getStaticProps: GetStaticProps = async () => {

async function fetchRemoteICalFile(url: string) {
try {
const response = await axios.get(url, { method: 'no-cors' });
return response.data;
const response = await fetch(url, { method: 'GET', mode: 'no-cors' });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text();
return data;
} catch (error) {
console.error('Error fetching iCal file:', error);
return null;
Expand All @@ -55,7 +58,9 @@ export const getStaticProps: GetStaticProps = async () => {
const remoteICalUrl =
'https://calendar.google.com/calendar/ical/c_8r4g9r3etmrmt83fm2gljbatos%40group.calendar.google.com/public/basic.ics';
const datesInfo = await fetchRemoteICalFile(remoteICalUrl)
.then((icalData) => printEventsForNextFourWeeks(ical.parseICS(icalData)))
.then((icalData: any) =>
printEventsForNextFourWeeks(ical.parseICS(icalData)),
)
.catch((error) => console.error('Error:', error));
return {
props: {
Expand Down
13 changes: 9 additions & 4 deletions pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Headline4 } from '~/components/Headlines';
import { GetStaticProps } from 'next';

/* eslint-disable */
import axios from 'axios';
import ical from 'node-ical';
import moment from 'moment-timezone';
import { useTheme } from 'next-themes';
Expand Down Expand Up @@ -49,8 +48,12 @@ export const getStaticProps: GetStaticProps = async () => {
// Function to fetch the remote iCal file
async function fetchRemoteICalFile(url: string) {
try {
const response = await axios.get(url, { method: 'no-cors' });
return response.data;
const response = await fetch(url, { method: 'GET', mode: 'no-cors' });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.text();
return data;
} catch (error) {
console.error('Error fetching iCal file:', error);
return null;
Expand All @@ -60,7 +63,9 @@ export const getStaticProps: GetStaticProps = async () => {
const remoteICalUrl =
'https://calendar.google.com/calendar/ical/info%40json-schema.org/public/basic.ics'; // Replace with the actual URL
const datesInfo = await fetchRemoteICalFile(remoteICalUrl)
.then((icalData) => printEventsForNextFourWeeks(ical.parseICS(icalData)))
.then((icalData: any) =>
printEventsForNextFourWeeks(ical.parseICS(icalData)),
)
.catch((error) => console.error('Error:', error));
// console.log('this is fetched data', datesInfo)
return {
Expand Down

0 comments on commit b1e68ad

Please sign in to comment.