This commit is contained in:
Stefan Schwarz 2020-05-03 16:51:20 +02:00
parent 867a2c8ebf
commit e99de57edc
24 changed files with 11772 additions and 187 deletions

43
frontend/src/App.js Normal file
View file

@ -0,0 +1,43 @@
import React from "react";
import { useState, useEffect } from "react";
import Layout from "./component/Layout";
import List from "./component/List";
import Thread from "./component/Thread";
import Search from "./component/Search";
export default function Index() {
const [query, setQuery] = useState("tag:inbox");
const leftup = <Search setQuery={setQuery} />;
const [threads, setThreads] = useState({
count: 0,
threads: [{}],
});
useEffect(() => {
fetch(`/search/${query}/0/30`)
.then((response) => response.json())
.then(
(data) => setThreads(data),
(error) => console.log(error)
);
}, [query]);
const [thread, setThread] = useState("");
const [threadSpec, setThreadSpec] = useState({ message_ids: [] });
useEffect(() => {
if (thread == "") {
return;
}
fetch(`/thread/${thread}`)
.then((response) => response.json())
.then(
(data) => setThreadSpec(data),
(error) => console.log(error)
);
}, [thread]);
const left = <List items={threads.threads} setThread={setThread} />;
const right = <Thread spec={threadSpec} />;
return <Layout leftup={leftup} left={left} right={right}></Layout>;
}

View file

@ -0,0 +1,18 @@
import React from "react";
import Container from "@material-ui/core/Container";
import Grid from "@material-ui/core/Grid";
export default ({ leftup, left, right }) => (
<Container>
<Grid container spacing={1}>
<Grid item xs={4}>
{leftup}
{left}
</Grid>
<Grid item xs={8}>
{right}
</Grid>
</Grid>
</Container>
);

View file

@ -0,0 +1,20 @@
import React from "react";
import { List, ListItem, ListItemText } from "@material-ui/core";
export default ({ items = [{}], setThread }) => {
const handleThreadChange = (e, thread) => {
e.preventDefault();
setThread(thread.thread_id);
};
const list = items.map((i) => (
<ListItem button dense key={i.thread_id}>
<ListItemText
primary={i.subject}
onClick={(e) => handleThreadChange(e, i)}
/>
</ListItem>
));
return <List>{list}</List>;
};

View file

@ -0,0 +1,58 @@
import React from "react";
import { useState, useEffect } from "react";
import {
Button,
Card,
CardActionArea,
CardContent,
Chip,
Divider,
Typography,
} from "@material-ui/core";
export default ({ messageId }) => {
const [message, setMessage] = useState({
tags: [],
from: "",
to: "",
cc: "",
bcc: "",
});
useEffect(() => {
fetch(`/message/${encodeURIComponent(messageId)}`)
.then((response) => response.json())
.then(
(data) => setMessage(data),
(error) => console.log(error)
);
}, []);
function body(message) {
return { __html: message.body };
}
const chips = message.tags.map((tag) => {
return <Chip size="small" label={tag} />;
});
return (
<Card key={messageId}>
<CardContent>
<Typography variant="h5" component="h2">
{message.subject}
</Typography>
<div>
From: {message.from}
<br />
To: {message.to}
<br />
</div>
<div>{chips}</div>
<Divider />
<div dangerouslySetInnerHTML={body(message)}></div>
</CardContent>
<CardActionArea>
<Button color="primary">Reply</Button>
</CardActionArea>
</Card>
);
};

View file

@ -0,0 +1,52 @@
import React from "react";
import { useState, useEffect } from "react";
import { Container, TextField } from "@material-ui/core";
import { Autocomplete } from "@material-ui/lab";
export default ({ setQuery }) => {
const [search, setSearch] = useState(["tag:inbox"]);
function handleSearchChange(event, value) {
console.log(event);
console.log(value);
setSearch(value);
setQuery(value.join(" "));
}
const [options, setOptions] = useState([]);
const handleSubSearchChange = (event, value, reason) => {
const [kind, ...q] = value.split(":");
const qq = q.join(":");
fetch(`/autocomplete/${kind}/${qq}`)
.then((response) => response.json())
.then(
(data) => {
if (data) {
setOptions(data.results || []);
}
},
(error) => console.log(error)
);
};
const [autocomplete, setAutocomplete] = useState("");
useEffect(() => {
setAutocomplete(
<Autocomplete
multiple
id="search"
value={search}
onChange={handleSearchChange}
onInputChange={handleSubSearchChange}
options={options}
renderInput={(params) => (
<TextField {...params} label="query" variant="outlined" />
)}
size="small"
/>
);
}, [options]);
return autocomplete;
};

View file

@ -0,0 +1,11 @@
import React from "react";
import { Card, CardContent } from "@material-ui/core";
import Message from "./Message";
export default ({ spec }) => {
const messages = spec.message_ids.map((message_id) => {
return <Message key={message_id} messageId={message_id}></Message>;
});
return <div>{messages}</div>;
};

16
frontend/src/index.js Normal file
View file

@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

View file

@ -0,0 +1,141 @@
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}
export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
registration.unregister();
})
.catch(error => {
console.error(error.message);
});
}
}