import React, { Component } from "react"; import logo from "./assets/adrecord.svg"; import "./App.css"; import styled from "styled-components"; import Toggle from "react-toggle-component"; import "react-toggle-component/styles.css"; import { convertFormDataToArray, convertFormData } from "./utils/form"; import { debounce } from "./utils/debounce"; import { BASE_URL, REST_NONCE, INITIAL_APIKEY, INITIAL_CLEAN_LINKS, PLUGIN_URL, IMAGE_DIR, HELP_API_KEY_LINK, translate } from "./globals"; const adrecord_url = "https://api.v2.adrecord.com"; class SettingsScreen extends Component { state = { user: null, loading: false, checked: INITIAL_CLEAN_LINKS }; constructor() { super(); this.apikeyRef = React.createRef(); } componentDidMount() { if (INITIAL_APIKEY) { this.fetchUser(INITIAL_APIKEY); } } async saveApiKey(api_key) { let response = await fetch(BASE_URL + "/set_api_key", { method: "post", credentials: "include", headers: { "X-WP-Nonce": REST_NONCE }, body: JSON.stringify({ api_key, user_id: this.state.user ? this.state.user.userID : "", nonce: REST_NONCE }) }); let json = await response.json(); // console.log("response: ", json); } async setCleanLinks(clean_links_enabled) { let response = await fetch(BASE_URL + "/set_clean_links_enabled", { method: "post", credentials: "include", headers: { "X-WP-Nonce": REST_NONCE }, body: JSON.stringify({ clean_links_enabled, nonce: REST_NONCE }) }); let json = await response.json(); console.log("response: ", json); } async fetchUser(api_key) { this.setState({ loading: true }); try { let response = await fetch(`${adrecord_url}/user?apikey=${api_key}`); let json = await response.json(); if (json.userID) { //Got a valid user. this.setState({ user: json, loading: false }); return true; } else { this.setState({ user: null }); } } catch (e) { this.setState({ loading: false, user: null }); } return false; } handleAPIKeyChange = debounce(async e => { const api_key = this.apikeyRef.current.value; let success = await this.fetchUser(api_key); if (success) { this.saveApiKey(api_key); } }, 300); render() { const { user, loading } = this.state; let incorrectUserId = false; if ( !loading && !user && (this.apikeyRef.current && this.apikeyRef.current.value.length) ) { incorrectUserId = true; } return (
{translate("about_text_1")}
{translate("about_text_2")}
{translate("get_started_text_1")} {translate("get_started_link_1")}
{translate("get_started_text_2")} {translate("get_started_link_2")}