Skip to content

Commit

Permalink
feat(notice-board): use axios instead of unnecesary apiFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
wri7tno committed Oct 12, 2023
1 parent 344036c commit 6978bb8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pages/dashboards/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const getServerSideProps = async ({ params, query, req }) => {
}

let countryData = await getCategorisedCountries(true);
const notifications = await getPublishedNotifications({});
const notifications = await getPublishedNotifications();

if (!type || type === 'global') {
// get global data
Expand Down
2 changes: 1 addition & 1 deletion pages/grants-and-fellowships/[section].js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getServerSideProps = async ({ query }) => {
};
}

const notifications = await getPublishedNotifications({});
const notifications = await getPublishedNotifications();

if (query?.section === 'projects') {
const pageTexts = await getSGFPage();
Expand Down
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const HomePage = (props) => (

export const getStaticProps = async () => {
const newsArticles = await getNewsArticles();
const notifications = await getPublishedNotifications({});
const notifications = await getPublishedNotifications();

return {
props: {
Expand Down
2 changes: 1 addition & 1 deletion pages/map/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ALLOWED_TYPES = ['global', 'country', 'wdpa', 'use', 'geostore', 'aoi'];
export const getServerSideProps = async ({ req, params }) => {
const [type] = params?.location || [];
let userToken = null;
const notifications = await getPublishedNotifications({});
const notifications = await getPublishedNotifications();

try {
userToken = parse(req.headers.cookie)['gfw-token'];
Expand Down
2 changes: 1 addition & 1 deletion pages/my-gfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MyGfwPage = (props) => (
);

export const getStaticProps = async () => {
const notifications = await getPublishedNotifications({});
const notifications = await getPublishedNotifications();

return {
props: {
Expand Down
12 changes: 9 additions & 3 deletions services/__tests__/notifications.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NOTIFICATIONS_RESPONSE } from '../../fixtures/notifications';
// eslint-disable-next-line no-undef
jest.mock('axios');

describe.skip('getPublishedNotifications', () => {
describe('getPublishedNotifications', () => {
describe('when API call is successful', () => {
it('should return a list of notifications', async () => {
const response = NOTIFICATIONS_RESPONSE;
Expand Down Expand Up @@ -34,8 +34,11 @@ describe.skip('getPublishedNotifications', () => {

axios.get.mockResolvedValueOnce(response);

const result = await getPublishedNotifications({});
const result = await getPublishedNotifications();

expect(axios.get).toHaveBeenCalledWith(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp/v2/notice?per_page=100&page=1&orderby=date&order=desc`
);
expect(result).toEqual(expectedResult);
});
});
Expand All @@ -46,8 +49,11 @@ describe.skip('getPublishedNotifications', () => {

axios.get.mockRejectedValueOnce(new Error(message));

const result = await getPublishedNotifications({});
const result = await getPublishedNotifications();

expect(axios.get).toHaveBeenCalledWith(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp/v2/notice?per_page=100&page=1&orderby=date&order=desc`
);
expect(result).toBe(null);
});
});
Expand Down
26 changes: 4 additions & 22 deletions services/notifications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import apiFetch from '@wordpress/api-fetch';
import axios from 'axios';

const mapResponseToNotification = ({
Expand All @@ -20,28 +19,11 @@ const mapResponseToNotification = ({
};
};

apiFetch.setFetchHandler(async (options) => {
const headers = { 'Content-Type': 'application/json' };
const { url, path, data, method, params } = options;

return axios({
headers,
url: url || path,
method,
data,
params,
});
});

export const getPublishedNotifications = async ({ cancelToken }) => {
export const getPublishedNotifications = async () => {
try {
const notificationsData = await apiFetch({
url: `${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp/v2/notice?per_page=100&page=1&orderby=date&order=desc`,
params: {
_embed: true,
},
cancelToken,
});
const notificationsData = await axios.get(
`${process.env.NEXT_PUBLIC_WORDPRESS_URL}/wp/v2/notice?per_page=100&page=1&orderby=date&order=desc`
);

return notificationsData?.data.map((item) =>
mapResponseToNotification(item)
Expand Down

0 comments on commit 6978bb8

Please sign in to comment.