Imported the directory structure from egg trainer
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import React, { useRef, useContext } from 'react';
|
||||
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
|
||||
const BanUser = props => {
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//ref
|
||||
const usernameRef = useRef();
|
||||
|
||||
return (
|
||||
<div className='panel'>
|
||||
<h2 className='text centered'>Permanently Ban User</h2>
|
||||
<form className='constrained'>
|
||||
<input type='text' name='username' placeholder='Username' ref={usernameRef} />
|
||||
|
||||
<button type='button' onClick={async evt => {
|
||||
evt.preventDefault();
|
||||
const yes = confirm('Permanently ban this user from the website?');
|
||||
|
||||
if (!yes) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [err, result] = await handleButtonPress(usernameRef.current.value, authTokens.tokenFetch);
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
usernameRef.current.value = '';
|
||||
}
|
||||
}}>Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleButtonPress = async (username, tokenFetch) => {
|
||||
const result = await tokenFetch(`${process.env.AUTH_URI}/admin/banuser`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username
|
||||
})
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
return [err, false];
|
||||
}
|
||||
|
||||
return [null, true];
|
||||
};
|
||||
|
||||
export default BanUser;
|
||||
@@ -0,0 +1,71 @@
|
||||
import React, { useState, useEffect, useContext } from 'react';
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
import dateFormat from 'dateformat';
|
||||
|
||||
const ChatReports = props => {
|
||||
const [reports, setReports] = useState([]);
|
||||
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
useEffect(async () => {
|
||||
const result = await authTokens.tokenFetch(`${process.env.CHAT_URI}/admin/reports`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
alert(err);
|
||||
} else {
|
||||
setReports(await result.json());
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className='panel' style={{minWidth: '100%'}}>
|
||||
<h2 className='text centered'>Chat Reports</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Username</th>
|
||||
<th className='mobile hide'>Room Name</th>
|
||||
<th>Content</th>
|
||||
<th>Reported By</th>
|
||||
<th className='mobile hide'>Delete</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{reports.map((report, index) => (
|
||||
<tr key={index}>
|
||||
<td className='text centered'>{dateFormat(report.chatlog.createdAt, 'yyyy-mm-dd, H:MM:ss')}</td>
|
||||
<td className='text centered'>{report.chatlog.username}</td>
|
||||
<td className='text mobile hide centered'>{report.chatlog.room}</td>
|
||||
<td className='text centered'>{report.chatlog.text}</td>
|
||||
<td className='text centered'>{report.reporter.join(', ')}</td>
|
||||
<td className='text mobile hide centered'><button onClick={() => deleteReportsFor(report.chatlogIndex, authTokens.tokenFetch, setReports)}>Delete</button></td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const deleteReportsFor = (chatlogIndex, tokenFetch, setReports) => {
|
||||
tokenFetch(`${process.env.CHAT_URI}/admin/reports`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({ chatlogIndex })
|
||||
});
|
||||
|
||||
setReports(reports => reports.filter(report => report.chatlogIndex != chatlogIndex));
|
||||
};
|
||||
|
||||
export default ChatReports;
|
||||
@@ -0,0 +1,71 @@
|
||||
import React, { useRef, useContext } from 'react';
|
||||
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
|
||||
const GrantAdmin = props => {
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//ref
|
||||
const usernameRef = useRef();
|
||||
|
||||
return (
|
||||
<div className='panel'>
|
||||
<h2 className='text centered'>Grant Admin Privileges</h2>
|
||||
<form className='constrained'>
|
||||
<input type='text' name='username' placeholder='Username' ref={usernameRef} />
|
||||
|
||||
<button type='button' onClick={async evt => {
|
||||
evt.preventDefault();
|
||||
const [err, result] = await handleButtonPress(usernameRef.current.value, authTokens.tokenFetch, 'POST');
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
alert('admin set');
|
||||
usernameRef.current.value = '';
|
||||
}
|
||||
}}>Submit</button>
|
||||
|
||||
<button type='button' onClick={async evt => {
|
||||
evt.preventDefault();
|
||||
const [err, result] = await handleButtonPress(usernameRef.current.value, authTokens.tokenFetch, 'DELETE');
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
alert('admin removed');
|
||||
usernameRef.current.value = '';
|
||||
}
|
||||
}}>Remove</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleButtonPress = async (username, tokenFetch, method) => {
|
||||
const result = await tokenFetch(`${process.env.AUTH_URI}/admin/admin`, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username
|
||||
})
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
return [err, false];
|
||||
}
|
||||
|
||||
return [null, true];
|
||||
};
|
||||
|
||||
export default GrantAdmin;
|
||||
@@ -0,0 +1,71 @@
|
||||
import React, { useRef, useContext } from 'react';
|
||||
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
|
||||
const GrantMod = props => {
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//ref
|
||||
const usernameRef = useRef();
|
||||
|
||||
return (
|
||||
<div className='panel'>
|
||||
<h2 className='text centered'>Grant Moderation Privileges</h2>
|
||||
<form className='constrained'>
|
||||
<input type='text' name='username' placeholder='Username' ref={usernameRef} />
|
||||
|
||||
<button type='button' onClick={async evt => {
|
||||
evt.preventDefault();
|
||||
const [err, result] = await handleButtonPress(usernameRef.current.value, authTokens.tokenFetch, 'POST');
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
alert('mod set');
|
||||
usernameRef.current.value = '';
|
||||
}
|
||||
}}>Submit</button>
|
||||
|
||||
<button type='button' onClick={async evt => {
|
||||
evt.preventDefault();
|
||||
const [err, result] = await handleButtonPress(usernameRef.current.value, authTokens.tokenFetch, 'DELETE');
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
alert('mod removed');
|
||||
usernameRef.current.value = '';
|
||||
}
|
||||
}}>Remove</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleButtonPress = async (username, tokenFetch, method) => {
|
||||
const result = await tokenFetch(`${process.env.AUTH_URI}/admin/mod`, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username
|
||||
})
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
return [err, false];
|
||||
}
|
||||
|
||||
return [null, true];
|
||||
};
|
||||
|
||||
export default GrantMod;
|
||||
@@ -0,0 +1,146 @@
|
||||
import React, { useState, useEffect, useContext, useRef } from 'react';
|
||||
import Select from 'react-dropdown-select';
|
||||
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
|
||||
const NewsEditor = props => {
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//refs
|
||||
const titleRef = useRef();
|
||||
const authorRef = useRef();
|
||||
const bodyRef = useRef();
|
||||
|
||||
//state
|
||||
const [articles, setArticles] = useState([]);
|
||||
const [index, setIndex] = useState(null);
|
||||
|
||||
//run once
|
||||
useEffect(async () => {
|
||||
const result = await fetch(`${process.env.NEWS_URI}/news/metadata?limit=999`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
alert(err);
|
||||
} else {
|
||||
setArticles(await result.json());
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className='panel'>
|
||||
<h2 className='text centered'>News Editor</h2>
|
||||
<Select
|
||||
options={articles.map(article => { return { label: article.title, value: article.index }; })}
|
||||
onChange={async values => {
|
||||
//fetch this article
|
||||
const index = values[0].value;
|
||||
|
||||
const result = await fetch(`${process.env.NEWS_URI}/news/archive/${index}`, {
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
}
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
console.log(err);
|
||||
alert(err);
|
||||
} else {
|
||||
const article = await result.json();
|
||||
titleRef.current.value = article.title;
|
||||
authorRef.current.value = article.author;
|
||||
bodyRef.current.value = article.body;
|
||||
setIndex(index);
|
||||
}
|
||||
}}
|
||||
placeholder='Select Article'
|
||||
/>
|
||||
|
||||
<form className='constrained' onSubmit={async evt => {
|
||||
//onSubmit
|
||||
evt.preventDefault();
|
||||
const [err] = await handleSubmit(titleRef.current.value, authorRef.current.value, bodyRef.current.value, index, authTokens.tokenFetch);
|
||||
if (err) {
|
||||
alert(err);
|
||||
} else {
|
||||
titleRef.current.value = authorRef.current.value = bodyRef.current.value = '';
|
||||
alert(`Edited as article index ${index}`);
|
||||
}
|
||||
}}>
|
||||
<input type='text' name='title' placeholder='Title' ref={titleRef} />
|
||||
<input type='text' name='author' placeholder='Author' ref={authorRef} />
|
||||
<textarea name='body' rows='10' cols='150' placeholder='Body of the article goes here...' ref={bodyRef} />
|
||||
|
||||
<button type='submit'>Update</button>
|
||||
<button type='button' onClick={async evt => {
|
||||
//onDelete
|
||||
const [err, result] = await handleDelete(index, authTokens.tokenFetch);
|
||||
|
||||
if (err) {
|
||||
alert(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (result) {
|
||||
titleRef.current.value = authorRef.current.value = bodyRef.current.value = '';
|
||||
alert(`Article deleted`);
|
||||
}
|
||||
}}>Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleSubmit = async (title, author, body, index, tokenFetch) => {
|
||||
title = title.trim();
|
||||
author = author.trim();
|
||||
body = body.trim();
|
||||
|
||||
//fetch POST json data
|
||||
const result = await tokenFetch(`${process.env.NEWS_URI}/news/${index}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
author,
|
||||
body
|
||||
})
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
return [`${result.status}: ${await result.text()}`];
|
||||
}
|
||||
|
||||
return [null];
|
||||
};
|
||||
|
||||
const handleDelete = async (index, tokenFetch) => {
|
||||
const conf = confirm('Are you sure you want to delete this article?');
|
||||
|
||||
if (conf) {
|
||||
const result = await tokenFetch(`${process.env.NEWS_URI}/news/${index}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (!result.ok) {
|
||||
const err = `${result.status}: ${await result.text()}`;
|
||||
return [err, false];
|
||||
}
|
||||
}
|
||||
|
||||
return [null, conf];
|
||||
};
|
||||
|
||||
export default NewsEditor;
|
||||
@@ -0,0 +1,69 @@
|
||||
import React, { useContext, useRef } from 'react';
|
||||
|
||||
import { TokenContext } from '../../utilities/token-provider';
|
||||
|
||||
const NewsPublisher = props => {
|
||||
//context
|
||||
const authTokens = useContext(TokenContext);
|
||||
|
||||
//refs
|
||||
const titleRef = useRef();
|
||||
const authorRef = useRef();
|
||||
const bodyRef = useRef();
|
||||
|
||||
return (
|
||||
<div className='panel'>
|
||||
<h2 className='text centered'>News Publisher</h2>
|
||||
<form className='constrained' onSubmit={async evt => {
|
||||
//on submit
|
||||
evt.preventDefault();
|
||||
const [err, index] = await handleSubmit(titleRef.current.value, authorRef.current.value, bodyRef.current.value, authTokens.tokenFetch);
|
||||
if (err) {
|
||||
alert(err);
|
||||
} else {
|
||||
titleRef.current.value = authorRef.current.value = bodyRef.current.value = ''; //TODO: null bug here?
|
||||
alert(`Published as article index ${index}`);
|
||||
}
|
||||
}}>
|
||||
<input type='text' name='title' placeholder='Title' ref={titleRef} />
|
||||
<input type='text' name='author' placeholder='Author' ref={authorRef} />
|
||||
<textarea name='body' rows='10' cols='150' placeholder='Body of the article goes here...' ref={bodyRef} />
|
||||
|
||||
<button type='submit'>Publish</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleSubmit = async (title, author, body, tokenFetch) => {
|
||||
title = title.trim();
|
||||
author = author.trim();
|
||||
body = body.trim();
|
||||
|
||||
//fetch POST json data
|
||||
const result = await tokenFetch(
|
||||
`${process.env.NEWS_URI}/news`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
author,
|
||||
body
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
if (!result.ok) {
|
||||
return [`${result.status}: ${await result.text()}`];
|
||||
}
|
||||
|
||||
const json = await result.json();
|
||||
|
||||
return [null, json.index];
|
||||
};
|
||||
|
||||
export default NewsPublisher;
|
||||
Reference in New Issue
Block a user