Skip to content

Commit

Permalink
Merge pull request #460 from CodeForAfrica/chore/charterafrica_api_re…
Browse files Browse the repository at this point in the history
…sources

@/charterafrica Move all documents API queries to /resources
  • Loading branch information
kilemensi committed Jun 26, 2023
2 parents b1331fe + f8ed7ba commit a59930f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function useDocuments(q, options, pathname) {
const qs = queryString({ ...options, q, pathname });
const separator = qs ? "?" : "";
const { data, error } = useSWR(
`/api/v1/opportunities/consultation/documents${separator}${qs}`,
`/api/v1/resources/documents${separator}${qs}`,
fetcher
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ async function processPageConsultation(page, api, context) {
slug: "documents",
title: documentsTitle ?? null,
};
// SWR fallback
let swrKey = `/api/v1/opportunities/consultation/documents`;

let swrKey = `/api/v1/resources/documents`;
const qs = queryString(documentsQuery);
if (qs) {
swrKey = `${swrKey}?${qs}`;
Expand Down Expand Up @@ -157,7 +157,7 @@ async function processPageConsultation(page, api, context) {
);
}
blocks[playlistIndex] = {
slug: "embedded-playlist",
...blocks[playlistIndex],
config: {
mostRecentText: "Most Recent",
relevanceText: "Relevance",
Expand All @@ -172,6 +172,7 @@ async function processPageConsultation(page, api, context) {
...playlistField,
items,
},
slug: "embedded-playlist",
title: title ?? null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default async function processPageDatasets(page, api, context) {
}
// eslint-disable-next-line no-param-reassign
page.fallback = {
[`${swrKey}`]: datasets,
[swrKey]: datasets,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ export default async function processPageDocuments(page, api, context) {
pathname: pageUrl,
};

let swrKey = `/api/v1/resources/datasets`;
let swrKey = `/api/v1/resources/documents`;
const qs = queryString(documentsQuery);
if (qs) {
swrKey = `${swrKey}?${qs}`;
}
// eslint-disable-next-line no-param-reassign
page.fallback = {
[`${swrKey}`]: documents,
[swrKey]: documents,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import { fetchDocuments } from "@/charterafrica/lib/sourceAfrica";
import { fetchResource } from "@/charterafrica/lib/youtube";
import * as Sentry from "@sentry/nextjs";

const documents = async (req, res) => {
const { q, media, pathname, ...rest } = req.query;
import { fetchResource } from "@/charterafrica/lib/youtube";

try {
const data = await fetchDocuments(q, pathname, rest);
return res.status(200).json(data);
} catch (error) {
return res.status(500).json({ error });
}
};
async function multimedia(req, res) {
const { pathname, ...rest } = req.query;

const multimedia = async (req, res) => {
try {
const { pathname, ...rest } = req.query;
const data = await fetchResource(pathname, rest);
return res.status(200).json(data);
} catch (error) {
return res.status(500).json(error);
}
};
const data = await fetchResource(pathname, rest);
return res.status(200).json(data);
}

const mediaMap = {
documents,
const fetchMediaMap = {
multimedia,
};

export default async function handler(req, res) {
const {
query: { media },
} = req;
const response = mediaMap[media];
if (response) {
return response(req, res);
const { media } = req.query;
const fetchMedia = fetchMediaMap[media];
if (fetchMedia) {
try {
return fetchMedia(req, res);
} catch (err) {
Sentry.captureException(err);
return res.status(500).json(err);
}
}
return res.status(404).json({ message: "UNKNOWN_MEDIA", media });
}
54 changes: 54 additions & 0 deletions apps/charterafrica/src/pages/api/v1/resources/[type].page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as Sentry from "@sentry/nextjs";

import fetchDatasets from "@/charterafrica/lib/openAfrica";
import { fetchDocuments } from "@/charterafrica/lib/sourceAfrica";

async function datasets(req, res) {
const {
sort = "metadata_created desc",
tags,
countries,
q = "",
page = 1,
pathname,
locale,
organizationId,
} = req.query;

const data = await fetchDatasets(organizationId, pathname, {
q,
page,
sort,
tags: tags?.split(","),
countries: countries?.split(","),
locale,
});
return res.status(200).json(data);
}

async function documents(req, res) {
const { q, pathname, ...rest } = req.query;

const data = await fetchDocuments(q, pathname, rest);
return res.status(200).json(data);
}

const fetchResourcesByType = {
datasets,
documents,
};

export default async function handler(req, res) {
const { type } = req.query;

const fetchResources = fetchResourcesByType[type];
if (fetchResources) {
try {
return fetchResources(req, res);
} catch (err) {
Sentry.captureException(err);
return res.status(500).json(err);
}
}
return res.status(404).json({ message: "UNKNOWN_TYPE", type });
}
39 changes: 0 additions & 39 deletions apps/charterafrica/src/pages/api/v1/resources/datasets.page.js

This file was deleted.

1 comment on commit a59930f

@vercel
Copy link

@vercel vercel bot commented on a59930f Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.