Skip to content

Commit

Permalink
Fix Quick guide double show up (#4190)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasryaric authored Mar 7, 2024
1 parent e7f890f commit d844e59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
3 changes: 2 additions & 1 deletion front/lib/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export function useUser() {
export function useUserMetadata(key: string) {
const userMetadataFetcher: Fetcher<GetUserMetadataResponseBody> = fetcher;

const { data, error } = useSWR(
const { data, error, mutate } = useSWR(
`/api/user/metadata/${encodeURIComponent(key)}`,
userMetadataFetcher
);
Expand All @@ -253,6 +253,7 @@ export function useUserMetadata(key: string) {
metadata: data ? data.metadata : null,
isMetadataLoading: !error && !data,
isMetadataError: error,
mutateMetadata: mutate,
};
}

Expand Down
44 changes: 17 additions & 27 deletions front/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,25 @@ export async function getUserMetadataFromClient(key: string) {
}
}

/**
* Client-side: sets the metadata for the current user. This function is best effort, and never
* errors.
* @param metadata MetadataType the metadata to set for the current user.
*/
export function setUserMetadataFromClient(metadata: UserMetadataType) {
void (async () => {
try {
const res = await fetch(
`/api/user/metadata/${encodeURIComponent(metadata.key)}`,
{
method: "POST",
body: JSON.stringify({ value: metadata.value }),
headers: {
"Content-Type": "application/json",
},
}
);
export async function setUserMetadataFromClient(metadata: UserMetadataType) {
const res = await fetch(
`/api/user/metadata/${encodeURIComponent(metadata.key)}`,
{
method: "POST",
body: JSON.stringify({ value: metadata.value }),
headers: {
"Content-Type": "application/json",
},
}
);

if (!res.ok) {
const err = await res.json();
console.error("setUserMetadata error", err);
}
if (!res.ok) {
const err = await res.json();
console.error("setUserMetadata error", err);
throw new Error(`Error setting user metadata: ${err.message}`);
}

// Finally mutate to kick SWR to revalidate.
} catch (err) {
console.error("setUserMetadata error", err);
}
})();
return;
}

export const guessFirstandLastNameFromFullName = (
Expand Down
7 changes: 6 additions & 1 deletion front/pages/w/[wId]/assistant/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default function AssistantNew({
metadata: quickGuideSeen,
isMetadataError: isQuickGuideSeenError,
isMetadataLoading: isQuickGuideSeenLoading,
mutateMetadata: mutateQuickGuideSeen,
} = useUserMetadata("quick_guide_seen");
const [showQuickGuide, setShowQuickGuide] = useState<boolean>(false);

Expand Down Expand Up @@ -236,7 +237,11 @@ export default function AssistantNew({
}, [user]);

const { submit: persistQuickGuideSeen } = useSubmitFunction(async () => {
setUserMetadataFromClient({ key: "quick_guide_seen", value: "true" });
setUserMetadataFromClient({ key: "quick_guide_seen", value: "true" })
.then(() => {
return mutateQuickGuideSeen();
})
.catch(console.error);
setShowQuickGuide(false);
});

Expand Down

0 comments on commit d844e59

Please sign in to comment.