Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix trader page loading, dynamic trader links #946

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 17 additions & 43 deletions src/components/menu/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//import { Suspense } from 'react';
import { useMemo } from 'react';
import useStateWithLocalStorage from '../../hooks/useStateWithLocalStorage.jsx';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
Expand All @@ -25,6 +25,7 @@ import alertConfig from './alert-config.js';
import IntersectionObserverWrapper from './intersection-observer-wrapper.js';

import './index.css';
import useTradersData from '../../features/traders/index.js';

// automatically selects the alert color
const alertColor = alertConfig.alertColors[alertConfig.alertLevel];
Expand Down Expand Up @@ -64,6 +65,11 @@ const Menu = () => {
}

const { data: bosses } = useBossesData();
const { data: allTraders } = useTradersData();

const traders = useMemo(() => {
return allTraders.filter(t => t.barters?.length > 0)
}, [allTraders]);

return (
<>
Expand Down Expand Up @@ -197,48 +203,16 @@ const Menu = () => {
<li className="submenu-wrapper submenu-items overflow-member" key="menu-traders" data-targetid="traders">
<Link to="/traders">{t('Traders')}</Link>
<ul>
<MenuItem
displayText={t('Prapor')}
key="menu-item-prapor"
to={`/trader/prapor`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Therapist')}
key="menu-item-therapist"
to={`/trader/therapist`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Skier')}
key="menu-item-skier"
to={`/trader/skier`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Peacekeeper')}
key="menu-item-peacekeeper"
to={`/trader/peacekeeper`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Mechanic')}
key="menu-item-mechanic"
to={`/trader/mechanic`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Ragman')}
key="menu-item-ragman"
to={`/trader/ragman`}
//onClick={setIsOpen.bind(this, false)}
/>
<MenuItem
displayText={t('Jaeger')}
key="menu-item-jaeger"
to={`/trader/jaeger`}
//onClick={setIsOpen.bind(this, false)}
/>
{traders.map(trader => {
return (
<MenuItem
displayText={trader.name}
key={`menu-item-${trader.normalizedName}`}
to={`/trader/${trader.normalizedName}`}
//onClick={setIsOpen.bind(this, false)}
/>
);
})}
</ul>
</li>
<li className="submenu-wrapper submenu-items overflow-member" key="menu-bosses" data-targetid="bosses">
Expand Down
13 changes: 11 additions & 2 deletions src/pages/trader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SmallItemTable from '../../components/small-item-table/index.js';
import QuestTable from '../../components/quest-table/index.js';
import TraderResetTime from '../../components/trader-reset-time/index.js';
import ErrorPage from '../error-page/index.js';
import Loading from '../../components/loading/index.js';
import LoadingSmall from '../../components/loading-small/index.js';
import PropertyList from '../../components/property-list/index.js';
import formatPrice from '../../modules/format-price.js';
Expand Down Expand Up @@ -81,12 +82,17 @@ function Trader() {
},
[setNameFilter],
);
const { data: traders } = useTradersData();
const { data: traders, status } = useTradersData();

const trader = traders.find(tr => tr.normalizedName === traderName.toLowerCase());
const trader = useMemo(() => {
return traders.find(tr => tr.normalizedName === traderName.toLowerCase());;
}, [traders, traderName]);

const levelProperties = useMemo(() => {
const props = {};
if (!trader) {
return props;
}
if (!Number.isInteger(selectedTable)) {
return props;
}
Expand All @@ -100,6 +106,9 @@ function Trader() {
}
return props;
}, [trader, selectedTable, t]);
if (!trader && (status === 'idle' || status === 'loading')) {
return <Loading/>;
}
if (!trader)
return <ErrorPage />;

Expand Down
10 changes: 5 additions & 5 deletions src/pages/traders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import TraderResetTime from '../../components/trader-reset-time/index.js';
import LoadingSmall from '../../components/loading-small/index.js';

import useTradersData from '../../features/traders/index.js';
import useItemsData from '../../features/items/index.js';

import i18n from '../../i18n.js';

Expand All @@ -19,12 +18,13 @@ import './index.css';
function Traders(props) {
const { t } = useTranslation();
const { data: allTraders } = useTradersData();
const { data: items } = useItemsData();

// filter traders to only those who sell items
// filter traders to only those who have barters
// we used to check for sell items, but requires all item data
// items.some(item => item.buyFor.some(offer => offer.vendor.trader?.id === trader.id))
const traders = useMemo(() => {
return allTraders.filter(trader => items.some(item => item.buyFor.some(offer => offer.vendor.trader?.id === trader.id)));
}, [allTraders, items]);
return allTraders.filter(trader => trader.barters?.length > 0);
}, [allTraders]);

return [
<SEO
Expand Down