Skip to content

Commit

Permalink
Merge pull request #142 from BrunoAseff/staging
Browse files Browse the repository at this point in the history
fix: add timeStamp update to spaces change
  • Loading branch information
BrunoAseff authored Jan 17, 2025
2 parents 195c6a4 + 007130b commit 2391e0a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/app/api/settings/sync/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ export async function POST(req: Request) {
const processedChangeIds = await db.$transaction(async (tx) => {
const processedIds: string[] = [];

// Handle space changes in batch
if (groupedChanges.space) {
const settings = await tx.settings.findUnique({
where: { userId: session.user.id },
select: { spaces: { select: { id: true, clientId: true } } },
select: {
id: true,
spaces: { select: { id: true, clientId: true } },
}, // Added id to select
});

if (!settings) throw new Error("Settings not found");

// Process all space changes
await Promise.all(
groupedChanges.space.map(async (change: Change) => {
const space = settings.spaces.find(
Expand All @@ -53,6 +54,12 @@ export async function POST(req: Request) {
where: { id: space.id },
data: updateData,
});

await tx.settings.update({
where: { id: settings.id },
data: { lastModified: new Date() },
});

processedIds.push(change.id);
}),
);
Expand Down
19 changes: 13 additions & 6 deletions src/components/autoSaveProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import type { Change } from "@/types/changes";
import { useAutoSave } from "@/utils/useAutoSave";
import { createContext, useContext } from "react";
Expand Down Expand Up @@ -28,14 +29,20 @@ type Props = {
};

export function AutoSaveProvider({ children, userId }: Props) {
const { addChange, forceSyncNow, syncStatus } = useAutoSave(userId);

if (!userId) {
return <>{children}</>;
}
const { addChange, forceSyncNow, syncStatus } = useAutoSave(userId ?? "");

return (
<AutoSaveContext.Provider value={{ addChange, forceSyncNow, syncStatus }}>
<AutoSaveContext.Provider
value={
userId
? { addChange, forceSyncNow, syncStatus }
: {
addChange: () => {},
forceSyncNow: async () => {},
syncStatus: "idle" as const,
}
}
>
{children}
</AutoSaveContext.Provider>
);
Expand Down

0 comments on commit 2391e0a

Please sign in to comment.