import React, { useState, useEffect } from 'react'; import * as BS from 'react-bootstrap' import { useForm } from "react-hook-form"; import autosize from 'autosize'; import moment from 'moment'; import 'moment-timezone'; import Popup from 'reactjs-popup'; import API_URL from "../Constants/constants.js"; import defaultAvatar from '../default_discord_avatar.png' import { FaAddressCard } from "react-icons/fa"; export default function System(props) { const { register: registerEdit, handleSubmit: handleSubmitEdit } = useForm(); const { register: registerPrivacy, handleSubmit: handleSubmitPrivacy } = useForm(); const [ user, setUser ] = useState(JSON.parse(localStorage.getItem('user'))); const [ tag, setTag ] = useState(""); const [ timezone, setTimezone ] = useState(""); const [ avatar, setAvatar ] = useState(""); const [ desc, setDesc ] = useState(""); const [ editDesc, setEditDesc ] = useState(""); const [ editMode, setEditMode ] = useState(false); const [ privacyMode, setPrivacyMode ] = useState(false); const [ privacyView, setPrivacyView ] = useState(false); const [ invalidTimezone, setInvalidTimezone ] = useState(false); const [ errorAlert, setErrorAlert ] = useState(false); useEffect(() => { const { toHTML } = require('../Functions/discord-parser.js'); if (user.tag) { setTag(user.tag); } else setTag(''); if (user.tz) { setTimezone(user.tz); } else setTimezone(''); if (user.avatar_url) { setAvatar(user.avatar_url) } else setAvatar('') if (user.description) { setDesc(toHTML(user.description)); setEditDesc(user.description); } else { setDesc("(no description)"); setEditDesc(""); }}, [user.description, user.tag, user.avatar_url, user.tz]); useEffect(() => { autosize(document.querySelector('textarea')); }) const submitEdit = data => { if (data.tz) { if (!moment.tz.zone(data.tz)) { setInvalidTimezone(true); return; } } fetch(`${API_URL}s`,{ method: 'PATCH', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', 'Authorization': JSON.stringify(localStorage.getItem("token")).slice(1, -1) }}).then (res => res.json() ).then ( () => { setUser(prevState => {return {...prevState, ...data}}); localStorage.setItem('user', JSON.stringify(user)); setEditMode(false)} ).catch (error => { console.error(error); setErrorAlert(true); }) } const submitPrivacy = data => { fetch(`${API_URL}s`,{ method: 'PATCH', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', 'Authorization': JSON.stringify(localStorage.getItem("token")).slice(1, -1) }}).then (res => res.json() ).then (data => { setUser(prevState => {return {...prevState, ...data}}); localStorage.setItem('user', JSON.stringify(user)); setPrivacyMode(false)} ).catch (error => { console.error(error); setErrorAlert(true); }) } return ( {user.name} { user.avatar_url ? } className="avatar" modal> {close => (
close()}>
)}
: }
{ errorAlert ? Something went wrong, please try logging in and out again. : "" } { editMode ? Note: if you refresh the page, the old data might show up again, this is due to the bot caching data.
Try editing a member to clear the cache, or wait a few minutes before refreshing.
Name: Tag: Timezone: { invalidTimezone ? Please enter a valid timezone : "" } Avatar url: Description: setEditMode(false)}>Cancel Submit
: <> ID: {user.id} Tag: {tag} Timezone: {timezone} { privacyView ? "" : Privacy: setPrivacyView(true)}>View } { privacyMode ?
Editing privacy settings
Description: Member list: Front: Front history: setPrivacyMode(false)}>Cancel Submit
: privacyView ? <>
Viewing privacy settings
Description: {user.description_privacy} Member list: {user.member_list_privacy} Front: {user.front_privacy} Front history: {user.front_history_privacy} setPrivacyView(false)}>Exit setPrivacyMode(true)}>Edit
: "" }

Description:

{ privacyMode ? "" : privacyView ? "" : setEditMode(true)}>Edit} }
) }