// © XlXi 2021 // Graphictoria 5 import axios from 'axios'; import React, { useEffect, useState } from "react"; import { Link, useHistory, useParams } from "react-router-dom"; import Config from '../config.js'; import SetTitle from "../Helpers/Title.js"; import Loader from '../Components/Loader.js'; import { GenericErrorModal } from './Errors.js'; import { BigCard, Card, CardTitle } from '../Components/Card.js'; import { getCookie, paginate } from '../helpers/utils.js'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; var url = Config.BaseUrl.replace('http://', ''); var protocol = Config.Protocol; const Settings = (props) => { var id = useParams().id; const [validity, setValidity] = useState({error: false, message: ``, inputs: []}); const [state, setState] = useState({offline: false, loading: true}); const user = props.user; const history = useHistory(); const changeSettings = async (setting, form) => { switch(setting){ case "aboutMe": setState({...state, loading: true}); var body = form; body.append('token', encodeURIComponent(getCookie(`gtok`))); axios.post(`${protocol}apis.${url}/api/change/user/about`, body, {headers: {'X-CSRF-TOKEN': document.querySelector(`meta[name="csrf-token"]`).content, "X-Requested-With":"XMLHttpRequest"}}).then(data=>{ const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ window.location.replace(`/user/${user.id}`); //updates entire user } setState({...state, loading: false}); }).catch(error=>{console.log(error);}); break; case "changePassword": setState({...state, loading: true}); var body = form; body.append('token', encodeURIComponent(getCookie(`gtok`))); axios.post(`${protocol}apis.${url}/api/change/user/password`, body, {headers: {'X-CSRF-TOKEN': document.querySelector(`meta[name="csrf-token"]`).content, "X-Requested-With":"XMLHttpRequest"}}).then(data=>{ const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ window.location.replace(`/home`); //updates entire user } setState({...state, loading: false}); }).catch(error=>{console.log(error);}); break; case "changeEmail": setState({...state, loading: true}); var body = form; body.append('token', encodeURIComponent(getCookie(`gtok`))); axios.post(`${protocol}apis.${url}/api/change/user/email`, body, {headers: {'X-CSRF-TOKEN': document.querySelector(`meta[name="csrf-token"]`).content, "X-Requested-With":"XMLHttpRequest"}}).then(data=>{ const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ window.location.replace(`/home`); //updates entire user } setState({...state, loading: false}); }).catch(error=>{console.log(error);}); break; default: break; } } useEffect(()=>{ /* add stuff later if necessary */ setState({...state, loading: false}); }, []); return ( state.loading ? :

Account Settings

Need to change something? Welcome to the settings page!

{validity.error?

{validity.message}

: null}
About me
{e.preventDefault();changeSettings(`aboutMe`, new FormData(e.target));}} className={`flex column jcc alc`}>
Change your Password
{e.preventDefault();changeSettings(`changePassword`, new FormData(e.target));}} className={`flex column jcc alc`}>

Current Password

input == `body`)? `is-invalid` : null)}`} name={`currentPassword`}/>

New Password

input == `body`)? `is-invalid` : null)}`} name={`newPassword`}/>

Re-enter New Password

input == `body`)? `is-invalid` : null)}`} name={`checkNewPassword`}/>
Change your Email
{e.preventDefault();changeSettings(`changeEmail`, new FormData(e.target));}} className={`flex column jcc alc`}>

New Email

input == `body`)? `is-invalid` : null)}`} name={`newEmail`}/>

Current Password

input == `body`)? `is-invalid` : null)}`} name={`currentPassword`}/>
); } export default Settings;