one-click-accessibility/modules/settings/assets/js/api/index.js
Nirbhay Singh cd6a7e854e
[APP-908] Accessibility generator (#131)
* add: accessibility statement radio icons

* update: add form group on radio buttons

* add: statement generator

* add: statement generator

* add: accessibility statement data option

* update: create page in WordPress and save it to the option

* update: exclude zip file from the git

* update: render statement page conditionally

* add: statement link layout and settings

* add: preload statement data

* update: publish the created page and add link for it

* update: changed Dynamic Script Loader to WidgetLoader

* add: accessibility statement url

* update: text and styling

* update: styling of the preview text

* update: restructure statement generator

* add: support for dynamic update in statement links

* update: remove index.css file for widget loader

* add: widget styling for settings page

* add: empty link when hide link is enabled

* update: statement page structure and logic

* fix: typo

* update: convert component into a styled component

* fix: styling and layout

* update: icons

* update: convert radio buttons to styled component

* fix: typo and style

* add: fading for the link preview

* update: import

* update: styling and spacing

* fix: sidebar layout

* update: wpcs to latest version

* fix: spacing

* fix: wpcs version

* add: check for valid statement page

* fix: jitters on rendering

* fix: use escape attribute

* update: settings menu slug and plugin name

* fix: menu item rendering

* update: definition of the styled text field

* add: addPage function to the API

* fix: add notification on page creation

* add: copy link icon

* update: optimize SVGs
2025-01-13 09:09:55 +02:00

149 lines
2.8 KiB
JavaScript

import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import APIError from './exceptions/APIError';
const wpV2Prefix = '/wp/v2';
const v1Prefix = '/ea11y/v1';
class API {
static async request({ path, data, method = 'POST' }) {
try {
if ('GET' === method && !path.startsWith(wpV2Prefix)) {
path = addQueryArgs(path, { sb_time: new Date().getTime() });
}
const response = await apiFetch({
path,
method,
data,
});
if (path.startsWith(wpV2Prefix)) {
return response;
}
if (!response.success) {
throw new APIError(response.data.message);
}
return response.data;
} catch (e) {
if (e instanceof APIError) {
throw e;
} else {
throw new APIError(e.message);
}
}
}
static async initConnect(context = 'new') {
const data = {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
};
if ('update' === context) {
data.update_redirect_uri = true;
}
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/authorize`,
data,
});
}
static async clearSession() {
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/deactivate_and_disconnect`,
data: {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
clear_session: true,
},
});
}
static async deactivateAndDisconnect() {
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/deactivate_and_disconnect`,
data: {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
},
});
}
static async deactivate() {
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/deactivate`,
data: {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
},
});
}
static async disconnect() {
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/disconnect`,
data: {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
},
});
}
static async reconnect() {
return API.request({
method: 'POST',
path: `${v1Prefix}/connect/reconnect`,
data: {
wp_rest: window?.ea11ySettingsData?.wpRestNonce,
},
});
}
static async getSettings() {
return API.request({
method: 'GET',
path: `${wpV2Prefix}/settings`,
});
}
static async updateSettings(data) {
return API.request({
method: 'PUT',
path: `${wpV2Prefix}/settings`,
data,
});
}
static async addPage(data) {
return API.request({
method: 'POST',
path: `${wpV2Prefix}/pages`,
data,
});
}
/**
* @return {Promise<any>} {}
*/
static async getPluginSettings() {
return API.request({
method: 'GET',
path: `${v1Prefix}/settings/get-settings`,
});
}
/**
* Go to connect service
* @return {Promise<void>} redirect
*/
static async redirectToConnect() {
const link = await this.initConnect();
window.open(link, '_self').focus();
}
}
export default API;