From a6459ced9ff3f19dbf553ed3ab1a9b46bbeb06bd Mon Sep 17 00:00:00 2001 From: Tachis Alopex Date: Sat, 13 Aug 2022 20:52:49 -0600 Subject: [PATCH 1/2] Add the ability to login to e621 --- src/features/gameboard/types.ts | 5 + .../PornSetting/PornSetting.css | 6 +- .../PornSetting/PornSetting.tsx | 108 +++++++++++++++++- .../SaveSetting/SaveSetting.tsx | 2 +- .../SettingsControls/SettingsControls.tsx | 12 +- src/features/settings/store/actions.ts | 8 +- src/features/settings/store/reducer.ts | 10 +- src/features/settings/types.ts | 69 +++++++++++ src/helpers/saveFormat.ts | 12 +- 9 files changed, 219 insertions(+), 13 deletions(-) diff --git a/src/features/gameboard/types.ts b/src/features/gameboard/types.ts index 44042ce..4429331 100644 --- a/src/features/gameboard/types.ts +++ b/src/features/gameboard/types.ts @@ -15,6 +15,11 @@ export enum EGrip { none, } +export type Credentials = { + login: string + api_key: string +} + export type PornList = string[] export type GameEvent = ( diff --git a/src/features/settings/SettingsControls/PornSetting/PornSetting.css b/src/features/settings/SettingsControls/PornSetting/PornSetting.css index 80d28d2..48565eb 100644 --- a/src/features/settings/SettingsControls/PornSetting/PornSetting.css +++ b/src/features/settings/SettingsControls/PornSetting/PornSetting.css @@ -3,4 +3,8 @@ flex-wrap: wrap; width: 100%; margin-top: 10px; -} \ No newline at end of file +} + +.PornSetting__error { + color: #cc0000; +} diff --git a/src/features/settings/SettingsControls/PornSetting/PornSetting.tsx b/src/features/settings/SettingsControls/PornSetting/PornSetting.tsx index 373bf7d..6a764b0 100644 --- a/src/features/settings/SettingsControls/PornSetting/PornSetting.tsx +++ b/src/features/settings/SettingsControls/PornSetting/PornSetting.tsx @@ -1,14 +1,16 @@ import React from 'react' import '../settings.css' import './PornSetting.css' -import { PornList } from '../../../gameboard/types' +import { Credentials, PornList } from '../../../gameboard/types' import { E621Posts } from '../../types' -import axios, { AxiosResponse } from 'axios' +import axios, { AxiosRequestConfig, AxiosResponse } from 'axios' import { PornThumbnail } from './PornThumbnail' import { debounce } from 'lodash' import reactGA from '../../../../analytics' interface IPornSettingProps { + credentials: Credentials | null + setCredentials: (newCredentials: Credentials | null) => void pornList: PornList setPornList: (newPornList: PornList) => void } @@ -20,6 +22,9 @@ interface IPornSettingState { flags: { highRes: boolean } + credentials: Credentials + addCredentials: boolean + credentialsError: string | null } export class PornSetting extends React.Component { @@ -33,8 +38,15 @@ export class PornSetting extends React.Component) { + const login = event.target.value + this.setState(prevState => ({ + credentials: { + ...prevState.credentials!, + login, + }, + credentialsError: null, + })) + } + + updateApiKey(event: React.ChangeEvent) { + const api_key = event.target.value + this.setState(prevState => ({ + credentials: { + ...prevState.credentials!, + api_key, + }, + credentialsError: null, + })) + } + + saveCredentials() { + // Check to see if these credentials are valid + const config: AxiosRequestConfig = { + params: this.state.credentials, + responseType: 'json', + } + axios + .get(`https://e621.net/users/${this.state.credentials.login}.json`, config) + .then(() => this.props.setCredentials(this.state.credentials)) + .catch(() => this.setState({ credentialsError: 'Invalid credentials' })) + } + + clearCredentials() { + this.props.setCredentials(null) + this.setState({ addCredentials: false }) + } + downloadFromTags() { debounce(() => { if (localStorage.getItem('allowCookies') !== 'true' || localStorage.getItem('allowCookies') !== null) return @@ -54,11 +105,15 @@ export class PornSetting extends React.Component=${this.state.minScore}` : '')) axios - .get(`https://e621.net/posts.json?tags=${tags}&limit=${this.state.count}&callback=callback`, { - responseType: 'json', - }) + .get(`https://e621.net/posts.json?tags=${tags}&limit=${this.state.count}&callback=callback`, config) .then((response: AxiosResponse<{ posts: E621Posts }>) => { this.props.setPornList( (response.data.posts @@ -92,6 +147,49 @@ export class PornSetting extends React.ComponentImport from e621 +
+ {this.props.credentials ? ( + <> + +
+ Logged in. You can use votedup:me & private sets. + + ) : ( + <> + + Login to use votedup:me & private sets. + {this.state.addCredentials ? ( + <> + +
+
+ + + (found in your account under "Manage API Access") + + + {this.state.credentialsError != null ? {this.state.credentialsError} : null} + + ) : null} + + )} +