diff --git a/src/components/pages/Urls.tsx b/src/components/pages/Urls.tsx
index 0c32349..9fff17d 100644
--- a/src/components/pages/Urls.tsx
+++ b/src/components/pages/Urls.tsx
@@ -1,20 +1,44 @@
import React, { useEffect, useState } from 'react';
-import { Grid, Card, CardHeader, Box, Typography, IconButton, Link } from '@material-ui/core';
-import { ContentCopy as CopyIcon, DeleteForever as DeleteIcon } from '@material-ui/icons';
+import { Grid, Card, CardHeader, Box, Typography, IconButton, Link, Dialog, DialogContent, DialogActions, Button, DialogTitle, TextField } from '@material-ui/core';
+import { ContentCopy as CopyIcon, DeleteForever as DeleteIcon, Add as AddIcon } from '@material-ui/icons';
import Backdrop from 'components/Backdrop';
import useFetch from 'hooks/useFetch';
import Alert from 'components/Alert';
import copy from 'copy-to-clipboard';
+import { useFormik } from 'formik';
+import { useStoreSelector } from 'lib/redux/store';
+import * as yup from 'yup';
+
+function TextInput({ id, label, formik, ...other }) {
+ return (
+
+ );
+}
export default function Urls() {
+ const user = useStoreSelector(state => state.user);
+
const [loading, setLoading] = useState(true);
const [urls, setURLS] = useState([]);
const [open, setOpen] = useState(false);
+ const [createOpen, setCreateOpen] = useState(false);
const [severity, setSeverity] = useState('success');
const [message, setMessage] = useState('Deleted');
- const updatePages = async () => {
+ const updateURLs = async () => {
setLoading(true);
const urls = await useFetch('/api/user/urls');
@@ -34,7 +58,7 @@ export default function Urls() {
setOpen(true);
}
- updatePages();
+ updateURLs();
};
const copyURL = u => {
@@ -44,8 +68,55 @@ export default function Urls() {
setOpen(true);
};
+ const formik = useFormik({
+ initialValues: {
+ url: '',
+ vanity: '',
+ },
+ validationSchema: yup.object({
+
+ }),
+ onSubmit: async (values) => {
+ const cleanURL = values.url.trim();
+ const cleanVanity = values.vanity.trim();
+
+ if (cleanURL === '') return formik.setFieldError('username', 'Username can\'t be nothing');
+
+ const data = {
+ url: cleanURL,
+ vanity: cleanVanity === '' ? null : cleanVanity,
+ };
+
+ setCreateOpen(false);
+ setLoading(true);
+ const res = await fetch('/api/shorten', {
+ method: 'POST',
+ headers: {
+ 'Authorization': user.token,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(data),
+ });
+ const json = await res.json();
+
+ if (json.error) {
+ setSeverity('error');
+ setMessage('Could\'nt create URL: ' + json.error);
+ setOpen(true);
+ } else {
+ setSeverity('success');
+ setMessage('Copied URL: ' + json.url);
+ copy(json.url);
+ setOpen(true);
+ setCreateOpen(false);
+ updateURLs();
+ }
+ setLoading(false);
+ },
+ });
+
useEffect(() => {
- updatePages();
+ updateURLs();
}, []);
return (
@@ -53,6 +124,22 @@ export default function Urls() {
+
+
{!urls.length ? (
- No URLs
+ No URLs setCreateOpen(true)}>
- ) : URLs}
+ ) : URLs setCreateOpen(true)}>}
{urls.length ? urls.map(url => (