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

Update Feature Fetch #870

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 36 additions & 16 deletions src/components/form/bounty/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,48 @@ function Form(props: FormProps) {
useEffect(() => {
(async () => {
try {
const features = await main.getWorkspaceFeatures(workspaceid, {
page: 1,
status: 'active'
});
interface Feature {
uuid: string;
name: string;
}
let allFeatures: Feature[] = [];
let currentPage = 1;
let hasMoreData = true;

while (hasMoreData) {
const features = await main.getWorkspaceFeatures(workspaceid, {
page: currentPage,
status: 'active'
});

console.log(features);

if (Array.isArray(features)) {
allFeatures = [...allFeatures, ...features];
console.log(allFeatures);
hasMoreData = features.length > 0; // If no features returned, stop fetching
console.log(hasMoreData);
currentPage++;
} else {
console.log('Unexpected features object:', features);
hasMoreData = false; // Stop loop on unexpected response
}
}

if (Array.isArray(features)) {
const filteredFeatures = features.map(
({ uuid, name }: { uuid: string; name: string }) => ({
value: uuid,
label: name
})
);
// Process features once all pages are fetched
const filteredFeatures = allFeatures.map(
({ uuid, name }: { uuid: string; name: string }) => ({
value: uuid,
label: name
})
);

setWorkspaceFeatures(filteredFeatures);
} else {
console.log('Features object:', features);
}
setWorkspaceFeatures(filteredFeatures);
} catch (error) {
console.error('Error fetching or parsing features:', error);
}
})();
}, [main, featureid, workspaceFeature, workspaceid]);
}, [main, workspaceid]);

useEffect(() => {
(async () => {
Expand Down
80 changes: 52 additions & 28 deletions src/people/widgetViews/WorkspaceMission.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable no-unused-vars */
import {
EuiDragDropContext,
EuiDraggable,
Expand Down Expand Up @@ -78,7 +80,6 @@ import AddRepoModal from './workspace/AddRepoModal';
import EditSchematic from './workspace/EditSchematicModal';
import ManageWorkspaceUsersModal from './workspace/ManageWorkspaceUsersModal';
import { BudgetWrapComponent } from './BudgetWrap';
import { LoadMoreContainer } from './WidgetSwitchViewer';
import { EditableField } from './workspace/EditableField';
import { Toast } from './workspace/interface';

Expand Down Expand Up @@ -301,7 +302,6 @@ const WorkspaceMission = () => {
const [modalType, setModalType] = useState('add');
const [featureModal, setFeatureModal] = useState(false);
const [features, setFeatures] = useState<Feature[]>([]);
const [currentPage, setCurrentPage] = useState(1);
const [featuresCount, setFeaturesCount] = useState(0);
const [isOpenUserManage, setIsOpenUserManage] = useState<boolean>(false);
const [users, setUsers] = useState<Person[]>([]);
Expand All @@ -321,6 +321,7 @@ const WorkspaceMission = () => {
const [missionPreviewMode, setMissionPreviewMode] = useState<'preview' | 'edit'>('edit');
const [tacticsPreviewMode, setTacticsPreviewMode] = useState<'preview' | 'edit'>('edit');
const [toasts, setToasts] = useState<Toast[]>([]);
const [holding, setHolding] = useState(false);

const fetchCodeGraph = useCallback(async () => {
try {
Expand Down Expand Up @@ -559,18 +560,18 @@ const WorkspaceMission = () => {
getWorkspaceUsers();
}, [getWorkspaceData, getWorkspaceUsers]);

const getFeaturesCount = useCallback(async () => {
if (!uuid) return;
const featuresCount = await main.getWorkspaceFeaturesCount(uuid);
if (!featuresCount) return;
setFeaturesCount(featuresCount);
// const getFeaturesCount = useCallback(async () => {
// if (!uuid) return;
// const featuresCount = await main.getWorkspaceFeaturesCount(uuid);
// if (!featuresCount) return;
// setFeaturesCount(featuresCount);

setLoading(false);
}, [uuid, main]);
// setLoading(false);
// }, [uuid, main]);

useEffect(() => {
getFeaturesCount();
}, [getFeaturesCount]);
// useEffect(() => {
// getFeaturesCount();
// }, [getFeaturesCount]);

const updateFeatures = (newFeatures: Feature[]) => {
const updatedFeatures: Feature[] = [...features];
Expand All @@ -585,17 +586,40 @@ const WorkspaceMission = () => {

const getFeatures = useCallback(async () => {
if (!uuid) return;
const featuresRes = await main.getWorkspaceFeatures(uuid, {
page: currentPage,
status: 'active'
});
if (!featuresRes) return;

updateFeatures(featuresRes);
getFeaturesCount();
setLoading(true); // Set loading to true when the fetch starts
setHolding(true); // Set holding to true to show fetching is in progress

setLoading(false);
}, [uuid, main, getFeaturesCount, currentPage]);
let allFeatures: Feature[] = [];
let page = 1;
let hasMoreData = true;

try {
while (hasMoreData) {
const featuresRes = await main.getWorkspaceFeatures(uuid, {
page,
status: 'active'
});

if (featuresRes && Array.isArray(featuresRes)) {
allFeatures = [...allFeatures, ...featuresRes];
hasMoreData = featuresRes.length > 0; // Stop fetching if no data returned
page++;
} else {
hasMoreData = false; // Stop on unexpected response
}
}

if (allFeatures.length > 0) {
updateFeatures(allFeatures); // Update features with all data
}
} catch (error) {
console.error('Error fetching features:', error);
} finally {
setLoading(false); // Reset loading state after fetch completes
setHolding(false); // Reset holding state after fetch completes
}
}, [uuid, main]);

useEffect(() => {
getFeatures();
Expand Down Expand Up @@ -712,10 +736,10 @@ const WorkspaceMission = () => {
setSchematicModal(!schematicModal);
};

const loadMore = () => {
const nextPage = currentPage + 1;
setCurrentPage(nextPage);
};
// const loadMore = () => {
// const nextPage = currentPage + 1;
// setCurrentPage(nextPage);
// };

const handleReorderFeatures = async (feat: Feature, priority: number) => {
await main.addWorkspaceFeature({
Expand Down Expand Up @@ -757,7 +781,7 @@ const WorkspaceMission = () => {
[users]
);

if (loading) {
if (loading || holding) {
return (
<Body style={{ justifyContent: 'center', alignItems: 'center' }}>
<EuiLoadingSpinner size="xl" />
Expand Down Expand Up @@ -1337,7 +1361,7 @@ const WorkspaceMission = () => {
</EuiDroppable>
</EuiDragDropContext>
</FeaturesWrap>
{featuresCount > features.length ? (
{/* {featuresCount > features.length ? (
<LoadMoreContainer
color={color}
style={{
Expand All @@ -1351,7 +1375,7 @@ const WorkspaceMission = () => {
Load More
</div>
</LoadMoreContainer>
) : null}
) : null} */}
</FieldWrap>
</DataWrap>
<Modal
Expand Down
Loading