// © 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
?
Need to change something? Welcome to the settings page!
{validity.message}