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 },
];
export default () => {
export default async () => {
/* Ensure that there's a slug worth trying, and that it isn't
already a route that should be "protected"
*/
if (!!slug && !routes.map(
({ path }) => path,
).includes(window.location.pathname)) {
CheckUrlRedirect(slug).then((url) => {
const url = await CheckUrlRedirect(slug);
if (url) {
window.location.href = url;
}).catch(() => {});
}
}
ReactDOM.render(

View file

@ -1,3 +1,10 @@
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';
export default (slug) => axios.get(`/api/${slug}`)
.then((resp) => resp.data);
export default async (slug) => {
try {
const resp = await axios.get(`/api/${slug}`);
return resp.data;
} catch {
return null;
}
};

View file

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

View file

@ -1,12 +1,12 @@
import axios from 'axios';
import { routes } from '../App';
export default (url, slug) => {
export default async (url, slug) => {
const paths = routes.map(({ path }) => path.substr(1));
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,
} from 'react-bootstrap';
import { CSSTransition } from 'react-transition-group';
import putImgHandler from '../axios/putFileHandler';
import putFileHandler from '../axios/putFileHandler';
const toMb = (size) => (size / 1000000).toFixed(2);
@ -15,7 +15,7 @@ export default () => {
error: null,
});
const submit = (evt) => {
const submit = async (evt) => {
evt.preventDefault();
if (state.file && state.file.name.length) {
if (state.file.size > 16000000) {
@ -27,14 +27,14 @@ export default () => {
});
return;
}
setState({ ...state, loading: true });
putImgHandler(state.file).then((data) => {
setState({
loading: false,
result: `${window.location.origin}/f/${data}`.replace('5000', '3000'),
file: null,
error: null,
});
const data = await putFileHandler(state.file);
setState({
loading: false,
result: `${window.location.origin}/f/${data}`.replace('5000', '3000'),
file: null,
error: null,
});
}
};

View file

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