| 1 |
import {addTab} from '../store/actions'; |
| 2 |
import axios from 'axios'; |
| 3 |
|
| 4 |
export const registerTab = (tab) => { |
| 5 |
const {dispatch} = window.giveDonorDashboard.store; |
| 6 |
|
| 7 |
// Validate the tab object |
| 8 |
if (isValidTab(tab) === true) { |
| 9 |
dispatch(addTab(tab)); |
| 10 |
} else { |
| 11 |
return null; |
| 12 |
} |
| 13 |
}; |
| 14 |
|
| 15 |
const isValidTab = (tab) => { |
| 16 |
const tabPropTypes = { |
| 17 |
slug: 'string', |
| 18 |
icon: 'string', |
| 19 |
label: 'string', |
| 20 |
content: 'function', |
| 21 |
}; |
| 22 |
|
| 23 |
const isValid = Object.keys(tabPropTypes).reduce((acc, key) => { |
| 24 |
if (typeof tab[key] !== tabPropTypes[key]) { |
| 25 |
/* eslint-disable-next-line */ |
| 26 |
console.error(`Error registering tab! The '${key}' property must be a ${tabPropTypes[key]}.`); |
| 27 |
return false; |
| 28 |
} else if (acc === false) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
return true; |
| 32 |
}); |
| 33 |
|
| 34 |
return isValid; |
| 35 |
}; |
| 36 |
|
| 37 |
export const getWindowData = (value) => { |
| 38 |
const data = window.giveDonorDashboardData; |
| 39 |
return data[value]; |
| 40 |
}; |
| 41 |
|
| 42 |
export const getQueryParam = (param) => { |
| 43 |
const urlParams = new URLSearchParams(window.location.search); |
| 44 |
return urlParams.get(param); |
| 45 |
}; |
| 46 |
|
| 47 |
export const isLoggedIn = () => { |
| 48 |
return Number(getWindowData('id')) !== 0 ? true : false; |
| 49 |
}; |
| 50 |
|
| 51 |
export const getAPIRoot = () => { |
| 52 |
return getWindowData('apiRoot'); |
| 53 |
}; |
| 54 |
|
| 55 |
export const getAPINonce = () => { |
| 56 |
return getWindowData('apiNonce'); |
| 57 |
}; |
| 58 |
|
| 59 |
export const donorDashboardApi = axios.create({ |
| 60 |
baseURL: getAPIRoot() + 'give-api/v2/donor-dashboard/', |
| 61 |
headers: {'X-WP-Nonce': getAPINonce()}, |
| 62 |
}); |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns string in Kebab Case (ex: kebab-case) |
| 66 |
* |
| 67 |
* @param {string} str String to be returned in Kebab Case |
| 68 |
* @return {string} String returned in Kebab Case |
| 69 |
* @since 2.8.0 |
| 70 |
*/ |
| 71 |
export const toKebabCase = (str) => { |
| 72 |
return str |
| 73 |
.replace(' / ', ' ') |
| 74 |
.replace(/([a-z])([A-Z])/g, '$1-$2') |
| 75 |
.replace(/\s+/g, '-') |
| 76 |
.toLowerCase(); |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns a unique id in kebab case for components |
| 81 |
* |
| 82 |
* @param {string} str String to be returned as unique id |
| 83 |
* @return {string} String returned as unique id |
| 84 |
* @since 2.8.0 |
| 85 |
*/ |
| 86 |
export const toUniqueId = (str) => { |
| 87 |
const prefix = str ? str : 'component'; |
| 88 |
return toKebabCase(`${prefix}-${Math.floor(Math.random() * Math.floor(1000))}`); |
| 89 |
}; |
| 90 |
|