PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonorDashboards / resources / js / app / utils / index.js

index.js in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/DonorDashboards/resources/js/app/utils/index.js

128 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import {__} from '@wordpress/i18n';
3 import {addTab} from '../store/actions';
4
5 export const registerTab = (tab) => {
6 const {dispatch} = window.giveDonorDashboard.store;
7
8 // Validate the tab object
9 if (isValidTab(tab) === true) {
10 dispatch(addTab(tab));
11 } else {
12 return null;
13 }
14 };
15
16 const isValidTab = (tab) => {
17 const tabPropTypes = {
18 slug: 'string',
19 icon: 'string',
20 label: 'string',
21 content: 'function',
22 };
23
24 const isValid = Object.keys(tabPropTypes).reduce((acc, key) => {
25 if (typeof tab[key] !== tabPropTypes[key]) {
26 /* eslint-disable-next-line */
27 console.error(`Error registering tab! The '${key}' property must be a ${tabPropTypes[key]}.`);
28 return false;
29 } else if (acc === false) {
30 return false;
31 }
32 return true;
33 });
34
35 return isValid;
36 };
37
38 export const getWindowData = (value) => {
39 const data = window.giveDonorDashboardData;
40 return data[value];
41 };
42
43 export const getQueryParam = (param) => {
44 const urlParams = new URLSearchParams(window.location.search);
45 return urlParams.get(param);
46 };
47
48 export const isLoggedIn = () => {
49 return Number(getWindowData('id')) !== 0 ? true : false;
50 };
51
52 const DONOR_DASHBOARD_NAMESPACE = '/give-api/v2/donor-dashboard/';
53
54 /**
55 * Posts to a Donor Dashboard REST route and resolves with the parsed response body.
56 *
57 * FormData is sent as-is so the browser can set its own multipart boundary. Anything
58 * else is sent as JSON.
59 *
60 * @since 4.16.8
61 *
62 * @param {string} endpoint Route relative to the Donor Dashboard namespace.
63 * @param {Object|FormData} data Request payload.
64 * @return {Promise<Object>} Parsed response body.
65 */
66 export const donorDashboardApi = {
67 post: (endpoint, data) =>
68 apiFetch({
69 path: DONOR_DASHBOARD_NAMESPACE + endpoint,
70 method: 'POST',
71 ...(data instanceof window.FormData ? {body: data} : {data: data || {}}),
72 }),
73 };
74
75 /**
76 * Translates a rejected apiFetch request into a message safe to show a donor.
77 *
78 * apiFetch rejects with the parsed REST error body for a response the server actually produced,
79 * and otherwise with a client-side object carrying only a code and a message - offline, or a
80 * response that was not JSON because PHP failed partway through. Only the first kind describes
81 * something a donor can act on, so it is the only kind whose message is shown; everything else,
82 * server faults included, falls back to the generic message.
83 *
84 * @since 4.16.8
85 *
86 * @param {Object} error Rejection value from apiFetch.
87 * @return {string} Message to display.
88 */
89 export const getApiErrorMessage = (error) => {
90 const status = error && error.data ? error.data.status : null;
91
92 if (!status || status >= 500 || !error.message) {
93 return __(
94 'An error occurred while processing your request. Please try again later, or contact support if the issue persists.',
95 'give'
96 );
97 }
98
99 return error.message;
100 };
101
102 /**
103 * Returns string in Kebab Case (ex: kebab-case)
104 *
105 * @param {string} str String to be returned in Kebab Case
106 * @return {string} String returned in Kebab Case
107 * @since 2.8.0
108 */
109 export const toKebabCase = (str) => {
110 return str
111 .replace(' / ', ' ')
112 .replace(/([a-z])([A-Z])/g, '$1-$2')
113 .replace(/\s+/g, '-')
114 .toLowerCase();
115 };
116
117 /**
118 * Returns a unique id in kebab case for components
119 *
120 * @param {string} str String to be returned as unique id
121 * @return {string} String returned as unique id
122 * @since 2.8.0
123 */
124 export const toUniqueId = (str) => {
125 const prefix = str ? str : 'component';
126 return toKebabCase(`${prefix}-${Math.floor(Math.random() * Math.floor(1000))}`);
127 };
128