Interim commit for async/await

This commit is contained in:
oitsjustjose 2021-07-02 20:31:20 -05:00
commit fc4d1aa5f4
8 changed files with 65 additions and 37 deletions

View file

@ -15,16 +15,17 @@ export const routes = [
{ path: '/files', component: FileUpload }, { path: '/files', component: FileUpload },
]; ];
export default () => { export default async () => {
/* Ensure that there's a slug worth trying, and that it isn't /* Ensure that there's a slug worth trying, and that it isn't
already a route that should be "protected" already a route that should be "protected"
*/ */
if (!!slug && !routes.map( if (!!slug && !routes.map(
({ path }) => path, ({ path }) => path,
).includes(window.location.pathname)) { ).includes(window.location.pathname)) {
CheckUrlRedirect(slug).then((url) => { const url = await CheckUrlRedirect(slug);
if (url) {
window.location.href = url; window.location.href = url;
}).catch(() => {}); }
} }
ReactDOM.render( ReactDOM.render(

View file

@ -1,3 +1,10 @@
import axios from 'axios'; import axios from 'axios';
export default (slug) => axios.get(`/c/${slug}`).then((resp) => resp.data); export default async (slug) => {
try {
const resp = await axios.get(`/c/${slug}`);
return resp.data;
} catch {
return null;
}
};

View file

@ -1,4 +1,10 @@
import axios from 'axios'; import axios from 'axios';
export default (slug) => axios.get(`/api/${slug}`) export default async (slug) => {
.then((resp) => resp.data); try {
const resp = await axios.get(`/api/${slug}`);
return resp.data;
} catch {
return null;
}
};

View file

@ -1,13 +1,18 @@
import axios from 'axios'; import axios from 'axios';
export default (file) => { export default async (file) => {
if (file.size > 16000000) { if (file.size > 16000000) {
return { error: true, size: file.size }; return { error: true, size: file.size };
} }
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
return axios.put('/api/file', formData)
.then((resp) => resp.data) try {
.catch((ex) => alert(ex)); const resp = await axios.put('/api/file', formData);
return resp.data;
} catch (ex) {
alert(ex);
return null;
}
}; };

View file

@ -1,8 +1,9 @@
import axios from 'axios'; import axios from 'axios';
export default (state) => { export default async ({ paste, syntax, expiresAt }) => {
const { paste, syntax, expiresAt } = state; const resp = await axios.put('/api/paste', {
return axios.put('/api/paste', {
paste, syntax, expiresAt, paste, syntax, expiresAt,
}).then((resp) => resp.data); });
return resp.data;
}; };

View file

@ -1,12 +1,12 @@
import axios from 'axios'; import axios from 'axios';
import { routes } from '../App'; import { routes } from '../App';
export default (url, slug) => { export default async (url, slug) => {
const paths = routes.map(({ path }) => path.substr(1)); const paths = routes.map(({ path }) => path.substr(1));
if (slug && paths.includes(slug)) { if (slug && paths.includes(slug)) {
return Promise.resolve(null); return null;
} }
return axios.put('/api/url', { url, slug }).then((resp) => resp.data); const resp = await axios.put('/api/url', { url, slug });
return resp.data;
}; };

View file

@ -3,7 +3,7 @@ import {
Button, Container, Form, InputGroup, Button, Container, Form, InputGroup,
} from 'react-bootstrap'; } from 'react-bootstrap';
import { CSSTransition } from 'react-transition-group'; import { CSSTransition } from 'react-transition-group';
import putImgHandler from '../axios/putFileHandler'; import putFileHandler from '../axios/putFileHandler';
const toMb = (size) => (size / 1000000).toFixed(2); const toMb = (size) => (size / 1000000).toFixed(2);
@ -15,7 +15,7 @@ export default () => {
error: null, error: null,
}); });
const submit = (evt) => { const submit = async (evt) => {
evt.preventDefault(); evt.preventDefault();
if (state.file && state.file.name.length) { if (state.file && state.file.name.length) {
if (state.file.size > 16000000) { if (state.file.size > 16000000) {
@ -27,14 +27,14 @@ export default () => {
}); });
return; return;
} }
setState({ ...state, loading: true }); setState({ ...state, loading: true });
putImgHandler(state.file).then((data) => { const data = await putFileHandler(state.file);
setState({ setState({
loading: false, loading: false,
result: `${window.location.origin}/f/${data}`.replace('5000', '3000'), result: `${window.location.origin}/f/${data}`.replace('5000', '3000'),
file: null, file: null,
error: null, error: null,
});
}); });
} }
}; };

View file

@ -31,6 +31,7 @@ export default () => {
loading: false, loading: false,
result: null, result: null,
}); });
const [renderState, setRenderState] = useState({ const [renderState, setRenderState] = useState({
paste: null, paste: null,
notFound: false, notFound: false,
@ -39,6 +40,15 @@ export default () => {
const query = new URLSearchParams(window.location.search); const query = new URLSearchParams(window.location.search);
const isValid = state.paste && state.syntax && state.expiresAt; const isValid = state.paste && state.syntax && state.expiresAt;
const loadPaste = async () => {
try {
const paste = await getTxtHandler(query.get('id'));
setRenderState({ ...renderState, paste });
} catch {
setRenderState({ ...renderState, notFound: true });
}
};
// Check if the user is _viewing_ a paste // Check if the user is _viewing_ a paste
if (query.has('id')) { if (query.has('id')) {
// If the paste isn't loaded: // If the paste isn't loaded:
@ -53,9 +63,7 @@ export default () => {
} }
// Grab the paste, and set it // Grab the paste, and set it
getTxtHandler(query.get('id')) loadPaste();
.then((paste) => setRenderState({ ...renderState, paste }))
.catch(() => setRenderState({ ...renderState, notFound: true }));
// Render nice loading text :) // Render nice loading text :)
return ( return (
@ -76,16 +84,16 @@ export default () => {
); );
} }
const submit = (evt) => { const submit = async (evt) => {
evt.preventDefault(); evt.preventDefault();
if (state.paste && state.syntax && state.expiresAt) { if (state.paste && state.syntax && state.expiresAt) {
setState({ ...state, loading: true }); setState({ ...state, loading: true });
putTxtHandler(state).then((data) => { const data = await putTxtHandler(state);
setState({ setState({
...state, ...state,
result: `${window.location.origin}/code?id=${data}`, result: `${window.location.origin}/code?id=${data}`,
loading: false, loading: false,
});
}); });
} }
}; };