diff --git a/app/api/config.ts b/app/api/config.ts index 890377e..9246acb 100644 --- a/app/api/config.ts +++ b/app/api/config.ts @@ -18,6 +18,10 @@ export const ENDPOINTS = { favorite: `${API_PREFIX}/favorite`, settings: { my: `${API_PREFIX}/profile/preference/my`, + statsEdit: `${API_PREFIX}/profile/preference/privacy/stats/edit`, + countsEdit: `${API_PREFIX}/profile/preference/privacy/counts/edit`, + socialEdit: `${API_PREFIX}/profile/preference/privacy/social/edit`, + friendRequestsEdit: `${API_PREFIX}/profile/preference/privacy/friendRequests/edit`, } }, filter: `${API_PREFIX}/filter`, diff --git a/app/components/Profile/Profile.EditPrivacyModal.tsx b/app/components/Profile/Profile.EditPrivacyModal.tsx index 1f75d03..33171cf 100644 --- a/app/components/Profile/Profile.EditPrivacyModal.tsx +++ b/app/components/Profile/Profile.EditPrivacyModal.tsx @@ -2,6 +2,7 @@ import { Modal } from "flowbite-react"; import { ENDPOINTS } from "#/api/config"; +import { useState } from "react"; export const ProfileEditPrivacyModal = (props: { isOpen: boolean; @@ -16,7 +17,6 @@ export const ProfileEditPrivacyModal = (props: { }; setPrivacySettings: (privacySettings: any) => void; }) => { - const setting_text = { privacy_stats: "Кто видит мою статистику, оценки и историю просмотра", privacy_counts: @@ -25,10 +25,40 @@ export const ProfileEditPrivacyModal = (props: { privacy_friend_requests: "Кто может отправлять мне заявки в друзья", }; + const _endpoints = { + privacy_stats: `${ENDPOINTS.user.settings.statsEdit}?token=${props.token}`, + privacy_counts: `${ENDPOINTS.user.settings.countsEdit}?token=${props.token}`, + privacy_social: `${ENDPOINTS.user.settings.socialEdit}?token=${props.token}`, + privacy_friend_requests: `${ENDPOINTS.user.settings.friendRequestsEdit}?token=${props.token}`, + }; + + const [loading, setLoading] = useState(false); + function _setPrivacySetting(el: any) { let privacySettings = structuredClone(props.privacySettings); - privacySettings[el.target.name] = el.target.value; - props.setPrivacySettings(privacySettings); + setLoading(true); + fetch(_endpoints[props.setting], { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + permission: el.target.value, + }), + }) + .then((res) => { + if (res.ok) { + setLoading(false); + privacySettings[el.target.name] = el.target.value; + props.setPrivacySettings(privacySettings); + } else { + new Error("failed to send data"); + } + }) + .catch((err) => { + console.log(err); + setLoading(false); + }); } return ( @@ -47,6 +77,7 @@ export const ProfileEditPrivacyModal = (props: { <>
_setPrivacySetting(e)} checked={props.privacySettings[props.setting] == 0} id="default-radio-1" @@ -64,6 +95,7 @@ export const ProfileEditPrivacyModal = (props: {
_setPrivacySetting(e)} checked={props.privacySettings[props.setting] == 1} id="default-radio-2" @@ -84,6 +116,7 @@ export const ProfileEditPrivacyModal = (props: { <>
_setPrivacySetting(e)} checked={props.privacySettings[props.setting] == 0} id="default-radio-1" @@ -101,6 +134,7 @@ export const ProfileEditPrivacyModal = (props: {
_setPrivacySetting(e)} checked={props.privacySettings[props.setting] == 1} id="default-radio-2" @@ -118,6 +152,7 @@ export const ProfileEditPrivacyModal = (props: {
_setPrivacySetting(e)} checked={props.privacySettings[props.setting] == 2} id="default-radio-3"