better profile photo update logic

This commit is contained in:
Daniel
2023-06-08 17:09:22 +03:30
parent 39abb09002
commit dcdef77387
5 changed files with 31 additions and 53 deletions
+2 -12
View File
@@ -1,6 +1,5 @@
import { create } from "zustand";
import { AccountSettings } from "@/types/global";
import avatarExists from "@/lib/client/avatarExists";
type AccountStore = {
account: AccountSettings;
@@ -8,13 +7,6 @@ type AccountStore = {
updateAccount: (user: AccountSettings) => Promise<boolean>;
};
const determineProfilePicSource = async (data: any) => {
const path = `/api/avatar/${data.response.id}`;
const imageExists = await avatarExists(path);
if (imageExists) return path + "?" + Date.now();
else return null;
};
const useAccountStore = create<AccountStore>()((set) => ({
account: {} as AccountSettings,
setAccount: async (email) => {
@@ -22,7 +14,7 @@ const useAccountStore = create<AccountStore>()((set) => ({
const data = await response.json();
const profilePic = await determineProfilePicSource(data);
const profilePic = `/api/avatar/${data.response.id}?${Date.now()}`;
if (response.ok) set({ account: { ...data.response, profilePic } });
},
@@ -39,9 +31,7 @@ const useAccountStore = create<AccountStore>()((set) => ({
console.log(data);
const profilePic = await determineProfilePicSource(data);
if (response.ok) set({ account: { ...data.response, profilePic } });
if (response.ok) set({ account: { ...data.response } });
return response.ok;
},