chore: move api to another component
This commit is contained in:
122
src/features/profile/api/settingsApi.ts
Normal file
122
src/features/profile/api/settingsApi.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import axios, { isAxiosError } from 'axios';
|
||||
import apiClient from '@/lib/apiClient';
|
||||
|
||||
import {
|
||||
type GetProfileApiResponse,
|
||||
type SaveProfileApiResponse,
|
||||
type TokenApiResponse,
|
||||
type PhoneNumberApiResponse,
|
||||
type ConfirmPhoneNumberApiResponse,
|
||||
} from '../types/settingsApiType';
|
||||
import { type InfoRowData } from '../types/settingsType';
|
||||
|
||||
const storedToken = () => localStorage.getItem('authToken');
|
||||
|
||||
export async function fetchProfile(): Promise<{ data: GetProfileApiResponse }> {
|
||||
const res = await apiClient.post<GetProfileApiResponse>(
|
||||
'/Profile/GetProfile',
|
||||
{},
|
||||
{ headers: { Authorization: `Bearer ${storedToken()}` } },
|
||||
);
|
||||
return { data: res.data };
|
||||
}
|
||||
|
||||
export async function saveProfile(payload?: {
|
||||
data: InfoRowData;
|
||||
imageUrl: string | null;
|
||||
}): Promise<{ data: SaveProfileApiResponse }> {
|
||||
if (!payload) {
|
||||
throw new Error('Payload for saving profile is missing.');
|
||||
}
|
||||
|
||||
const res = await apiClient.post<SaveProfileApiResponse>(
|
||||
'/Profile/SaveProfilePersonalInforamtion',
|
||||
payload,
|
||||
{ headers: { Authorization: `Bearer ${storedToken()}` } },
|
||||
);
|
||||
return { data: res.data };
|
||||
}
|
||||
|
||||
export async function getToken(): Promise<{
|
||||
data: { success: boolean; message?: string };
|
||||
}> {
|
||||
const apiUrl = 'https://accounts.business-harmony.com';
|
||||
const tokenEndpoint = `${apiUrl}/connect/token`;
|
||||
|
||||
try {
|
||||
const body = new URLSearchParams();
|
||||
body.set('grant_type', 'password');
|
||||
body.set('username', 'zareian.1381@gmail.com');
|
||||
body.set('password', '123@Qweasd');
|
||||
body.set('client_id', 'harmony_identity');
|
||||
body.set('scope', 'openid harmony_identity profile offline_access');
|
||||
|
||||
const response = await axios.post<TokenApiResponse>(
|
||||
tokenEndpoint,
|
||||
body.toString(),
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (response.data?.access_token) {
|
||||
localStorage.setItem('authToken', response.data.access_token);
|
||||
return { data: { success: true } };
|
||||
} else {
|
||||
throw new Error('No access token in response');
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
let message = 'Failed to get token';
|
||||
if (isAxiosError(error) && error.response) {
|
||||
message = `Request failed with status ${error.response.status}`;
|
||||
} else if (error instanceof Error) {
|
||||
message = error.message;
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendVerificationCode(payload?: {
|
||||
phoneNumber: string;
|
||||
}): Promise<{ data: PhoneNumberApiResponse }> {
|
||||
if (!payload) {
|
||||
throw new Error('Payload for sending verification code is missing.');
|
||||
}
|
||||
const res = await apiClient.post<PhoneNumberApiResponse>(
|
||||
'/Profile/SendVerfiyPhoneNumberCode',
|
||||
payload,
|
||||
{ headers: { Authorization: `Bearer ${storedToken()}` } },
|
||||
);
|
||||
return { data: res.data };
|
||||
}
|
||||
|
||||
export async function confirmPhoneNumberCode(payload?: {
|
||||
phoneNumber: string;
|
||||
verifyCode: string;
|
||||
}): Promise<{ data: ConfirmPhoneNumberApiResponse }> {
|
||||
if (!payload) {
|
||||
throw new Error('Payload for confirming phone number is missing.');
|
||||
}
|
||||
const res = await apiClient.post<ConfirmPhoneNumberApiResponse>(
|
||||
'/Profile/ConfirmPhoneNumberChangeCode',
|
||||
payload,
|
||||
{ headers: { Authorization: `Bearer ${storedToken()}` } },
|
||||
);
|
||||
return { data: res.data };
|
||||
}
|
||||
|
||||
export async function changePhoneNumber(payload?: {
|
||||
phoneNumber: string;
|
||||
}): Promise<{ data: PhoneNumberApiResponse }> {
|
||||
if (!payload) {
|
||||
throw new Error('Payload for changing phone number is missing.');
|
||||
}
|
||||
const res = await apiClient.post<PhoneNumberApiResponse>(
|
||||
'/Profile/ChangePhoneNumber',
|
||||
payload,
|
||||
{ headers: { Authorization: `Bearer ${storedToken()}` } },
|
||||
);
|
||||
return { data: res.data };
|
||||
}
|
||||
Reference in New Issue
Block a user