diff --git a/src/app/api/profile-field/profileField.ts b/src/app/api/profile-field/profileField.ts index 13ba306..b8343cf 100644 --- a/src/app/api/profile-field/profileField.ts +++ b/src/app/api/profile-field/profileField.ts @@ -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(`${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(`${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(`${process.env.WP_API_URL}/xprofile/fields`, { + const response = await http.get(`${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( + `${process.env.WP_API_URL}/xprofile/${args.fieldId}/data/${args.userId}`, + { + context: "edit", + value: args.value, + }, + { + headers: { Authorization: session.wpJwtToken }, + }, + ); + return response.data; +}; diff --git a/src/app/api/profile-field/profileField.type.ts b/src/app/api/profile-field/profileField.type.ts index 0a69306..ae9dfad 100644 --- a/src/app/api/profile-field/profileField.type.ts +++ b/src/app/api/profile-field/profileField.type.ts @@ -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[]; @@ -36,9 +46,6 @@ export type Data = { value: Value; } -export type GetProfileFieldsParams = { - userId: number -} export type FieldOption = { id: number; diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index 7818d6d..404a9ba 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -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", @@ -16,10 +16,12 @@ export default async function Profile() { if (!profile) return
Nebylo možné načíst data
; - const profileFields = await getProfileFields({ - userId: profile?.id + const profileFields = await getGroupsWithFields({ + userId: profile?.id, }); + if (!profileFields) return
Nebylo možné načíst data
; + return (
{profile.avatar_urls?.thumb && isValidURL(profile.avatar_urls.thumb) && (