Files
mykb/comps/AddDoc.js
JJ Kasper 53ac8a6793 added prettier, removed seperate react eslint config, fixed missing
component display name Markdown.js, and added run method to app
2018-06-01 16:31:14 -05:00

49 lines
1.3 KiB
JavaScript

import React, { Component } from 'react';
import { connect } from 'react-redux';
import fetch from 'isomorphic-unfetch';
import mapUser from '../util/mapUser';
import getUrl from '../util/getUrl';
import getJwt from '../util/getJwt';
const getDoc = async (id, req) => {
let found, doc;
const jwt = getJwt(req);
if(!jwt) return { found, doc, id };
const docRes = await fetch(getUrl('docs/' + id, Boolean(req)), {
method: 'GET',
headers: { Authorization: jwt }
});
if(docRes.ok) {
doc = await docRes.json();
found = true;
}
return { found, doc, id };
};
export default ComposedComponent => {
class DocComp extends Component {
state = {
found: false,
id: null,
doc: {}
}
static async getInitialProps({ query, req }) {
return await getDoc(query.id, req);
}
static getDerivedStateFromProps(nextProps, state) {
const { found, id, doc } = nextProps;
if(state.found !== found) return { found, id, doc };
return null;
}
async componentDidUpdate(prevProps) {
const { user, found, id } = this.props;
if(prevProps.user.email === user.email || found) return;
if(!user.email) return;
this.setState(await getDoc(id));
}
render() {
return <ComposedComponent {...this.state} />;
}
}
return connect(mapUser)(DocComp);
};