<commit_before>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2020
 */

import { batch } from 'react-redux';
import { BluetoothStatus } from 'react-native-bluetooth-status';
import { request, check, PERMISSIONS, RESULTS } from 'react-native-permissions';

import { PermissionSelectors } from '../selectors/permission';

export const PERMISSION_ACTIONS = {
  SET_LOCATION: 'permissionActions/setLocation',
  SET_WRITE_STORAGE: 'permissionActions/setWriteStorage',
  SET_BLUETOOTH: 'permissionActions/setBluetooth',
};

const setBluetooth = status => ({ type: PERMISSION_ACTIONS.SET_BLUETOOTH, payload: { status } });
const setLocation = status => ({ type: PERMISSION_ACTIONS.SET_LOCATION, payload: { status } });
const setWriteStorage = status => ({
  type: PERMISSION_ACTIONS.SET_WRITE_STORAGE,
  payload: { status },
});

const requestBluetooth = () => async dispatch => {
  const result = await BluetoothStatus.enable();

  dispatch(setBluetooth(result));
};

const requestWriteStorage = () => async (dispatch, getState) => {
  const writeStoragePermission = PermissionSelectors.writeStorage(getState());

  if (!writeStoragePermission) {
    const result = await request(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE);
    dispatch(setWriteStorage(result === RESULTS.GRANTED));
  }
};

const requestLocation = () => async (dispatch, getState) => {
  const locationPermission = PermissionSelectors.location(getState());

  if (!locationPermission) {
    const result = await request(PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION);
    dispatch(setLocation(result === RESULTS.GRANTED));
  }
};

const checkPermissions = () => async dispatch => {
  const promises = [
    check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE),
    check(PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION),
    BluetoothStatus.state(),
  ];

  const [locationPermission, writeStoragePermission, bluetoothState] = await Promise.all(promises);

  batch(() => {<commit_msg>Add fix ordering bug<commit_after>/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2020
 */

import { batch } from 'react-redux';
import { BluetoothStatus } from 'react-native-bluetooth-status';
import { request, check, PERMISSIONS, RESULTS } from 'react-native-permissions';

import { PermissionSelectors } from '../selectors/permission';

export const PERMISSION_ACTIONS = {
  SET_LOCATION: 'permissionActions/setLocation',
  SET_WRITE_STORAGE: 'permissionActions/setWriteStorage',
  SET_BLUETOOTH: 'permissionActions/setBluetooth',
};

const setBluetooth = status => ({ type: PERMISSION_ACTIONS.SET_BLUETOOTH, payload: { status } });
const setLocation = status => ({ type: PERMISSION_ACTIONS.SET_LOCATION, payload: { status } });
const setWriteStorage = status => ({
  type: PERMISSION_ACTIONS.SET_WRITE_STORAGE,
  payload: { status },
});

const requestBluetooth = () => async dispatch => {
  const result = await BluetoothStatus.enable();

  dispatch(setBluetooth(result));
};

const requestWriteStorage = () => async (dispatch, getState) => {
  const writeStoragePermission = PermissionSelectors.writeStorage(getState());

  if (!writeStoragePermission) {
    const result = await request(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE);
    dispatch(setWriteStorage(result === RESULTS.GRANTED));
  }
};

const requestLocation = () => async (dispatch, getState) => {
  const locationPermission = PermissionSelectors.location(getState());

  if (!locationPermission) {
    const result = await request(PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION);
    dispatch(setLocation(result === RESULTS.GRANTED));
  }
};

const checkPermissions = () => async dispatch => {
  const promises = [
    check(PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION),
    check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE),
    BluetoothStatus.state(),
  ];

  const [locationPermission, writeStoragePermission, bluetoothState] = await Promise.all(promises);

  batch(() => {
{'fraction_non_alphanumeric': 0.12779060816012316, 'fraction_numerical': 0.002052861175263023, 'mean_word_length': 5.518394648829432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591360', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1155, 'n_words': 231}