Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Add saveSrofileField and getGroupsWithFields
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub.sedlacek2 committed Oct 29, 2023
1 parent be261c6 commit f32455c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 20 deletions.
86 changes: 72 additions & 14 deletions src/app/api/profile-field/profileField.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,87 @@
import { getServerSession } from "next-auth";
import { GetProfileFieldsParams as GetProfileFieldsArgs } from "./profileField.type";
import { ProfileFieldResponse, ProfileGroupResponse } from "./profileField.type";
import { authOptions } from "app/api/auth/[...nextauth]/route";
import axios from "axios";
const http = axios.create({
baseURL: process.env.WP_API_URL,
headers: {
"Content-type": "application/json",
},
validateStatus: status => status >= 200 && status < 500,
baseURL: process.env.WP_API_URL,
headers: {
"Content-type": "application/json",
},
validateStatus: status => status >= 200 && status < 500,
});

export const getGroupsWithFields = async (args: { userId: number; groupId?: number }) => {
const session = await getServerSession(authOptions);
if (!session) return;

const response = await http.get<ProfileGroupResponse[]>(`${process.env.WP_API_URL}/xprofile/groups`, {
data: JSON.stringify({
user_id: args.userId,
profile_group_id: args.groupId,
fetch_field_data: true,
fetch_visibility_level: true,
fetch_fields: true,
context: "view",
}),
headers: { Authorization: session.wpJwtToken },
});

return response.data;
};

export const getProfileFields = async (args: { userId: number; groupId?: number }) => {
const session = await getServerSession(authOptions);
if (!session) return;

const response = await http.get<ProfileFieldResponse[]>(`${process.env.WP_API_URL}/xprofile/fields`, {
data: JSON.stringify({
user_id: args.userId,
profile_group_id: args.groupId,
fetch_field_data: true,
context: "view",
}),
headers: { Authorization: session.wpJwtToken },
});

export const getProfileFields = async (args: GetProfileFieldsArgs) => {
return response.data;
};

export const getProfileField = async (args: { userId: number; fieldId: number }) => {
const session = await getServerSession(authOptions);
if (!session) return;

const response = await http.get<GetProfileFieldsArgs>(`${process.env.WP_API_URL}/xprofile/fields`, {
const response = await http.get<ProfileFieldResponse>(`${process.env.WP_API_URL}/xprofile/fields/${args.fieldId}`, {
data: JSON.stringify({
user_id: args.userId,
fetch_field_data: true,
context: 'view'
}),
headers: { Authorization: session.wpJwtToken }
})
user_id: args.userId,
fetch_field_data: true,
context: "view",
}),
headers: { Authorization: session.wpJwtToken },
});

return response.data;
};

/*https://developer.buddypress.org/bp-rest-api/reference/extended-profiles/profile-data/*/
export const updateProfileField = async (args: {
fieldId: number;
userId: number;
//The value(s) (comma separated list of values needs to be used in case of multiple values) for the field data.
value: string;
}) => {
const session = await getServerSession(authOptions);
if (!session) return;

const response = await http.post<ProfileFieldResponse>(
`${process.env.WP_API_URL}/xprofile/${args.fieldId}/data/${args.userId}`,
{
context: "edit",
value: args.value,
},
{
headers: { Authorization: session.wpJwtToken },
},
);

return response.data;
};
13 changes: 10 additions & 3 deletions src/app/api/profile-field/profileField.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export type ProfileFieldResponse = {
_links?: Links;
}

export type ProfileGroupResponse = {
id: number;
name: string;
description: Description;
group_order: number;
can_delete: boolean;
fields: ProfileFieldResponse[];
_links: Links;
}

export type Links = {
self: Collection[];
collection: Collection[];
Expand All @@ -36,9 +46,6 @@ export type Data = {
value: Value;
}

export type GetProfileFieldsParams = {
userId: number
}

export type FieldOption = {
id: number;
Expand Down
8 changes: 5 additions & 3 deletions src/app/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Image from "next/image";
import { getCurrentMember } from "app/api/member/member";

import { isValidURL } from "utils/isValidURL";
import { getProfileFields } from "app/api/profile-field/profileField";
import { getGroupsWithFields } from "app/api/profile-field/profileField";

export const metadata: Metadata = {
title: "Můj profil",
Expand All @@ -16,10 +16,12 @@ export default async function Profile() {

if (!profile) return <div>Nebylo možné načíst data</div>;

const profileFields = await getProfileFields({
userId: profile?.id
const profileFields = await getGroupsWithFields({
userId: profile?.id,
});

if (!profileFields) return <div>Nebylo možné načíst data</div>;

return (
<main className="w-full p-5">
{profile.avatar_urls?.thumb && isValidURL(profile.avatar_urls.thumb) && (
Expand Down

0 comments on commit f32455c

Please sign in to comment.