This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
generated from WebJamApps/CollegeLutheran
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminController.tsx
75 lines (67 loc) · 2.41 KB
/
AdminController.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import superagent from 'superagent';
import React from 'react';
import { Editor } from '@tinymce/tinymce-react';
import fetch from '../../lib/fetch';
import type { AdminDashboard } from './index';
class AdminController {
view: AdminDashboard;
fetch: typeof fetch;
superagent: superagent.SuperAgentStatic;
constructor(view: AdminDashboard) {
this.fetch = fetch;
this.view = view;
this.superagent = superagent;
this.createBlogAPI = this.createBlogAPI.bind(this);
this.editor = this.editor.bind(this);
this.handleEditorChange = this.handleEditorChange.bind(this);
}
async createBlogAPI(evt: { preventDefault: () => void; }): Promise<string> {
evt.preventDefault();
const { auth } = this.view.props;
const { title, blogContent } = this.view.state;
let r;
try {
r = await this.superagent.post(`${process.env.BackendUrl}/blog`)
.set('Authorization', `Bearer ${auth.token}`)
.set('Accept', 'application/json')
.send({ title, body: blogContent });
} catch (e) { return `${e.message}`; }
if (r.status === 201) {
window.location.assign('/');
return `${r.status}`;
}
return 'Failed to create blog';
}
// eslint-disable-next-line class-methods-use-this
validateBlogPost(title: string, blogContent: string): boolean {
let disabled = true;
if (title !== '' && blogContent !== '') { disabled = false; }
return disabled;
}
handleEditorChange(blogContent: string): boolean { this.view.setState({ blogContent }); return true; }
editor(blogContent: string): JSX.Element {
// const blogC = blogContent || '';
return (
<Editor
apiKey={process.env.TINY_KEY}
initialValue={blogContent}
init={{
height: 600,
menubar: 'insert tools',
menu: { format: { title: 'Format', items: 'forecolor backcolor' } },
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table paste code help wordcount',
],
toolbar:
'undo redo | formatselect | bold italic backcolor forecolor |'
+ 'alignleft aligncenter alignright alignjustify |'
+ 'bullist numlist outdent indent | removeformat | help',
}}
onEditorChange={this.handleEditorChange}
/>
);
}
}
export default AdminController;