Skip to content

Commit

Permalink
refactor: settings api [WTEL-4024]
Browse files Browse the repository at this point in the history
  • Loading branch information
lizacoma committed Nov 8, 2023
1 parent d09fb34 commit ce76321
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions src/modules/settings/api/settings.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
import { EndpointPatcherApiConsumer } from 'webitel-sdk/esm2015/api-consumers';
import instance, { config } from '../../../app/api/old/instance';
import instance from '../../../app/api/instance';
import applyTransform, {
camelToSnake,
notify,
snakeToCamel
} from '@webitel/ui-sdk/src/api/transformers';

const baseUrl = 'users';
const itemPatcher = new EndpointPatcherApiConsumer({ baseUrl, instance });

export const changePassword = ({ id, changes }) => itemPatcher.patchItem({
id,
changes,
});
export const getWebPhone = async () => {

export const changeWebPhone = async (changes) => {
const url = 'user/settings/phone';

try {
config.errors.silent = true;
return await instance.put(url, changes);
const response = await instance.get(url);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw err;
} finally {
config.errors.silent = false;
throw applyTransform(err, [
notify,
]);
}
};

export const getWebPhone = async () => {
export const changeWebPhone = async (changes) => {

const item = applyTransform(changes, [
camelToSnake(),
]);

const url = 'user/settings/phone';

try {
config.errors.silent = true;
return await instance.get(url);
const response = await instance.put(url, item);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw err;
} finally {
config.errors.silent = false;
throw applyTransform(err, [
notify,
]);
}
};

const patchItem = async ({ changes, id }) => {
const body = applyTransform(changes, [
camelToSnake(),
]);
const url = `${baseUrl}/${id}`;
try {
const response = await instance.patch(url, body);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

export const changePassword = ({ id, changes }) => patchItem({
id,
changes,
});


export default {
changePassword,
changeWebPhone,
Expand Down

0 comments on commit ce76321

Please sign in to comment.