1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-08-31 04:59:50 +02:00

Fix flickering issue on the profile pages

This commit is contained in:
Kamran Ahmed
2023-05-12 22:38:14 +01:00
parent f4635d794f
commit a3470cd844
7 changed files with 43 additions and 12 deletions

View File

@@ -1,11 +1,22 @@
import { useStore } from '@nanostores/preact';
import { pageLoadingMessage } from '../stores/page';
import { useIsFirstRender } from '../hooks/use-is-first-render';
import SpinnerIcon from '../icons/spinner.svg';
import { pageLoadingMessage } from '../stores/page';
export function PageProgress() {
export interface Props {
initialMessage: string;
}
export function PageProgress(props: Props) {
const { initialMessage } = props;
const isFirstRender = useIsFirstRender();
const $pageLoadingMessage = useStore(pageLoadingMessage);
if (!$pageLoadingMessage) {
return null;
if (!initialMessage || !isFirstRender) {
return null;
}
}
return (
@@ -19,7 +30,7 @@ export function PageProgress() {
className="h-4 w-4 animate-spin fill-blue-600 text-gray-200 sm:h-4 sm:w-4"
/>
<h1 className="ml-2">
{$pageLoadingMessage}
{$pageLoadingMessage || initialMessage}
<span className="animate-pulse">...</span>
</h1>
</div>

View File

@@ -1,6 +1,4 @@
import { useCallback, useEffect, useState } from 'preact/hooks';
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/jwt';
import { useEffect, useState } from 'preact/hooks';
import { httpGet, httpPost } from '../../lib/http';
import { pageLoadingMessage } from '../../stores/page';
@@ -73,7 +71,6 @@ export default function UpdatePasswordForm() {
};
useEffect(() => {
pageLoadingMessage.set('Loading profile');
loadProfile().finally(() => {
pageLoadingMessage.set('');
});

View File

@@ -74,7 +74,6 @@ export function UpdateProfileForm() {
// Make a request to the backend to fill in the form with the current values
useEffect(() => {
pageLoadingMessage.set('Loading profile');
loadProfile().finally(() => {
pageLoadingMessage.set('');
});

View File

@@ -0,0 +1,13 @@
import { useRef } from "preact/hooks"
export function useIsFirstRender(): boolean {
const isFirst = useRef(true)
if (isFirst.current) {
isFirst.current = false
return true
}
return isFirst.current
}

View File

@@ -18,6 +18,7 @@ export interface Props {
keywords?: string[];
noIndex?: boolean;
canonicalUrl?: string;
initialLoadingMessage?: string;
permalink?: string;
jsonLd?: Record<string, unknown>[];
}
@@ -32,6 +33,7 @@ const {
canonicalUrl: givenCanonical = '',
jsonLd = [],
redirectUrl = '',
initialLoadingMessage = '',
} = Astro.props;
// Remove trailing slashes to consider the page as canonical
@@ -148,7 +150,7 @@ const gaPageIdentifier = Astro.url.pathname
</slot>
<Authenticator />
<PageProgress client:idle />
<PageProgress initialMessage={initialLoadingMessage} client:idle />
<PageSponsor
gaPageIdentifier={briefTitle || gaPageIdentifier}
client:load

View File

@@ -4,7 +4,12 @@ import UpdatePasswordForm from '../../components/Setting/UpdatePasswordForm';
import SettingLayout from '../../layouts/SettingLayout.astro';
---
<SettingLayout title='Change Password' description='' noIndex={true}>
<SettingLayout
title='Change Password'
description=''
noIndex={true}
initialLoadingMessage={'Loading profile'}
>
<SettingSidebar pageUrl='change-password' name='Change Password'>
<UpdatePasswordForm client:load />
</SettingSidebar>

View File

@@ -4,7 +4,11 @@ import { UpdateProfileForm } from '../../components/Setting/UpdateProfileForm';
import SettingLayout from '../../layouts/SettingLayout.astro';
---
<SettingLayout title='Update Profile' noIndex={true}>
<SettingLayout
title='Update Profile'
noIndex={true}
initialLoadingMessage={'Loading profile'}
>
<SettingSidebar pageUrl='profile' name='Profile'>
<UpdateProfileForm client:load />
</SettingSidebar>