utils#request JavaScript Examples

The following examples show how to use utils#request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* forgotPassword() {
  const emailAddress = yield select(makeSelectEmail());
  const locale = yield select(makeSelectLocale());
  const requestURL = api.auth.forgetPassword;
  const requestParameters = {
    method: 'POST',
    headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
    body: JSON.stringify({ emailAddress, locale }),
  };

  try {
    yield call(request, requestURL, requestParameters);
    yield put(forgotPasswordSuccessAction());
  } catch (error) {
    let message;

    switch (error?.statusCode) {
      case 400: {
        message = <FormattedMessage {...messages.notFound} />;
        break;
      }

      default: {
        message = <FormattedMessage {...messages.serverError} />;
        break;
      }
    }

    yield put(forgotPasswordErrorAction(message));
  }
}
Example #2
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* setUserData({ snippets }) {
  const { accessToken } = yield select(makeSelectToken());
  const newData = yield select(makeSelectNewData());
  const isCollapsedSidebar = yield select(makeSelectIsCollapsedSidebar());

  const requestURL = api.users();
  const requestParameters = {
    method: 'PATCH',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify(newData),
  };
  const style = { width: 400, marginLeft: isCollapsedSidebar ? 80 : 250 };
  const placement = 'bottomLeft';

  try {
    const userData = yield call(request, requestURL, requestParameters);
    yield put(setUserDataSuccessAction(userData));

    notification.success({
      message: snippets.success.title,
      description: snippets.success.description,
      style,
      placement,
    });
  } catch (error) {
    yield put(setUserDataIncorrectAction(error));
  }
}
Example #3
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getUserData() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.users();
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const userData = yield call(request, requestURL, requestParameters);
    yield put(getUserDataSuccessAction(userData));
  } catch (error) {
    yield put(getUserDataErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #4
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* resetPassword() {
  const password = yield select(makeSelectPassword());
  const token = yield select(makeSelectToken());
  const requestURL = api.auth.resetPassword;
  const requestParameters = {
    method: 'PATCH',
    headers: {       Authorization: `Bearer ${token}`, Accept: 'application/json', 'Content-Type': 'application/json' },
    body: JSON.stringify({ password })
  };

  try {
    yield call(request, requestURL, requestParameters);
    yield put(resetPasswordSuccessAction());
  } catch (error) {
    let message;

    switch (error?.statusCode) {
      case 400: {
        message = <FormattedMessage {...messages.tokenError} />;
        break;
      }

      default: {
        message = <FormattedMessage {...messages.serverError} />;
        break;
      }
    }

    yield put(resetPasswordErrorAction(message));
  }
}
Example #5
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* loginExpress() {
  const pinCode = yield select(makeSelectPinCode());
  const password = yield select(makeSelectPassword());

  const requestURL = api.auth.login;
  const requestParameters = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ pinCode, password }),
  };

  try {
    const { user, token } = yield call(request, requestURL, requestParameters);
    yield put(loginExpressSuccessAction(user, token));
    yield put(push(routes.dashboard.path));
  } catch (error) {
    const message = <FormattedMessage {...messages.serverError} />;
    yield put(loginExpressErrorAction(message));
  }
}
Example #6
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* register() {
  const firstName = yield select(makeSelectFirstName());
  const lastName = yield select(makeSelectLastName());
  const email = yield select(makeSelectEmail());
  const password = yield select(makeSelectPassword());
  const currency = yield select(makeSelectCurrency());

  const requestURL = api.auth.register;
  const requestParameters = {
    method: 'POST',
    headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
    body: JSON.stringify({ firstName, lastName, email, password, currency }),
  };

  try {
    const {
      userAuth: { pinCode },
    } = yield call(request, requestURL, requestParameters);
    yield put(registerSuccessAction(pinCode));
  } catch (error) {
    let message;

    switch (error.statusCode) {
      case 409:
        message = <FormattedMessage {...messages.emailUnique} />;
        break;

      default:
        message = <FormattedMessage {...messages.serverError} />;
        break;
    }

    yield put(registerErrorAction(message));
  }
}
Example #7
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getAuthorizationKey() {
  const { accessToken } = yield select(makeSelectToken());
  const transaction = yield select(makeSelectTransaction());
  const requestURL = api.transactions('authorizationKey')(transaction);
  const requestParameters = {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
  };

  try {
    const { authorizationKey } = yield call(
      request,
      requestURL,
      requestParameters,
    );
    yield put(getAuthorizationKeySuccessAction(authorizationKey));
  } catch (error) {
    // yield put(getAuthorizationKeyErrorAction(error));
    // switch (error.statusCode) {
    //   case 401:
    //     yield put(push(routes.login.path));
    //     break;
    //   default:
    //     yield put(push(routes.login.path));
    //     break;
    // }
  }
}
Example #8
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* createTransaction() {
  const { accessToken } = yield select(makeSelectToken());
  const locale = yield select(makeSelectLocale());
  const senderBill = yield select(makeSelectSenderBillUuid());
  const recipientBill = yield select(makeSelectRecipientBillUuid());
  const amountMoney = yield select(makeSelectAmountMoney());
  const transferTitle = yield select(makeSelectTransferTitle());
  const requestURL = api.transactions('create');
  const requestParameters = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({
      amountMoney,
      transferTitle,
      senderBill,
      recipientBill,
      locale,
    }),
  };

  try {
    const { uuid } = yield call(request, requestURL, requestParameters);
    yield put(createTransactionSuccessAction(uuid));
  } catch (error) {
    // yield put(createTransactionErrorAction(error));
    // switch (error.statusCode) {
    //   case 401:
    //     yield put(push(routes.login.path));
    //     break;
    //   default:
    //     yield put(push(routes.login.path));
    //     break;
    // }
  }
}
Example #9
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* searchRecipient({ value }) {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills('search')(value);
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  if (!numberValidation(value)) {
    return;
  }

  if (value.length > 1) {
    yield delay(300);
  }

  try {
    const { data } = yield call(request, requestURL, requestParameters);
    yield put(searchRecipientSuccessAction(data));
  } catch (error) {
    yield put(searchRecipientErrorAction(error));

    switch (error.statusCode) {
      case 401:
        yield put(push(routes.login.path));
        break;
      default:
        yield put(push(routes.login.path));
        break;
    }
  }
}
Example #10
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getBills() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills();
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { data } = yield call(request, requestURL, requestParameters);
    yield put(getBillsSuccessAction(data));
  } catch (error) {
    yield put(getBillsErrorAction(error));

    switch (error.statusCode) {
      case 401:
        yield put(push(routes.login.path));
        break;
      default:
        yield put(push(routes.login.path));
        break;
    }
  }
}
Example #11
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* login() {
  const pinCode = yield select(makeSelectPinCode());
  const password = yield select(makeSelectPassword());

  const requestURL = api.auth.login;
  const requestParameters = {
    method: 'POST',
    headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
    body: JSON.stringify({ pinCode, password }),
  };

  try {
    const { user, token } = yield call(request, requestURL, requestParameters);
    yield put(loginSuccessAction(user, token));
    yield put(push(routes.dashboard.path));
  } catch (error) {
    let message;

    switch (error.statusCode) {
      case 404:
        message = (
          <FormattedMessage
            {...messages.accountNotFound}
            values={{ pinCode }}
          />
        );
        break;
      case 403:
        message = <FormattedMessage {...messages.passwordInvalid} />;
        break;
      default:
        message = <FormattedMessage {...messages.serverError} />;
        break;
    }

    yield put(loginErrorAction(message));
  }
}
Example #12
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getConfirmationFile({ uuid }) {
  const locale = yield select(makeSelectLocale());
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = `${api.transactions('confirmationFile')(uuid, locale)}`;
  const requestParameters = {
    responseType: 'blob',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const response = yield call(request, requestURL, requestParameters, false);
    const pdfBlob = new Blob([response], { type: 'application/pdf' });
    saveAs(pdfBlob, `${uuid}.pdf`);

    yield put(getConfirmationFileSuccessAction());
  } catch (error) {
    yield put(getConfirmationFileErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #13
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getTransactionHistory({ currentPage }) {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = `${api.transactions()}?page=${currentPage}&order=DESC`;
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const transactions = yield call(request, requestURL, requestParameters);
    yield put(getTransactionHistorySuccessAction(transactions));
  } catch (error) {
    yield put(getTransactionHistoryErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #14
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* logout() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.auth.logout;
  const requestParameters = {
    method: 'PATCH',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
  };

  try {
    yield call(request, requestURL, requestParameters);
    yield put(logoutSuccessAction());
    yield put(push(routes.home.path));
  } catch (error) {
    yield put(logoutErrorAction(error));

    switch (error.statusCode) {
      case 401:
        yield put(push(routes.home.path));
        break;
      default:
        yield put(push(routes.login.path));
        break;
    }
  }
}
Example #15
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getRecentTransactions() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = `${api.transactions()}?take=4&order=DESC`;
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { data } = yield call(request, requestURL, requestParameters);
    yield put(getRecentTransactionsSuccessAction(data));
  } catch (error) {
    yield put(getRecentTransactionsErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #16
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getBills() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills();
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { data } = yield call(request, requestURL, requestParameters);
    yield put(getBillsSuccessAction(data));
  } catch (error) {
    yield put(getBillsErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #17
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
function* getAccountBalanceHistory() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills('accountBalanceHistory');
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { accountBalanceHistory } = yield call(
      request,
      requestURL,
      requestParameters,
    );

    return yield accountBalanceHistory;
  } catch (error) {
    throw new Error(error);
  }
}
Example #18
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
function* getAmountMoney() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills('amountMoney');
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { amountMoney, currencyName } = yield call(
      request,
      requestURL,
      requestParameters,
    );

    return yield { amountMoney, currencyName };
  } catch (error) {
    throw new Error(error);
  }
}
Example #19
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getNotifications() {
  const { accessToken } = yield select(makeSelectToken());
  const user = yield select(makeSelectUser());
  const requestURL = `${api.notifications}?take=${user?.userConfig?.notificationCount}&order=DESC`;

  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const response = yield call(request, requestURL, requestParameters);
    yield put(getNotificationsSuccessAction(response));
  } catch (error) {
    yield put(getNotificationsErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #20
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* readMessage() {
  const { accessToken } = yield select(makeSelectToken());
  const openedMessage = yield select(makeSelectOpenedMessage());
  const requestURL = api.messages;
  const requestParameters = {
    method: 'PATCH',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({ uuid: openedMessage }),
  };

  try {
    yield put(readMessageAction());
    yield call(request, requestURL, requestParameters);
    yield put(readMessageSuccessAction());
  } catch (error) {
    yield put(readMessageErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #21
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* readAllMessages() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.messages;
  const requestParameters = {
    method: 'PATCH',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
  };

  try {
    yield call(request, requestURL, requestParameters);
    yield put(readAllMessagesSuccessAction());
  } catch (error) {
    yield put(readAllMessagesErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #22
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getMessages() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.messages;
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const response = yield call(request, requestURL, requestParameters);
    yield put(getMessagesSuccessAction(response));
  } catch (error) {
    yield put(getMessagesErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #23
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* checkEmail({ value, reject, resolve }) {
  const requestURL = api.users('checkEmail')(value);

  if (!value) {
    yield call(resolve);
  }

  if (emailValidation(value)) {
    try {
      yield delay(400);
      const { exist } = yield call(request, requestURL);
      yield put(checkEmailSuccessAction(exist));

      if (exist) {
        yield call(reject);
      } else {
        yield call(resolve);
      }
    } catch (error) {
      const message = <FormattedMessage {...messages.serverError} />;
      yield put(checkEmailErrorAction(message));
    }
  } else {
    yield put(checkEmailInvalidAction());
    yield call(resolve);
  }
}
Example #24
Source File: saga.js    From bank-client with MIT License 6 votes vote down vote up
export function* getCurrencies() {
  const requestURL = api.currencies;

  try {
    const { data } = yield call(request, requestURL);
    yield put(getCurrenciesSuccessAction(data));
  } catch (error) {
    const message = <FormattedMessage {...messages.serverError} />;
    yield put(getCurrenciesErrorAction(message));
  }
}
Example #25
Source File: saga.js    From bank-client with MIT License 5 votes vote down vote up
export function* createNewBill({ snippets }) {
  const currency = yield select(makeSelectCurrency());
  const isCollapsedSidebar = yield select(makeSelectIsCollapsedSidebar());
  const { accessToken } = yield select(makeSelectToken());

  const requestURL = api.bills();
  const requestParameters = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({ currency }),
  };

  const style = { width: 400, marginLeft: isCollapsedSidebar ? 80 : 250 };
  const placement = 'bottomLeft';

  try {
    const bill = yield call(request, requestURL, requestParameters);
    delete bill.user;

    yield put(createNewBillSuccessAction(bill));

    notification.success({
      message: snippets.success.title,
      description: snippets.success.description,
      style,
      placement,
    });
  } catch (error) {
    switch (error.statusCode) {
      case 400:
        yield put(createNewBillIncorrectAction(error));

        notification.error({
          message: snippets.error.title,
          description: snippets.error.description,
          style,
          placement,
        });
        break;
      default:
        yield put(createNewBillErrorAction(error));
        yield put(push(routes.login.path));
        break;
    }
  }
}
Example #26
Source File: saga.js    From bank-client with MIT License 5 votes vote down vote up
export function* getAccountBalance() {
  const { accessToken } = yield select(makeSelectToken());
  const requestURL = api.bills('accountBalance');
  const requestParameters = {
    method: 'GET',
    headers: { Authorization: `Bearer ${accessToken}` },
  };

  try {
    const { revenues, expenses, currencyName } = yield call(
      request,
      requestURL,
      requestParameters,
    );

    const revenuesPercent =
      ((Number(revenues) - Number(expenses)) / Number(revenues)) * 100;
    let savingsPercent;
    let savingsData;
    let savingsColors;

    if (Number(revenues) === Infinity) {
      savingsPercent = 100;
      savingsColors = [colors.primaryBlue, colors.red];
      savingsData = [
        { name: 'revenues', value: parseFloat(revenues) },
        { name: 'expenses', value: parseFloat(expenses) },
      ];
    } else if (Number(expenses) === Infinity) {
      savingsPercent = 0;
      savingsColors = [colors.primaryBlue, colors.red];
      savingsData = [
        { name: 'revenues', value: parseFloat(revenues) },
        { name: 'expenses', value: parseFloat(expenses) },
      ];
    } else if (!Number(revenues) && !Number(expenses)) {
      savingsPercent = 0;
      savingsColors = ['#b8b8b8'];
      savingsData = [{ name: 'savings', value: 100 }];
    } else {
      savingsPercent = Math.max(0, revenuesPercent);
      savingsColors = [colors.primaryBlue, colors.red];
      savingsData = [
        { name: 'revenues', value: parseFloat(revenues) },
        { name: 'expenses', value: parseFloat(expenses) },
      ];
    }

    yield put(
      getAccountBalanceSuccessAction(
        currencyName,
        savingsPercent,
        savingsData,
        savingsColors,
      ),
    );
  } catch (error) {
    yield put(getAccountBalanceErrorAction(error));
    yield put(push(routes.login.path));
  }
}
Example #27
Source File: saga.js    From bank-client with MIT License 5 votes vote down vote up
export function* confirmTransaction({ snippets }) {
  const { accessToken } = yield select(makeSelectToken());
  const authorizationKey = yield select(makeSelectAuthorizationKey());
  const isCollapsedSidebar = yield select(makeSelectIsCollapsedSidebar());
  const requestURL = api.transactions('confirm');
  const requestParameters = {
    method: 'PATCH',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({ authorizationKey }),
  };
  const style = { width: 400, marginLeft: isCollapsedSidebar ? 80 : 250 };
  const placement = 'bottomLeft';

  try {
    yield call(request, requestURL, requestParameters);
    yield put(confirmTransactionSuccessAction());

    notification.success({
      message: snippets.success.title,
      description: snippets.success.description,
      style,
      placement,
    });

    yield put(push(routes.dashboard.path));
  } catch (error) {
    let message;

    switch (error.statusCode) {
      case 404:
        message = <FormattedMessage {...messages.authorizationKeyIncorrect} />;
        break;
      default:
        break;
    }

    yield put(confirmTransactionIncorrectAction(message));
  }
}