import React, { useState, useEffect } from 'react' import fetch from 'unfetch' import Head from 'next/head' import Link from 'next/link' import LoadingIndicator from '../components/LoadingIndicator' import Sleep from '../components/sleep' import NewsEntry from '../components/newsEntry' import Composer from '../components/composer' import DbSelector from '../components/dbSelector.js' import styles from './index.module.css' import constants from '../constants.json' const links = { install: { href: '/install', text: 'Install', }, irc: { href: '/irc', text: 'IRC chat', }, } function fetchNews() { return new Promise((resolve) => { fetch(`${constants.apiUrl}news.json`, { credentials: 'include', }) .then((resp) => { return resp.json() }) .then((res) => { resolve(res) }) }) } function addPost(title, content) { return new Promise((resolve) => { fetch(constants.apiUrl + 'addPost', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ title: title, content: content, }), }) .then((res) => { return res.json() }) .then((res) => { resolve(res) }) }) } function postFeedback(content, file) { const postText = (file) => { return new Promise((resolve) => { fetch(constants.apiUrl + 'postfeedback', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ content: content, file: file ? file.name : null, }), }) .then((res) => { return res.json() }) .then((res) => { resolve(res) }) }) } return new Promise((resolve) => { if (file) { const formData = new FormData() // eslint-disable-line formData.append('file', file) fetch(constants.apiUrl + 'postfeedbackfile', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', }, body: formData, }) .then((res) => { return res.json() }) .then((fileres) => { postText(file).then((textres) => { resolve({ fileres, textres }) }) }) } else { postText().then((res) => { resolve(res) }) } }) } export default function Index({ globalData }) { const userId = globalData.userId const motd = globalData.motd const [news, setNews] = useState(null) const [allQrSelector, setAllQrSelector] = useState(null) // const userSpecificMotd = props.globalData.userSpecificMotd useEffect(() => { console.info('Fetching news.json') fetchNews().then((res) => { setNews(res) }) }, []) const renderNews = () => { if (news) { let newsItems = Object.keys(news) .map((key) => { let newsEntryData = news[key] return ( { fetch(constants.apiUrl + 'rmPost', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ newsKey: key, }), }) .then((res) => { return res.json() }) .then((res) => { if (res.status === 'fail') { alert(res.msg) } else { setNews(res.news) } }) }} onNewsReact={({ reaction, isDelete }) => { fetch(constants.apiUrl + 'react', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ reaction: reaction, newsKey: key, isDelete: isDelete, }), }) .then((res) => { return res.json() }) .then((res) => { console.log(res) setNews(res.news) }) }} onCommentReact={({ path, reaction, isDelete }) => { fetch(constants.apiUrl + 'react', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'reaction', newsKey: key, path: path, reaction: reaction, isDelete: isDelete, }), }) .then((res) => { return res.json() }) .then((res) => { setNews(res.news) }) }} onDelete={(path) => { fetch(constants.apiUrl + 'comment', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'delete', path: path, newsKey: key, }), }) .then((res) => { return res.json() }) .then((res) => { if (res.status === 'fail') { alert(res.msg) } else { setNews(res.news) } }) }} onComment={(path, content) => { fetch(constants.apiUrl + 'comment', { method: 'POST', credentials: 'include', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'add', path: path, content: content, newsKey: key, }), }) .then((res) => { return res.json() }) .then((res) => { setNews(res.news) }) }} uid={userId} key={key} newsKey={key} newsItem={newsEntryData} /> ) }) .reverse() return (
Forum

{ if (!content) { alert('Üres a tartalom!') return } console.log(type, title, content, file) if (type === 'private') { postFeedback(content, file).then( (/* { fileres, textres } */) => { alert('Privát visszajelzés elküldve!') } ) } else { if (!title) { alert('Üres a téma!') return } addPost(title, content).then((res) => { setNews(res.news) }) } }} />
{newsItems}
) } else { return } } const renderMotd = () => { return (
MOTD

{motd ? (
) : (
...
)}
) } // const renderUserSpecificMotd = () => { // return ( //
//
//
Üzenet admintól:
// {userSpecificMotd ? ( //
// ) : ( //
...
// )} //
// ) // } const renderDbSelector = () => { if (allQrSelector) { return ( { setAllQrSelector(null) }} onDbSelect={(selectedDb) => { if (allQrSelector === 'txt') { if (selectedDb === 'all') { window.open(`${constants.apiUrl}allqr.txt`, '_blank') } else { window.open( `${constants.apiUrl}allqr.txt?db=${selectedDb.name}`, '_blank' ) } } else if (allQrSelector === 'json') { window.open(`${constants.apiUrl}${selectedDb.path}`, '_blank') } }} /> ) } else { return null } } return (
Qmining | Frylabs.net
{renderMotd()} {/*{userSpecificMotd && renderUserSpecificMotd()} */}

{renderNews()} {renderDbSelector()}
) }