Skip to content

Commit 42ee244

Browse files
committed
Removes unused ref and simplifies project refresh logic
Eliminates the `isLoadingRef` as it was no longer in use and comments out the `hasProjectChanged` function to simplify the code. Improves the project refresh mechanism by directly updating the state when new projects are fetched, removing unnecessary checks and reducing complexity. This enhances maintainability and ensures clearer logic flow.
1 parent 315a43e commit 42ee244

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

src/app/api/projects/route.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,3 @@ export async function GET() {
66
const projects = await projectDataSource.getProjects()
77
return NextResponse.json({ projects })
88
}
9-
10-
export async function POST() {
11-
const projects = await projectDataSource.refreshProjects()
12-
return NextResponse.json({ projects })
13-
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse } from "next/server";
2+
import { projectDataSource } from "@/composition";
3+
4+
5+
export async function GET(request: Request) {
6+
const { searchParams } = new URL(request.url);
7+
const shouldRefresh = searchParams.get("refresh") === "true";
8+
const projects = shouldRefresh
9+
? await projectDataSource.refreshProjects()
10+
: await projectDataSource.getProjects();
11+
return NextResponse.json({ projects });}

src/features/projects/view/ProjectsContextProvider.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,26 @@ const ProjectsContextProvider = ({
1212
children?: React.ReactNode;
1313
}) => {
1414
const [projects, setProjects] = useState<Project[]>(initialProjects || []);
15-
const isLoadingRef = useRef(false);
1615
const [refreshing, setRefreshing] = useState(false);
1716

18-
const hasProjectChanged = (value: Project[]) =>
19-
value.some((project, index) => {
20-
// Compare by project id and version (or any other key fields)
21-
return (
22-
project.id !== projects[index]?.id ||
23-
project.versions !== projects[index]?.versions
24-
);
25-
});
26-
2717
const setProjectsAndRefreshed = (value: Project[]) => {
2818
setProjects(value);
2919
};
3020

3121
// Trigger background refresh after initial mount
3222
useEffect(() => {
3323
const refreshProjects = () => {
34-
if (isLoadingRef.current) return;
35-
isLoadingRef.current = true;
3624
setRefreshing(true);
3725

3826
fetch("/api/projects", { method: "POST" })
3927
.then((res) => res.json())
4028
.then(
41-
({ projects }) =>
42-
projects &&
43-
hasProjectChanged(projects) &&
44-
setProjectsAndRefreshed(projects)
29+
({ projects }) => {
30+
if (projects) setProjectsAndRefreshed(projects);
31+
}
4532
)
4633
.catch((error) => console.error("Failed to refresh projects", error))
4734
.finally(() => {
48-
isLoadingRef.current = false;
4935
setRefreshing(false);
5036
});
5137
};

0 commit comments

Comments
 (0)