This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add saveSrofileField and getGroupsWithFields
- Loading branch information
jakub.sedlacek2
committed
Oct 29, 2023
1 parent
be261c6
commit f32455c
Showing
3 changed files
with
87 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters