Skip to content
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
12 changes: 5 additions & 7 deletions web/src/routes/admin/users/[id]/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { goto, invalidateAll } from '$app/navigation';
import AdminCard from '$lib/components/AdminCard.svelte';
import AdminPageLayout from '$lib/components/layouts/AdminPageLayout.svelte';
import OnEvents from '$lib/components/OnEvents.svelte';
Expand Down Expand Up @@ -48,10 +48,7 @@

const { children, data }: Props = $props();

let user = $state(data.user);
const userPreferences = $state(data.userPreferences);
const userStatistics = $state(data.userStatistics);
const userSessions = $state(data.userSessions);
const { user, userPreferences, userStatistics, userSessions } = $derived(data);
const TiB = 1024 ** 4;
const usage = $derived(user.quotaUsageInBytes ?? 0);
let [statsUsage, statsUsageUnit] = $derived(getBytesWithUnit(usage, usage > TiB ? 2 : 0));
Expand Down Expand Up @@ -79,9 +76,10 @@

const { ResetPassword, ResetPinCode, Update, Delete, Restore } = $derived(getUserAdminActions($t, user));

const onUpdate = (update: UserAdminResponseDto) => {
const onUpdate = async (update: UserAdminResponseDto) => {
if (update.id === user.id) {
user = update;
data.user = update;
await invalidateAll();
Comment on lines +81 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the primary issue that data.user isn't reactive? If you assign user = update instead it would work, right? Without re-fetching all the data

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how it was before and it doesn't work. The user is loaded in the layout load function which you cannot update by reassigning. When navigating to /edit it will use the stale values from the layout function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before it was assigning data.user. I meant the derived user object

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data returned by a load function is read-only, you can’t update it by reassigning it, you can only get fresh values by invalidating and re-running the load function.

  1. Opening the edit modal navigates to /[id]/edit, and the modal reads the user from the layout’s load data
  2. After saving, you’re redirected back to /[id], and the local user state updates correctly there
  3. When you open the edit modal again, it still reads the layout's load data, which hasn’t been re-run, so it shows the old (stale) values

To fix step 3, we need to invalidate so it re-fetches and the edit modal always gets the latest user data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh got it, thanks!

}
};

Expand Down
10 changes: 4 additions & 6 deletions web/src/routes/admin/users/[id]/edit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

let { data }: Props = $props();

const user = $state(data.user);
let isAdmin = $state(user.isAdmin);
let name = $state(user.name);
let email = $state(user.email);
let storageLabel = $state(user.storageLabel || '');
const previousQuota = $state(user.quotaSizeInBytes);
const user = $derived(data.user);
let { isAdmin, name, email } = $derived(user);
let storageLabel = $derived(user.storageLabel || '');
const previousQuota = $derived(user.quotaSizeInBytes);

let quotaSize = $derived(
typeof user.quotaSizeInBytes === 'number' ? convertFromBytes(user.quotaSizeInBytes, ByteUnit.GiB) : undefined,
Expand Down
Loading