Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): use new way for profile HTTP requests #155

Merged
merged 4 commits into from
Mar 7, 2025
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
8 changes: 3 additions & 5 deletions src/app/profile/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import Switch from '@/components/Switch';
import { BASE_INPUT_STYLE } from '@/constants/config';
import { classNames } from '@/utils';

import { updateUser } from '#/domain/profile/repository';
import SocialSettingsFormView from '#/domain/profile/views/social-settings-form';
import { ProfileTitle, ProfileLabel } from '#/domain/profile/widgets/blocks';
import { upload } from '#/services/common';
import { postUserInfo } from '#/services/user';
import { useUser } from '#/state/application/hooks';
import { useConfig } from '#/state/application/hooks';

Expand Down Expand Up @@ -149,7 +149,7 @@ export default function Profile() {
) {
setFromError(true);
} else {
const res = await postUserInfo({
const res = await updateUser({
user_avatar: forms.avatar,
user_bio: forms.bio,
user_city: forms.city,
Expand All @@ -170,12 +170,10 @@ export default function Profile() {
user_show_email: forms.emailVisible,
user_show_github: forms.githubVisible,
});
if (res.code === 200) {
if (res.success) {
toast.success('Saved successfully');
setShowSave(false);
// doFetch()
} else {
toast.error(res.message);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/u/[handle]/Banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import Image from '@/components/Image';

import { revalidatePathAction } from '#/app/actions';
import { updateBanner } from '#/domain/profile/repository';
import { upload } from '#/services/common';
import { changeBanner } from '#/services/user';
import { useUser } from '#/state/application/hooks';

export function Banner({data}) {
Expand All @@ -47,7 +47,7 @@
setLoading(true);
upload({ file: formData })
.then(async res => {
await changeBanner(res.data.user_upload_path);
await updateBanner(res.data.user_upload_path);
setLoading(false);
revalidatePathAction();
})
Expand All @@ -74,7 +74,7 @@
{data?.base.user_id === user?.base.user_id && (
<button className="absolute top-3 right-3 md:top-4 md:right-[60px]">
<label htmlFor="upload-cover" className="flex items-center text-white gap-2 bg-[rgba(26,26,26,0.6)] hover:bg-[rgba(26,26,26,0.8)] rounded px-3 py-2 cursor-pointer">
<img className={clsx({'animate-spin': loading})} src={'/images/svg/refetch.svg'} alt="" />

Check warning on line 77 in src/app/u/[handle]/Banner.js

View workflow job for this annotation

GitHub Actions / lint

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element

Check warning on line 77 in src/app/u/[handle]/Banner.js

View workflow job for this annotation

GitHub Actions / lint

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element
Change cover
</label>
<input
Expand Down
22 changes: 11 additions & 11 deletions src/app/u/[handle]/[follow]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
import { useSession } from 'next-auth/react';
import { usePathname, useRouter } from 'next/navigation';
import { useState, useCallback, useEffect } from 'react';
import { toast } from 'react-toastify';

import Avatar from '@/components/Avatar';
import { Button } from '@/components/Button';
import { SvgIcon } from '@/components/Image';
import Loader from '@/components/Loader';
import { NoData } from '@/components/NoData';
import { post, get } from '@/utils/request';

import { fetchFollowerList, fetchFollowedList, followUser, unfollowUser } from '#/domain/profile/repository';
import TabBarWidget from '#/domain/profile/widgets/tab-bar';
import { useUser } from '#/state/application/hooks';

const followFetchMap = {
followers: fetchFollowerList,
following: fetchFollowedList,
};

export default function Follow({ params }) {
const { status } = useSession();
const router = useRouter();
Expand All @@ -47,7 +51,7 @@ export default function Follow({ params }) {
const fetchList = useCallback(async () => {
// handle
setLoading(true);
const data = await get(`ts/v1/user/${params.handle}/${params.follow}?&skip=${(page - 1) * 20}&take=${20}`);
const data = await followFetchMap[params.follow]({ handle: params.handle, pageNum: page });
setLoading(false);
if (page === 1) {
setList(data.data.list);
Expand All @@ -66,14 +70,12 @@ export default function Follow({ params }) {
router.push(`/signin?from=${pathname}`);
} else {
setFollowLoading(index);
const res = await post(`ts/v1/user/follow/${item.user.user_id}`);
const res = await followUser(item.user.user_id);
setFollowLoading(null);
if (res.code === 200) {
if (res.success) {
const _prevList = [...list];
_prevList[index].mutual = type;
setList(_prevList);
} else {
toast.error(res.message);
}
}
};
Expand All @@ -83,14 +85,12 @@ export default function Follow({ params }) {
router.push(`/signin?from=${pathname}`);
} else {
setFollowLoading(index);
const res = await post(`ts/v1/user/follow/${item.user.user_id}/del`);
const res = await unfollowUser(item.user.user_id);
setFollowLoading(null);
if (res.code === 200) {
if (res.success) {
const _prevList = [...list];
_prevList[index].mutual = type;
setList(_prevList);
} else {
toast.error(res.message);
}
}
};
Expand Down
18 changes: 6 additions & 12 deletions src/app/u/[handle]/creator/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,18 @@
* limitations under the License.
*/

import { get } from '@/utils/request';
import { fetchUser, fetchUserActivityList } from '#/domain/profile/repository';

import ProjectOwner from '../ProjectOwner';

export default async function CreatorProfile({ params }) {
const config = { isServer: true };
const { data } = await get(`ts/v1/user/info/handle/${params.handle}`, config);
if (data?.social.user_wallet && data?.base.user_show_wallet) {
data.web3Bio = await get(`https://api.web3.bio/profile/${data?.social.user_wallet}`, {
...config,
headers: {
'X-API-KEY': process.env.NEXT_PUBLIC_WEB3BIO,
},
});
}
const { data } = await fetchUser(params.handle);

if (!data?.base?.user_project_owner) {
return <div>This user is not a creator.</div>;
}
const { data: activityData } = await get(`ts/v1/user/info/${data?.base.user_id}/creator/activity`, config);

const { data: activityData } = await fetchUserActivityList(data?.base.user_id);

return <ProjectOwner data={data} activities={activityData?.list || []} />;
}
15 changes: 2 additions & 13 deletions src/app/u/[handle]/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@
* limitations under the License.
*/

// import { Content } from './Content'
import { get } from '@/utils/request';
import { fetchUser } from '#/domain/profile/repository';

import { Banner } from './Banner';
import InfoCard from './InfoCard';

export default async function UserProfileLayout({ params, children }) {
const config = {isServer: true};
const { data } = await get(`ts/v1/user/info/handle/${params.handle}`, config);

if (data?.social.user_wallet && data?.base.user_show_wallet) {
data.web3Bio = await get(`https://api.web3.bio/profile/${data?.social.user_wallet}`, {
...config,
headers: {
'X-API-KEY': process.env.NEXT_PUBLIC_WEB3BIO,
},
});
}
const { data } = await fetchUser(params.handle);

return (
<>
Expand Down
13 changes: 2 additions & 11 deletions src/app/u/[handle]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@
* limitations under the License.
*/

import { get } from '@/utils/request';
import { fetchUser } from '#/domain/profile/repository';

import ProjectPersonal from './ProjectPersonal';

export default async function UserProfile({ params }) {
const config = { isServer: true };
const { data } = await get(`ts/v1/user/info/handle/${params.handle}`, config);
const { data } = await fetchUser(params.handle);

if (data?.social.user_wallet && data?.base.user_show_wallet) {
data.web3Bio = await get(`https://api.web3.bio/profile/${data?.social.user_wallet}`, {
...config,
headers: {
'X-API-KEY': process.env.NEXT_PUBLIC_WEB3BIO,
},
});
}
return <ProjectPersonal data={data} />;
}
73 changes: 71 additions & 2 deletions src/domain/profile/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,73 @@
* limitations under the License.
*/

import { unwrapBlockData, wrapBlockData } from '@/components/block-editor';
import { unwrapBlockData, wrapBlockData } from '@/components/block-editor/helper';
import httpClient from '@/utils/http';

async function fetchWeb3BioProfile(address) {
return httpClient.get(`https://api.web3.bio/profile/${address}`, {
headers: { 'X-API-KEY': process.env.NEXT_PUBLIC_WEB3BIO },
});
}

async function fetchUser(handle) {
const res = await httpClient.get(`/user/info/handle/${handle}`);

if (!res.success) {
return res;
}

const { data, ...others } = res;

if (data?.social.user_wallet && data?.base.user_show_wallet) {
const { data: web3BioProfile } = await fetchWeb3BioProfile(data?.social.user_wallet);
data.web3Bio = web3BioProfile;
}

return { ...others, data, success: true };
}

async function updateUser(data) {
return httpClient.post('/user/info', data);
}

async function fetchUserActivityList(uid) {
return httpClient.get(`/user/info/${uid}/creator/activity`);
}

function resolvePaginationParams(pageNum) {
const pageSize = 20;

return {
skip: (pageNum - 1) * pageSize,
take: pageSize,
};
}

async function fetchFollowerList(params = {}) {
const { handle, pageNum } = params;

return httpClient.get(`/user/${handle}/followers`, { params: resolvePaginationParams(pageNum) });
}

async function fetchFollowedList(params = {}) {
const { handle, pageNum } = params;

return httpClient.get(`/user/${handle}/following`, { params: resolvePaginationParams(pageNum) });
}

async function followUser(uid) {
return httpClient.post(`/user/follow/${uid}`);
}

async function unfollowUser(uid) {
return httpClient.post(`/user/follow/${uid}/del`);
}

async function updateBanner(url) {
return httpClient.post('/user/info/banner', { background_image: url });
}

async function fetchBlockContent(uid) {
return httpClient.get('/user/devplaza', { params: { uid } }).then(res => res.success ? ({
...res,
Expand All @@ -28,4 +92,9 @@ async function updateBlockContent(data) {
return httpClient.post('/user/devplaza', { body: wrapBlockData(data) });
}

export { fetchBlockContent, updateBlockContent };
export {
fetchUser, updateUser, fetchUserActivityList,
fetchFollowerList, fetchFollowedList, followUser, unfollowUser,
updateBanner,
fetchBlockContent, updateBlockContent,
};
25 changes: 0 additions & 25 deletions src/services/user/index.js

This file was deleted.

24 changes: 18 additions & 6 deletions src/shared/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ import { isLogicalSuccess, request } from './request';

async function normalizeResponse(res) {
if (res.ok) {
const { code, message, data, ...extra } = await res.json();
const jsonData = await res.json();

if (isPlainObject(jsonData)) {
const { code, message, data, ...extra } = jsonData;

return {
success: isLogicalSuccess(code),
code,
message,
data,
extra,
};
}

return {
success: isLogicalSuccess(code),
code,
message,
data,
extra,
success: true,
code: res.status,
message: undefined,
data: jsonData,
extra: {},
};
}

Expand Down
Loading