/*eslint max-lines: ["warn", {"max": 500, "skipBlankLines": true, "skipComments": true}]*/
require('./lockTools.less');
const React = require('react');
const createClass = require('create-react-class');
import request from '../../homebrew/utils/request-middleware.js';
const LockTools = createClass({
displayName : 'LockTools',
getInitialState : function() {
return {
fetching : false,
reviewCount : 0
};
},
componentDidMount : function() {
this.updateReviewCount();
},
updateReviewCount : async function() {
const newCount = await request.get('/api/lock/count')
.then((res)=>{return res.body?.count || 'Unknown';});
if(newCount != this.state.reviewCount){
this.setState({
reviewCount : newCount
});
}
},
updateLockData : function(lock){
this.setState({
lock : lock
});
},
render : function() {
return
Lock Count
Number of brews currently locked: {this.state.reviewCount}
REFRESH
;
}
});
const LockBrew = createClass({
displayName : 'LockBrew',
getInitialState : function() {
// Default values
return {
brewId : this.props.lock?.shareId || '',
code : this.props.lock?.code || 455,
editMessage : this.props.lock?.editMessage || '',
shareMessage : this.props.lock?.shareMessage || 'This Brew has been locked.',
result : {},
overwrite : false,
};
},
handleChange : function(e, varName) {
const output = {};
output[varName] = e.target.value;
this.setState(output);
},
submit : function(e){
e.preventDefault();
if(!this.state.editMessage) return;
const newLock = {
overwrite : this.state.overwrite,
code : parseInt(this.state.code) || 100,
editMessage : this.state.editMessage,
shareMessage : this.state.shareMessage,
applied : new Date
};
request.post(`/api/lock/${this.state.brewId}`)
.send(newLock)
.set('Content-Type', 'application/json')
.then((response)=>{
this.setState({ result: response.body });
})
.catch((err)=>{
this.setState({ result: err.response.body });
});
},
renderInput : function (name) {
return this.handleChange(e, name)} autoComplete='off' required/>;
},
renderResult : function(){
return <>
Result:
{Object.keys(this.state.result).map((key, idx)=>{
return
{key}
{this.state.result[key].toString()}
;
})}
>;
},
render : function() {
return
Lock Brew
{this.state.result && this.renderResult()}
Suggestions
Codes
455 - Generic Lock
456 - Copyright issues
457 - Confidential Information Leakage
458 - Sensitive Personal Information
459 - Defamation or Libel
460 - Hate Speech or Discrimination
461 - Illegal Activities
462 - Malware or Phishing
463 - Plagiarism
465 - Misrepresentation
466 - Inappropriate Content
Messages
Private Message: This is the private message that is ONLY displayed to the authors of the locked brew. This message MUST specify exactly what actions must be taken in order to have the brew unlocked.
Public Message: This is the public message that is displayed to the EVERYONE that attempts to view the locked brew.
;
}
});
const LockTable = createClass({
displayName : 'LockTable',
getDefaultProps : function() {
return {
title : '',
text : '',
fetchURL : '/api/locks',
resultName : '',
propertyNames : ['shareId'],
loadBrew : ()=>{}
};
},
getInitialState : function() {
return {
result : '',
error : '',
searching : false
};
},
lockKey : React.createRef(0),
clickFn : function (){
this.setState({ searching: true, error: null });
request.get(this.props.fetchURL)
.then((res)=>this.setState({ result: res.body }))
.catch((err)=>this.setState({ result: err.response.body }))
.finally(()=>{
this.setState({ searching: false });
});
},
updateBrewLockData : function (lockData){
this.lockKey.current++;
const brewData = {
key : this.lockKey.current,
shareId : lockData.shareId,
code : lockData.lock.code,
editMessage : lockData.lock.editMessage,
shareMessage : lockData.lock.shareMessage
};
this.props.loadBrew(brewData);
},
render : function () {
return <>
{this.props.title}
REFRESH
{this.state.result[this.props.resultName] &&
<>
{this.props.text}: {this.state.result[this.props.resultName].length}
{this.props.propertyNames.map((name, idx)=>{
return {name} ;
})}
clip
load
{this.state.result[this.props.resultName].map((result, resultIdx)=>{
return
{this.props.propertyNames.map((name, nameIdx)=>{
return
{result[name].toString()}
;
})}
{navigator.clipboard.writeText(result.shareId.toString());}}>
{this.updateBrewLockData(result);}}>
;
})}
>
}
>;
}
});
const LockLookup = createClass({
displayName : 'LockLookup',
getDefaultProps : function() {
return {
fetchURL : '/api/lookup'
};
},
getInitialState : function() {
return {
query : '',
result : '',
error : '',
searching : false
};
},
handleChange(e){
this.setState({ query: e.target.value });
},
clickFn(){
this.setState({ searching: true, error: null });
request.put(`${this.props.fetchURL}/${this.state.query}`)
.then((res)=>this.setState({ result: res.body }))
.catch((err)=>this.setState({ result: err.response.body }))
.finally(()=>{
this.setState({ searching: false });
});
},
renderResult : function(){
return
Result:
{Object.keys(this.state.result).map((key, idx)=>{
return
{key}
{this.state.result[key].toString()}
;
})}
;
},
render : function() {
return
{this.props.title}
{this.state.error
&&
{this.state.error.toString()}
}
{this.state.result && this.renderResult()}
;
}
});
module.exports = LockTools;