Skip to content

Commit

Permalink
* Refactors API requests to remove xivapi-js dependency
Browse files Browse the repository at this point in the history
* Refactors request params to reliably filter Actions and CraftActions (for DOH actions)
* Updates tooltip to request CraftActions content in addition to standard Actions
  • Loading branch information
bdejesus committed Jun 20, 2020
1 parent 684015e commit 7f71f60
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 60 deletions.
7 changes: 4 additions & 3 deletions components/Tooltip/context.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { createContext, useReducer } from 'react';
import PropTypes from 'prop-types';
import XIVAPI from 'xivapi-js';
import fetch from 'node-fetch';

const api = new XIVAPI();
const TooltipContext = createContext();
const TooltipDispatchContext = createContext();

Expand Down Expand Up @@ -56,7 +55,9 @@ async function updateTooltip(dispatch, data) {
const { action, position } = data;
dispatch({ type: 'startUpdate', data });
try {
const content = await api.data.get('Action', action.ID);
const request = `https://xivapi.com/${action.UrlType}/${action.ID}`;
const content = await fetch(request).then((res) => res.json());

if (content.Description) {
dispatch({ type: 'finishUpdate', content, position });
} else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"@zeit/next-sass": "^1.0.1",
"express": "^4.17.1",
"next": "9.3.2",
"node-fetch": "^2.6.0",
"path": "^0.12.7",
"prop-types": "^15.7.2",
"query-string": "^6.9.0",
"react": "^16.12.0",
"react-dom": "^16.9.0",
"sitemap": "^5.1.0",
"xivapi-js": "0.3.0"
"sitemap": "^5.1.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
Expand Down
51 changes: 27 additions & 24 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import XIVAPI from 'xivapi-js';
import { ascByKey } from 'utils';
import { ADVANCED_JOBS, ROLE_ACTION_IDS } from 'data/jobs';
import Header from 'components/Header';
Expand All @@ -9,6 +8,7 @@ import Footer from 'components/Footer';
import JobSelect from 'components/JobSelect';
import { JobSelectContextProvider } from 'components/JobSelect/context';
import JobMenu from 'components/JobSelect/JobMenu';
import fetch from 'node-fetch';
import XIVBars from './XIVBars';
import { XIVBarsContextProvider } from './XIVBars/context';

Expand Down Expand Up @@ -76,12 +76,14 @@ function Index({
Index.getInitialProps = async (req) => {
const ctx = req;

// eslint-disable-next-line global-require
const api = new XIVAPI();
// TODO: Refactor API calls into a separate lib component
const apiUrl = 'https://xivapi.com';

// Get Jobs List
const jobsData = await api.data.list('ClassJob');
const jobs = jobsData.Results.sort(ascByKey('Name'));
const jobsData = await fetch(`${apiUrl}/ClassJob`)
.then((res) => res.json())
.then((json) => json.Results);
const jobs = jobsData.sort(ascByKey('Name'));

function decorateJobs() {
const decoratedData = ADVANCED_JOBS.map((advancedJob) => {
Expand All @@ -100,29 +102,30 @@ Index.getInitialProps = async (req) => {
let jobActions = [];
let roleActions = [];

// Fetch Actions
if (selectedJob) {
// Get Job Actions
const jobActionsData = await api.search('', {
filters: `ClassJob.ID=${selectedJob.ID}`
});
jobActions = jobActionsData.Results;

if (selectedJob.ClassID !== null) {
const classActionsReq = await api.search('', {
filters: `ClassJob.ID=${selectedJob.ClassID}`
const jobActionRequest = `${apiUrl}/search?indexes=Action,CraftAction&filters=ClassJobTargetID=${selectedJob.ID}`;

jobActions = await fetch(jobActionRequest)
.then((res) => res.json())
.then(async (actions) => {
let classJobActions = [];
if (selectedJob.ClassID !== null) {
const classJobActionsRequest = `${apiUrl}/search?indexes=Action,CraftAction&filters=ClassJobTargetID=${selectedJob.ClassID}`;
classJobActions = await fetch(classJobActionsRequest)
.then((res) => res.json())
.then((json) => json.Results);
}
return [...classJobActions, ...actions.Results];
});
jobActions = jobActions.concat(classActionsReq.Results);
}

jobActions = jobActions.filter((action) => action.UrlType === 'Action');
jobActions = jobActions.sort(ascByKey('Icon'));
jobActions = jobActions.filter(
(action, index, self) => index === self.findIndex((t) => t.Name === action.Name)
);

if (selectedJob.Role) {
const roleActionsData = await api.data.list('Action', { ids: ROLE_ACTION_IDS[selectedJob.Role].toString() });
roleActions = roleActionsData.Results;
// Refactor this is pull IDS from ClassJob object instead of ROLE_ACTION_IDS
roleActions = await fetch(
`${apiUrl}/Action?ids=${ROLE_ACTION_IDS[selectedJob.Role].toString()}`
)
.then((res) => res.json())
.then((json) => json.Results);
}
}

Expand Down
33 changes: 2 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6901,7 +6901,7 @@ no-case@^2.2.0:
dependencies:
lower-case "^1.1.1"

node-fetch@2.6.0:
node-fetch@2.6.0, node-fetch@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
Expand Down Expand Up @@ -8568,22 +8568,6 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"

request-promise-core@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.3.tgz#e9a3c081b51380dfea677336061fea879a829ee9"
integrity sha512-QIs2+ArIGQVp5ZYbWD5ZLCY29D5CfWizP8eWnm8FoGD1TX61veauETVQbrV60662V0oFBkrDOuaBI8XgtuyYAQ==
dependencies:
lodash "^4.17.15"

request-promise-native@^1.0.5:
version "1.0.8"
resolved "https://registry.yarnpkg.com/request-promise-native/-/request-promise-native-1.0.8.tgz#a455b960b826e44e2bf8999af64dff2bfe58cb36"
integrity sha512-dapwLGqkHtwL5AEbfenuzjTYg35Jd6KPytsC2/TLkVMz8rm+tNt72MGUWT1RP/aYawMpN6HqbNGBQaRcBtjQMQ==
dependencies:
request-promise-core "1.1.3"
stealthy-require "^1.1.1"
tough-cookie "^2.3.3"

request@^2.87.0, request@^2.88.0:
version "2.88.2"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
Expand Down Expand Up @@ -9285,11 +9269,6 @@ stdout-stream@^1.4.0:
dependencies:
readable-stream "^2.0.1"

stealthy-require@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b"
integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=

stream-browserify@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.2.tgz#87521d38a44aa7ee91ce1cd2a47df0cb49dd660b"
Expand Down Expand Up @@ -9749,7 +9728,7 @@ toidentifier@1.0.0:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

tough-cookie@^2.3.3, tough-cookie@~2.5.0:
tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
integrity sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==
Expand Down Expand Up @@ -10378,14 +10357,6 @@ ws@^6.2.1:
dependencies:
async-limiter "~1.0.0"

xivapi-js@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/xivapi-js/-/xivapi-js-0.3.0.tgz#3dc27994c72bb55853e1befd487f9c0cdff22bab"
integrity sha512-l9l6YqUdL65DtghQyBnpjORikaBnoAaV10tZM3qEBO9MHqcix4u1PzJSbWNkyvyilDrWRoBlV5OMMoKnh9J2iw==
dependencies:
request "^2.88.0"
request-promise-native "^1.0.5"

xmlbuilder@^13.0.2:
version "13.0.2"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-13.0.2.tgz#02ae33614b6a047d1c32b5389c1fdacb2bce47a7"
Expand Down

0 comments on commit 7f71f60

Please sign in to comment.