2020-12-09 09:15:55 +01:00
import React , { useState , useEffect } from 'react' ;
import * as BS from 'react-bootstrap'
2020-12-12 19:19:22 +01:00
import { useForm } from "react-hook-form" ;
2020-12-11 16:35:25 +01:00
import autosize from 'autosize' ;
import moment from 'moment' ;
import 'moment-timezone' ;
import API _URL from "../Constants/constants.js" ;
2020-12-09 09:15:55 +01:00
import defaultAvatar from '../default_discord_avatar.png'
2020-12-11 16:35:25 +01:00
import { FaAddressCard } from "react-icons/fa" ;
2020-12-09 09:15:55 +01:00
export default function System ( props ) {
2020-12-12 19:19:22 +01:00
const {
register : registerEdit ,
handleSubmit : handleSubmitEdit
} = useForm ( ) ;
const {
register : registerPrivacy ,
handleSubmit : handleSubmitPrivacy
} = useForm ( ) ;
2020-12-11 16:35:25 +01:00
const [ user , setUser ] = useState ( JSON . parse ( localStorage . getItem ( 'user' ) ) ) ;
const [ tag , setTag ] = useState ( "" ) ;
const [ timezone , setTimezone ] = useState ( "" ) ;
const [ avatar , setAvatar ] = useState ( "" ) ;
2020-12-09 09:15:55 +01:00
const [ desc , setDesc ] = useState ( "" ) ;
2020-12-11 16:35:25 +01:00
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 ) ;
2020-12-09 09:15:55 +01:00
useEffect ( ( ) => {
2020-12-11 00:53:43 +01:00
const { toHTML } = require ( '../Functions/discord-parser.js' ) ;
2020-12-11 16:35:25 +01:00
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 ( '' )
2020-12-11 00:53:43 +01:00
2020-12-09 09:15:55 +01:00
if ( user . description ) {
setDesc ( toHTML ( user . description ) ) ;
2020-12-11 16:35:25 +01:00
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 ( data => { 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 ) ;
} )
}
2020-12-09 09:15:55 +01:00
return (
< BS . Card className = "mb-3 mt-3 w-100" >
< BS . Card . Header className = "d-flex align-items-center justify-content-between" >
< BS . Card . Title className = "float-left" > < FaAddressCard className = "mr-3" / > { user . name } < / B S . C a r d . T i t l e >
{ user . avatar _url ? < BS . Image src = { ` ${ user . avatar _url } ` } style = { { width : 50 , height : 50 } } className = "float-right" roundedCircle / > :
< BS . Image src = { defaultAvatar } style = { { width : 50 , height : 50 } } className = "float-right" roundedCircle / > }
< / B S . C a r d . H e a d e r >
< BS . Card . Body >
2020-12-11 16:35:25 +01:00
{ errorAlert ? < BS . Alert variant = "danger" > Something went wrong , please try logging in and out again . < / B S . A l e r t > : " " }
{ editMode ?
2020-12-12 19:19:22 +01:00
< BS . Form onSubmit = { handleSubmitEdit ( submitEdit ) } >
2020-12-11 16:35:25 +01:00
{ /* <BS.Form.Text className='mb-4'>Note: changes here may take a while to be reflected on the bot. This is due to the bot caching data.<br/> Try editing a member after to make the changes show up.</BS.Form.Text> */ }
< BS . Form . Row >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Name : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "name" ref = { registerEdit } defaultValue = { user . name } / >
2020-12-11 16:35:25 +01:00
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Tag : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "tag" ref = { registerEdit } defaultValue = { tag } / >
2020-12-11 16:35:25 +01:00
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Timezone : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "tz" ref = { registerEdit } defaultValue = { timezone } / >
2020-12-11 16:35:25 +01:00
{ invalidTimezone ? < BS . Form . Text > Please enter a valid < a href = 'https://xske.github.io/tz/' rel = "noreferrer" target = "_blank" > timezone < / a > < / B S . F o r m . T e x t > : " " }
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Avatar url : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "avatar_url" ref = { registerEdit } defaultValue = { avatar } / >
2020-12-11 16:35:25 +01:00
< / B S . C o l >
< / B S . F o r m . R o w >
< BS . Form . Group className = "mt-3" >
< BS . Form . Label > Description : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control maxLength = "1000" as = "textarea" name = "description" ref = { registerEdit } defaultValue = { editDesc } / >
2020-12-11 16:35:25 +01:00
< / B S . F o r m . G r o u p >
< BS . Button variant = "light" onClick = { ( ) => setEditMode ( false ) } > Cancel < / B S . B u t t o n > < B S . B u t t o n v a r i a n t = " p r i m a r y " t y p e = " s u b m i t " > S u b m i t < / B S . B u t t o n >
< / B S . F o r m > :
< > < BS . Row >
2020-12-09 17:45:11 +01:00
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > ID : < / b > { u s e r . i d } < / B S . C o l >
2020-12-11 16:35:25 +01:00
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Tag : < / b > { t a g } < / B S . C o l >
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Timezone : < / b > { t i m e z o n e } < / B S . C o l >
{ privacyView ? "" : < BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Privacy : < / b > < B S . B u t t o n v a r i a n t = " l i g h t " s i z e = " s m " o n C l i c k = { ( ) = > s e t P r i v a c y V i e w ( t r u e ) } > V i e w < / B S . B u t t o n > < / B S . C o l > }
2020-12-09 09:15:55 +01:00
< / B S . R o w >
2020-12-12 19:19:22 +01:00
{ privacyMode ? < BS . Form onSubmit = { handleSubmitPrivacy ( submitPrivacy ) } >
2020-12-11 16:35:25 +01:00
< hr / >
< h5 > Editing privacy settings < / h 5 >
< BS . Form . Row >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Description : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "description_privacy" as = "select" ref = { registerPrivacy } >
2020-12-11 16:35:25 +01:00
< option > public < / o p t i o n >
< option > private < / o p t i o n >
< / B S . F o r m . C o n t r o l >
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Member list : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "member_list_privacy" as = "select" ref = { registerPrivacy } >
2020-12-11 16:35:25 +01:00
< option > public < / o p t i o n >
< option > private < / o p t i o n >
< / B S . F o r m . C o n t r o l >
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Front : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "front_privacy" as = "select" ref = { registerPrivacy } >
2020-12-11 16:35:25 +01:00
< option > public < / o p t i o n >
< option > private < / o p t i o n >
< / B S . F o r m . C o n t r o l >
< / B S . C o l >
< BS . Col className = "mb-lg-2" xs = { 12 } lg = { 3 } >
< BS . Form . Label > Front history : < / B S . F o r m . L a b e l >
2020-12-12 19:19:22 +01:00
< BS . Form . Control name = "front_history_privacy" as = "select" ref = { registerPrivacy } >
2020-12-11 16:35:25 +01:00
< option > public < / o p t i o n >
< option > private < / o p t i o n >
< / B S . F o r m . C o n t r o l >
< / B S . C o l >
< / B S . F o r m . R o w >
< BS . Button variant = "light" onClick = { ( ) => setPrivacyMode ( false ) } > Cancel < / B S . B u t t o n > < B S . B u t t o n v a r i a n t = " p r i m a r y " t y p e = " s u b m i t " > S u b m i t < / B S . B u t t o n >
< hr / >
< /BS.Form> : privacyView ? <><hr/ >
< h5 > Viewing privacy settings < / h 5 >
< BS . Row >
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Description : < / b > { u s e r . d e s c r i p t i o n _ p r i v a c y } < / B S . C o l >
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Member list : < / b > { u s e r . m e m b e r _ l i s t _ p r i v a c y } < / B S . C o l >
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Front : < / b > { u s e r . f r o n t _ p r i v a c y } < / B S . C o l >
< BS . Col className = "mb-lg-3" xs = { 12 } lg = { 3 } > < b > Front history : < / b > { u s e r . f r o n t _ h i s t o r y _ p r i v a c y } < / B S . C o l >
< / B S . R o w >
< BS . Button variant = "light" onClick = { ( ) => setPrivacyView ( false ) } > Exit < / B S . B u t t o n > < B S . B u t t o n v a r i a n t = " p r i m a r y " o n C l i c k = { ( ) = > s e t P r i v a c y M o d e ( t r u e ) } > E d i t < / B S . B u t t o n >
< hr / > < / > : " " }
2020-12-09 09:15:55 +01:00
< p > < b > Description : < / b > < / p >
< p dangerouslySetInnerHTML = { { _ _html : desc } } > < / p >
2020-12-11 16:35:25 +01:00
{ privacyMode ? "" : privacyView ? "" : < BS . Button variant = "light" onClick = { ( ) => setEditMode ( true ) } > Edit < /BS.Button>}</ > }
2020-12-09 09:15:55 +01:00
< / B S . C a r d . B o d y >
< / B S . C a r d >
)
}