Repository navigation
Expand file tree
/
Copy pathadmin.tsx
More file actions
executable file
·164 lines (156 loc) · 5.33 KB
/
Copy pathadmin.tsx
File metadata and controls
executable file
·164 lines (156 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { gql, useMutation } from '@apollo/client';
import toast, { Toaster } from 'react-hot-toast';
const CreateLinkMutation = gql`
mutation (
$title: String!
$url: String!
$imageUrl: String!
$category: String!
$description: String!
) {
createLink(
title: $title
url: $url
imageUrl: $imageUrl
category: $category
description: $description
) {
title
url
imageUrl
category
description
}
}
`;
const Admin = () => {
const [createLink, { data, loading, error }] =
useMutation(CreateLinkMutation);
const {
register,
handleSubmit,
formState: { errors },
} = useForm();
const uploadPhoto = async (e) => {
const file = e.target.files[0];
const filename = encodeURIComponent(file.name);
const res = await fetch(`/api/upload-image?file=${filename}`);
const data = await res.json();
const formData = new FormData();
Object.entries({ ...data.fields, file }).forEach(([key, value]) => {
formData.append(key, value);
});
toast.promise(
fetch(data.url, {
method: 'POST',
body: formData,
}),
{
loading: 'Uploading...',
success: 'Image successfully uploaded!🎉',
error: `Upload failed 😥 Please try again ${error}`,
}
);
};
const (data) => {
const { title, url, category, description, image } = data;
const imageUrl = `https://my-awesome-links-bucket.s3.amazonaws.com/${image[0].name}`;
const variables = { title, url, category, description, imageUrl };
try {
toast.promise(createLink({ variables }), {
loading: 'Creating new link..',
success: 'Link successfully created!🎉',
error: `Something went wrong 😥 Please try again - ${error}`,
});
} catch (error) {
console.error(error);
}
};
return (
<div className="container mx-auto max-w-md py-12">
<Toaster />
<h1 className="text-3xl font-medium my-5">Create a new link</h1>
<form
className="grid grid-cols-1 gap-y-6 shadow-lg p-8 rounded-lg"
>
>
<label className="block">
<span className="text-gray-700">Title</span>
<input
placeholder="Title"
name="title"
type="text"
{...register('title', { required: true })}
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
/>
</label>
<label className="block">
<span className="text-gray-700">Description</span>
<input
placeholder="Description"
{...register('description', { required: true })}
name="description"
type="text"
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
/>
</label>
<label className="block">
<span className="text-gray-700">Url</span>
<input
placeholder="https://example.com"
{...register('url', { required: true })}
name="url"
type="text"
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
/>
</label>
<label className="block">
<span className="text-gray-700">Category</span>
<input
placeholder="Name"
{...register('category', { required: true })}
name="category"
type="text"
className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50"
/>
</label>
<label className="block">
<span className="text-gray-700">
Upload a .png or .jpg image (max 1MB).
</span>
<input
{...register('image', { required: true })}
>
type="file"
accept="image/png, image/jpeg"
name="image"
/>
</label>
<button
disabled={loading}
type="submit"
className="my-4 capitalize bg-blue-500 text-white font-medium py-2 px-4 rounded-md hover:bg-blue-600"
>
{loading ? (
<span className="flex items-center justify-center">
<svg
className="w-6 h-6 animate-spin mr-1"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M11 17a1 1 0 001.447.894l4-2A1 1 0 0017 15V9.236a1 1 0 00-1.447-.894l-4 2a1 1 0 00-.553.894V17zM15.211 6.276a1 1 0 000-1.788l-4.764-2.382a1 1 0 00-.894 0L4.789 4.488a1 1 0 000 1.788l4.764 2.382a1 1 0 00.894 0l4.764-2.382zM4.447 8.342A1 1 0 003 9.236V15a1 1 0 00.553.894l4 2A1 1 0 009 17v-5.764a1 1 0 00-.553-.894l-4-2z" />
</svg>
Creating...
</span>
) : (
<span>Create Link</span>
)}
</button>
</form>
</div>
);
};
export default Admin;
You can’t perform that action at this time.