Skip to content

Commit 91944df

Browse files
committed
Refactor museum collection logic and props
Moved museum collection summary calculations from the Collection component to the Utility function GetMCollection, which now returns total found, delivered, and missing items text. Updated Collection component to use these new props and removed local state and effect logic for totals.
1 parent cb422cb commit 91944df

2 files changed

Lines changed: 39 additions & 36 deletions

File tree

src/Components/AchieveTabs/Collection/index.tsx

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
import React, { useState, useEffect } from 'react'
21
import type { museumCollectionType } from 'types/displayDataTypes';
32

43
interface CollectionProps {
54
museumCollection: museumCollectionType;
65
}
76

8-
const Collection = ({ museumCollection }: CollectionProps) => {
9-
const [totalFound, setTotalFound] = useState(0);
10-
const [totalDelivered, setTotalDelivered] = useState(0);
11-
const [total, setTotal] = useState(0);
12-
13-
const getTotalFound = () => {
14-
const artifacts = museumCollection.artifacts;
15-
const minerals = museumCollection.minerals;
16-
17-
const totalArtFound = artifacts.reduce((accum, item) => (item.found) ? accum + 1 : accum, 0);
18-
const totalMinFound = minerals.reduce((accum, item) => (item.found) ? accum + 1 : accum, 0);
19-
const totalArtD = artifacts.reduce((accum, item) => (item.inMuseum) ? accum + 1 : accum, 0);
20-
const totalMinD = minerals.reduce((accum, item) => (item.inMuseum) ? accum + 1 : accum, 0);
21-
22-
setTotalFound(totalArtFound + totalMinFound);
23-
setTotalDelivered(totalArtD + totalMinD);
24-
setTotal(artifacts.length + minerals.length);
25-
};
26-
7+
const Collection = ({ museumCollection: mc }: CollectionProps) => {
278
const createCollectionItem = (item: any, i: number, type: string) => {
289
return (
2910
<a href={`https://stardewvalleywiki.com/${item.image}`} target="blank" key={i}>
@@ -42,30 +23,28 @@ const Collection = ({ museumCollection }: CollectionProps) => {
4223
);
4324
};
4425

45-
useEffect(() => {
46-
getTotalFound();
47-
}, []);
48-
4926
return (
5027
<div className="progress-container">
5128
<span className="a-title">
52-
<h1>{`You've found ${totalFound} objects and delivered ${totalDelivered} / ${total} to the museum`}</h1>
29+
<h1>
30+
{`You've found ${mc.totalFound || 0} objects and delivered ${mc.totalDelivered || 0} / ${mc.total || 0} to the museum`}
31+
</h1>
5332
</span>
5433
<br />
5534
<br />
5635
<h2>Museum Achievements</h2>
5736
<ul className="a-List">
58-
<li>A Complete Collection: {(total === totalDelivered) ?
37+
<li>A Complete Collection: {(mc.missingItemsText === undefined) ?
5938
<span className="completed">You have this achievement</span> :
60-
<span className="pending">You need to deliver {total - totalDelivered} more items to get this achievement.</span>
39+
<span className="pending">{mc.missingItemsText}</span>
6140
}
6241
</li>
6342
</ul>
6443
<span className="a-title"><h1>Artifacts</h1></span>
65-
{museumCollection.artifacts.map((item, i) => createCollectionItem(item, i, "Artifacts"))}
44+
{mc.artifacts.map((item, i) => createCollectionItem(item, i, "Artifacts"))}
6645

6746
<span className="a-title"><h1>Minerals</h1></span>
68-
{museumCollection.minerals.map((item, i) => createCollectionItem(item, i, "Minerals"))}
47+
{mc.minerals.map((item, i) => createCollectionItem(item, i, "Minerals"))}
6948
</div>
7049
);
7150
};

src/Components/Utility/Utility.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,32 +361,56 @@ const GetMonsterQuests = (allMonsters: itemsType[], slimesKilled: number): forma
361361
})
362362
return(mData)
363363
}
364-
const GetMCollection = (archeology: itemsType[], geology: itemsType[], currentCollection: itemsType[]) : museumCollectionType =>{
364+
const GetMCollection =
365+
(archeology: itemsType[], geology: itemsType[], currentCollection: itemsType[]) : museumCollectionType => {
365366

366367
if(currentCollection === undefined || currentCollection.length === 0) return {artifacts: [], minerals: []};
367368

368-
//Get found Archeology
369369
let artifacts: generalFormatedItemType[] = [];
370370
let minerals: generalFormatedItemType[] = []
371+
let _totalFound = 0;
372+
let _totalDonated = 0;
371373

372374
for(let collectionItem of Museum.collection) {
375+
let alreadyDonated = currentCollection.filter(c => c.value.int === collectionItem.id).length > 0;
376+
_totalDonated = alreadyDonated ? _totalDonated + 1 : _totalDonated;
373377
if( archeology && archeology.length > 0 && collectionItem.type === "artifact"){
378+
let found = archeology.filter(a => a.key.int === collectionItem.id).length > 0;
379+
_totalFound = found ? _totalFound + 1 : _totalFound;
380+
374381
artifacts.push({
375382
name: collectionItem.name,
376383
image: GetImages(collectionItem.name),
377-
found: (archeology && archeology.length > 0 && archeology.filter(a => a.key.int === collectionItem.id).length > 0),
378-
inMuseum: (currentCollection.filter(c => c.value.int === collectionItem.id).length > 0)
384+
found: found,
385+
inMuseum: alreadyDonated
379386
})
380387
} else if(geology && geology.length > 0 && collectionItem.type === "mineral"){
388+
let found = geology.filter(g => g.key.int === collectionItem.id).length > 0;
389+
_totalFound = found ? _totalFound + 1 : _totalFound;
381390
minerals.push({
382391
name: collectionItem.name,
383392
image: GetImages(collectionItem.name),
384-
found: (geology && geology.length > 0 && geology.filter(g => g.key.int === collectionItem.id).length > 0),
385-
inMuseum: (currentCollection.filter(c => c.value.int === collectionItem.id).length > 0)
393+
found: found,
394+
inMuseum: alreadyDonated
386395
})
387396
}
388397
}
389-
return {artifacts, minerals}
398+
399+
400+
let missingItemsText = (Museum.collection.length - _totalDonated > 0) ?
401+
`You need to deliver ${Museum.collection.length - _totalDonated} more items to get this achievement.` :
402+
undefined;
403+
404+
const museumCollection: museumCollectionType = {
405+
totalFound: _totalFound,
406+
totalDelivered: _totalDonated,
407+
total: Museum.collection.length,
408+
missingItemsText: missingItemsText,
409+
artifacts,
410+
minerals
411+
}
412+
413+
return museumCollection
390414
}
391415

392416
const ValidateKnown = (k:itemsType[], name: string) => {

0 commit comments

Comments
 (0)