Skip to content

Commit 38a2644

Browse files
committed
feat: implement use anonymous for creating a paste
1 parent d63a1e9 commit 38a2644

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/components/Editor.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from 'lodash';
44
import Router from 'next/router';
55
import { ApiCreatePasteResponse } from 'pages/api/pastes/create';
66
import { ApiUpdatePasteResponse } from 'pages/api/pastes/update/[refid]';
7-
import { ChangeEvent, useCallback, useEffect, useRef, useState } from 'react';
7+
import { ChangeEvent, MutableRefObject, useCallback, useEffect, useRef, useState } from 'react';
88
import DatePicker from 'react-datepicker';
99
import 'react-datepicker/dist/react-datepicker.css';
1010
import { useDropzone } from 'react-dropzone';
@@ -18,6 +18,7 @@ type EditorProps = {
1818
refid?: string;
1919
data?: Paste; // comes in with update,
2020
queryData?: RouterPasteQueryData; // queryData is for custom data from url query (only applicable to index)
21+
isAnonymousRef?: MutableRefObject<HTMLInputElement>;
2122
};
2223

2324
// convert the date to iso ?
@@ -29,7 +30,7 @@ const convertDateForInput = (date: string) => {
2930
}
3031
};
3132

32-
const MainEditor = ({ update, refid, data, queryData }: EditorProps) => {
33+
const MainEditor = ({ update, refid, data, queryData, isAnonymousRef }: EditorProps) => {
3334
const [pDate, setPDate] = useState<Date>(
3435
update ? (data.expiryDate ? new Date(convertDateForInput(data.expiryDate)) : null) : null
3536
);
@@ -88,13 +89,20 @@ const MainEditor = ({ update, refid, data, queryData }: EditorProps) => {
8889
onCreateNotify(update ? 'Updating paste...' : 'Creating paste...');
8990

9091
// contact api
91-
fetch(`${update ? `/api/pastes/update/${refid}` : '/api/pastes/create'}`, {
92-
method: 'PUT',
93-
headers: {
94-
'Content-Type': 'application/json'
95-
},
96-
body: JSON.stringify(pasteData)
97-
})
92+
fetch(
93+
`${
94+
update
95+
? `/api/pastes/update/${refid}`
96+
: `/api/pastes/create?isAnonymous=${String(isAnonymousRef.current?.checked)}`
97+
}`,
98+
{
99+
method: 'PUT',
100+
headers: {
101+
'Content-Type': 'application/json'
102+
},
103+
body: JSON.stringify(pasteData)
104+
}
105+
)
98106
.then((res) => res.json())
99107
.then((r: ApiCreatePasteResponse | ApiUpdatePasteResponse) => {
100108
if (r.error) {

src/pages/api/pastes/create.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
NOTE: /api/pastes/create -> endpoint for creating a paste
33
*/
44

5-
import type { NextApiRequest, NextApiResponse } from 'next';
6-
7-
import { nanoid } from 'nanoid';
8-
9-
import { PasteModel } from '@lib/models/paste';
10-
import methodHandler from '@lib/middleware/methods';
11-
import { getTokenAPI } from '@lib/hooks/useTokenAPI';
5+
import { errParseBody } from '@lib/body-parse';
126
import { getCodeLanguage } from '@lib/code';
137
import { getUser } from '@lib/hooks/getUser';
8+
import { getTokenAPI } from '@lib/hooks/useTokenAPI';
9+
import methodHandler from '@lib/middleware/methods';
10+
import { PasteModel } from '@lib/models/paste';
1411
import { schemaValidate } from '@lib/validate';
15-
16-
import { errParseBody } from '@lib/body-parse';
12+
import { ApiCreatePasteBody, Paste } from '@utils/interfaces/paste';
1713
import { CreatePasteQuery, QueryErrorResponse } from '@utils/interfaces/query';
1814
import { ApiCreateBodySchema } from '@utils/schema/createBody';
19-
import { ApiCreatePasteBody, Paste } from '@utils/interfaces/paste';
15+
import { nanoid } from 'nanoid';
16+
import type { NextApiRequest, NextApiResponse } from 'next';
2017

2118
export type ApiCreatePasteResponse = CreatePasteQuery;
2219
type ValidateCreateProps = { rdata: ApiCreatePasteBody; ok: boolean; err?: QueryErrorResponse };
@@ -31,7 +28,7 @@ const createPaste = async (req: NextApiRequest, res: NextApiResponse<ApiCreatePa
3128
}
3229

3330
const token = getTokenAPI(req, res);
34-
const { isUser, name } = await getUser(req, res);
31+
const { isUser, name } = await handleUser(req, res);
3532

3633
const data: Paste = {
3734
createdDate: new Date().toISOString(),
@@ -50,6 +47,19 @@ const createPaste = async (req: NextApiRequest, res: NextApiResponse<ApiCreatePa
5047
res.status(q.code).json(q);
5148
};
5249

50+
const handleUser = async (req: NextApiRequest, res: NextApiResponse): Promise<{ isUser: boolean; name: string }> => {
51+
const { isAnonymous } = req.query;
52+
if (isAnonymous === 'true') {
53+
return {
54+
isUser: false,
55+
name: 'anonymous'
56+
};
57+
}
58+
59+
const { isUser, name } = await getUser(req, res);
60+
return { isUser, name };
61+
};
62+
5363
// Getter and Validator for req.body
5464
const getPostCreateData = async (req: NextApiRequest): Promise<ValidateCreateProps> => {
5565
const d: ApiCreatePasteBody = req.body;

src/pages/index.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1+
import { useUser } from '@auth0/nextjs-auth0';
12
import MainEditor from '@components/Editor';
23
import Layout from '@components/Layout';
34
import { joinString } from '@ootiq/blank';
45
import { useRouter } from 'next/router';
6+
import { useRef } from 'react';
57

68
export default function Home() {
79
const router = useRouter();
810

911
const { filename, description, content } = router.query;
12+
const { user } = useUser();
13+
14+
const isAnonymousRef = useRef<HTMLInputElement>(null);
1015

1116
return (
1217
<Layout title="Welcome ">
13-
<div className="w-5/6 mx-auto mt-8">
18+
<div className="w-5/6 mx-auto mt-8 flex items-center justify-between">
1419
<p className="tracking-wide text-primary-500 opacity-90 text-lg font-bold">Create A New Paste</p>
20+
21+
{user && (
22+
<div className="inline-flex items-center" title="Publish this paste as anonymous user">
23+
<input ref={isAnonymousRef} type="checkbox" name="" id="" />
24+
<label className="lowercase ml-2 text-sm text-secondary-700" htmlFor="use-anonymouse">
25+
Use Anonymous
26+
</label>
27+
</div>
28+
)}
1529
</div>
1630
<MainEditor
1731
queryData={{
1832
filename: joinString(filename),
1933
description: joinString(description),
2034
content: joinString(content)
2135
}}
36+
isAnonymousRef={isAnonymousRef}
2237
/>
2338
</Layout>
2439
);

0 commit comments

Comments
 (0)