tweak: always show 'save changes' button

This commit is contained in:
sam 2023-08-06 15:38:33 +02:00
parent e0069a9375
commit 93a113206f
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1

View file

@ -28,26 +28,6 @@
setContext("currentUser", currentUser); setContext("currentUser", currentUser);
let error: APIError | null = null; let error: APIError | null = null;
let modified = false;
$: modified = isModified($currentUser, $user);
/** Returns whether or not the user in the store (referred to as newUser) is modified. */
const isModified = (user: MeUser, newUser: MeUser) => {
if ((newUser.bio || "") !== (user.bio || "")) return true;
if ((newUser.display_name || "") !== (user.display_name || "")) return true;
if ((newUser.member_title || "") !== (user.member_title || "")) return true;
if (!linksEqual(newUser.links, user.links)) return true;
if (!fieldsEqual(newUser.fields, user.fields)) return true;
if (!flagsEqual(newUser.flags, user.flags)) return true;
if (!namesEqual(newUser.names, user.names)) return true;
if (!pronounsEqual(newUser.pronouns, user.pronouns)) return true;
if (!customPreferencesEqual(newUser.custom_preferences, user.custom_preferences)) return true;
if (newUser.avatar !== null) return true;
if (newUser.list_private !== user.list_private) return true;
if (newUser.timezone !== user.timezone) return true;
return false;
};
const updateUser = async () => { const updateUser = async () => {
const toastId = addToast({ const toastId = addToast({
@ -86,67 +66,6 @@
delToast(toastId); delToast(toastId);
} }
}; };
// The individual functions called in isModified follow.
const fieldsEqual = (arr1: Field[], arr2: Field[]) => {
if (arr1?.length !== arr2?.length) return false;
if (!arr1.every((_, i) => arr1[i].entries.length === arr2[i].entries.length)) return false;
if (!arr1.every((_, i) => arr1[i].name === arr2[i].name)) return false;
return arr1.every((_, i) =>
arr1[i].entries.every(
(entry, j) =>
entry.value === arr2[i].entries[j].value && entry.status === arr2[i].entries[j].status,
),
);
};
const namesEqual = (arr1: FieldEntry[], arr2: FieldEntry[]) => {
console.log("new:", JSON.stringify(arr1));
console.log("current:", JSON.stringify(arr2));
if (arr1?.length !== arr2?.length) return false;
if (!arr1.every((_, i) => arr1[i].value === arr2[i].value)) return false;
if (!arr1.every((_, i) => arr1[i].status === arr2[i].status)) return false;
return true;
};
const pronounsEqual = (arr1: Pronoun[], arr2: Pronoun[]) => {
if (arr1?.length !== arr2?.length) return false;
if (!arr1.every((_, i) => arr1[i].pronouns === arr2[i].pronouns)) return false;
if (!arr1.every((_, i) => arr1[i].display_text === arr2[i].display_text)) return false;
if (!arr1.every((_, i) => arr1[i].status === arr2[i].status)) return false;
return true;
};
const linksEqual = (arr1: string[], arr2: string[]) => {
if (arr1.length !== arr2.length) return false;
return arr1.every((_, i) => arr1[i] === arr2[i]);
};
const flagsEqual = (arr1: PrideFlag[], arr2: PrideFlag[]) => {
if (arr1.length !== arr2.length) return false;
return arr1.every((_, i) => arr1[i].id === arr2[i].id);
};
const customPreferencesEqual = (obj1: CustomPreferences, obj2: CustomPreferences) => {
if (Object.keys(obj2).some((key) => !(key in obj1))) return false;
return Object.keys(obj1)
.map((key) => {
if (!(key in obj2)) return false;
return (
obj1[key].icon === obj2[key].icon &&
obj1[key].tooltip === obj2[key].tooltip &&
obj1[key].favourite === obj2[key].favourite &&
obj1[key].muted === obj2[key].muted &&
obj1[key].size === obj2[key].size
);
})
.every((entry) => entry);
};
</script> </script>
<svelte:head> <svelte:head>
@ -160,9 +79,7 @@
<Icon name="chevron-left" /> <Icon name="chevron-left" />
Back to your profile Back to your profile
</Button> </Button>
{#if modified}
<Button color="success" on:click={() => updateUser()}>Save changes</Button> <Button color="success" on:click={() => updateUser()}>Save changes</Button>
{/if}
</ButtonGroup> </ButtonGroup>
</h1> </h1>