Skip to content

Commit 426fdf5

Browse files
committed
feat: show modal on error
1 parent e1e212d commit 426fdf5

File tree

8 files changed

+164
-39
lines changed

8 files changed

+164
-39
lines changed

app/page.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ import { CommentContext } from '@/context/CommentContext'
1212
import { removeDeletedReply } from '@/utils/removeDeleted/removeDeletedReply'
1313
import { removeDeletedComment } from '@/utils/removeDeleted/removeDeletedComment'
1414
import { EditCommentContext } from '@/context/EditCommentContext'
15+
import Modal from '@/components/Modal'
1516

1617
const Home = () => {
1718

1819
const { values, setValues } = React.useContext(UserContext)
1920
const { reply } = React.useContext(ReplyContext)
2021
const { deletedCommentValues, setDeletedCommentValues } = React.useContext(CommentContext)
21-
const { editCommentValues, setEditCommentValues } = React.useContext(EditCommentContext)
22+
const { editCommentValues } = React.useContext(EditCommentContext)
2223
const router = useRouter()
2324

2425
const [comments, setComments] = React.useState<CommentProps[]>([])
2526
const [loading, setLoading] = React.useState(false)
27+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
28+
show: false,
29+
message: '',
30+
})
2631

2732
React.useEffect(() => {
2833
const checkIfUserIsLoggedIn = () => {
@@ -52,7 +57,10 @@ const Home = () => {
5257
setLoading(false)
5358

5459
if (data.error) {
55-
alert(data.error)
60+
setNeedsToShowModal({
61+
show: true,
62+
message: data.error,
63+
})
5664
return
5765
}
5866

@@ -165,6 +173,17 @@ const Home = () => {
165173
<>
166174
{values.isLoggedIn ?
167175
<>
176+
177+
{
178+
needsToShowModal.show &&
179+
<Modal
180+
title='Error'
181+
positive='Ok'
182+
message={needsToShowModal.message}
183+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
184+
/>
185+
}
186+
168187
<header className='md:w-[740px] md:mx-auto md:static md:my-10 flex items-center justify-between gap-5 mb-5 absolute top-0 left-0 right-0 bg-white p-5'>
169188
<div className='flex gap-5 items-center'>
170189
<Image className='w-12' src='/images/avatars/image-amyrobson.png' height={50} width={50} alt='your profile photo' />

components/AddComment.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react'
33
import Image from 'next/image'
44
import { UserContext } from '@/context/UserContext'
55
import { CommentProps } from '@/types/props'
6+
import Modal from './Modal'
67

78
type AddCommentProps = {
89
setComments: React.Dispatch<React.SetStateAction<CommentProps[]>>
@@ -12,6 +13,10 @@ const AddComment = ({ setComments }: AddCommentProps) => {
1213

1314
const [comment, setComment] = React.useState('')
1415
const [commenting, setCommenting] = React.useState(false)
16+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
17+
show: false,
18+
message: '',
19+
})
1520

1621
const { values } = React.useContext(UserContext)
1722

@@ -21,7 +26,10 @@ const AddComment = ({ setComments }: AddCommentProps) => {
2126
setCommenting(true)
2227

2328
if (!values.username) {
24-
alert('Please login to post a comment')
29+
setNeedsToShowModal({
30+
show: true,
31+
message: 'Please log in to post a comment.',
32+
})
2533
setCommenting(false)
2634
return
2735
}
@@ -41,7 +49,10 @@ const AddComment = ({ setComments }: AddCommentProps) => {
4149
setCommenting(false)
4250

4351
if (data.error) {
44-
console.log(data.error)
52+
setNeedsToShowModal({
53+
show: true,
54+
message: data.error,
55+
})
4556
return
4657
}
4758

@@ -52,6 +63,15 @@ const AddComment = ({ setComments }: AddCommentProps) => {
5263

5364
return (
5465
<div className='bg-white p-5 text-dark-blue relative rounded-md flex gap-5 mb-5 sm:flex-row items-start'>
66+
{
67+
needsToShowModal.show &&
68+
<Modal
69+
title='Error'
70+
positive='Ok'
71+
message={needsToShowModal.message}
72+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
73+
/>
74+
}
5575
<Image
5676
priority
5777
src='/images/avatars/image-amyrobson.png'

components/AddReply.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import Image from 'next/image'
44
import { UserContext } from '@/context/UserContext'
55
import { ReplyContext } from '@/context/ReplyContext'
66
import { ReplyProps } from '@/types/props'
7+
import Modal from './Modal'
78

89
const AddReply = ({ author, _id, addReplyToComment }: ReplyProps) => {
910

1011
const [comment, setComment] = React.useState('')
1112
const [commenting, setCommenting] = React.useState(false)
13+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
14+
show: false,
15+
message: '',
16+
})
1217

1318
const { values } = React.useContext(UserContext)
1419
const { setReply } = React.useContext(ReplyContext)
@@ -19,7 +24,10 @@ const AddReply = ({ author, _id, addReplyToComment }: ReplyProps) => {
1924
setCommenting(true)
2025

2126
if (!values.username) {
22-
alert('Please login to post a reply')
27+
setNeedsToShowModal({
28+
show: true,
29+
message: 'Please log in to post a comment.',
30+
})
2331
setCommenting(false)
2432
return
2533
}
@@ -43,7 +51,10 @@ const AddReply = ({ author, _id, addReplyToComment }: ReplyProps) => {
4351
setCommenting(false)
4452

4553
if (data.error) {
46-
console.log(data.error)
54+
setNeedsToShowModal({
55+
show: true,
56+
message: data.error,
57+
})
4758
return
4859
}
4960

@@ -60,6 +71,17 @@ const AddReply = ({ author, _id, addReplyToComment }: ReplyProps) => {
6071

6172
return (
6273
<div className='bg-white p-5 text-dark-blue relative rounded-md flex gap-5 mb-5 sm:flex-row items-start'>
74+
75+
{
76+
needsToShowModal.show &&
77+
<Modal
78+
title='Error'
79+
positive='Ok'
80+
message={needsToShowModal.message}
81+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
82+
/>
83+
}
84+
6385
<Image
6486
priority
6587
src='/images/avatars/image-amyrobson.png'

components/CommentCardBtns.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const CommentCardBtns = ({ _id, commentID, isReply, userId, author }: CommentPro
1717
const [loading, setLoading] = React.useState(true)
1818
const [deleting, setDeleting] = React.useState(false)
1919
const [confirmation, setConfirmation] = React.useState(false)
20+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
21+
show: false,
22+
message: '',
23+
})
2024

2125
const checkIfUserIsAuthor = async () => {
2226
fetch('/api/verify-author', {
@@ -29,7 +33,10 @@ const CommentCardBtns = ({ _id, commentID, isReply, userId, author }: CommentPro
2933
setLoading(false)
3034

3135
if (data.error) {
32-
console.log(data.error)
36+
setNeedsToShowModal({
37+
show: true,
38+
message: data.error,
39+
})
3340
return
3441
}
3542

@@ -65,7 +72,10 @@ const CommentCardBtns = ({ _id, commentID, isReply, userId, author }: CommentPro
6572
setDeleting(false)
6673

6774
if (data.error) {
68-
alert(data.error)
75+
setNeedsToShowModal({
76+
show: true,
77+
message: data.error,
78+
})
6979
return
7080
}
7181

@@ -95,6 +105,16 @@ const CommentCardBtns = ({ _id, commentID, isReply, userId, author }: CommentPro
95105
return (
96106
<div className='sm:static absolute bottom-8 right-5'>
97107

108+
{
109+
needsToShowModal.show &&
110+
<Modal
111+
title='Error'
112+
positive='Ok'
113+
message={needsToShowModal.message}
114+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
115+
/>
116+
}
117+
98118
{
99119
confirmation &&
100120
<Modal

components/EditComment.tsx

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import React from 'react'
22
import { CommentProps } from '@/types/props'
33
import { EditCommentContext } from '@/context/EditCommentContext'
4+
import Modal from './Modal'
45

56
const EditComment = ({ comment, commentID, isReply, _id }: CommentProps) => {
67

78
const [commenting, setCommenting] = React.useState(false)
89
const [localComment, setLocalComment] = React.useState(comment)
910

11+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
12+
show: false,
13+
message: '',
14+
})
15+
1016
const { editCommentValues, setEditCommentValues } = React.useContext(EditCommentContext)
1117

1218
const updateComment = async (e: React.FormEvent<HTMLFormElement>) => {
@@ -23,7 +29,10 @@ const EditComment = ({ comment, commentID, isReply, _id }: CommentProps) => {
2329
setCommenting(false)
2430

2531
if (data.error) {
26-
alert(data.error)
32+
setNeedsToShowModal({
33+
show: true,
34+
message: data.error,
35+
})
2736
return
2837
}
2938

@@ -37,31 +46,43 @@ const EditComment = ({ comment, commentID, isReply, _id }: CommentProps) => {
3746
}
3847

3948
return (
40-
<form action="#" onSubmit={updateComment} className='flex flex-1 flex-col items-start gap-5'>
41-
<textarea
42-
required
43-
value={localComment}
44-
onChange={(e) => setLocalComment(e.currentTarget.value)}
45-
placeholder='Add a reply...'
46-
name="comment" id="comment"
47-
className='placeholder-grayish-blue
49+
<>
50+
{
51+
needsToShowModal.show &&
52+
<Modal
53+
title='Error'
54+
positive='Ok'
55+
message={needsToShowModal.message}
56+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
57+
/>
58+
}
59+
60+
<form action="#" onSubmit={updateComment} className='flex flex-1 flex-col items-start gap-5'>
61+
<textarea
62+
required
63+
value={localComment}
64+
onChange={(e) => setLocalComment(e.currentTarget.value)}
65+
placeholder='Add a reply...'
66+
name="comment" id="comment"
67+
className='placeholder-grayish-blue
4868
resize-none border rounded-md w-full h-24 py-3 px-5 focus:border-moderate-blue focus:outline-none
4969
'
50-
/>
51-
<div className='self-end flex items-center bg-white sm:static absolute bottom-5 right-5'>
52-
<button aria-label='cancel edit'>
53-
<i
54-
onClick={() => setEditCommentValues({ ...editCommentValues, editComment: false })}
55-
className='fas fa-times mr-5 text-xl'
56-
/>
57-
</button>
58-
<button
59-
className='bg-moderate-blue text-white px-5 py-2 uppercase rounded-md font-medium hover:opacity-50'
60-
>
61-
{commenting ? 'Updating' : 'Update'}
62-
</button>
63-
</div>
64-
</form>
70+
/>
71+
<div className='self-end flex items-center bg-white sm:static absolute bottom-5 right-5'>
72+
<button aria-label='cancel edit'>
73+
<i
74+
onClick={() => setEditCommentValues({ ...editCommentValues, editComment: false })}
75+
className='fas fa-times mr-5 text-xl'
76+
/>
77+
</button>
78+
<button
79+
className='bg-moderate-blue text-white px-5 py-2 uppercase rounded-md font-medium hover:opacity-50'
80+
>
81+
{commenting ? 'Updating' : 'Update'}
82+
</button>
83+
</div>
84+
</form>
85+
</>
6586
)
6687
}
6788

components/Modal.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import { ModalProps } from "@/types/props"
33
const Modal = ({ title, message, positive, negative, confirm, cancel }: ModalProps) => {
44
return (
55
<div className='fixed top-0 left-0 w-full h-full bg-black bg-opacity-50 z-50 flex justify-center items-center'>
6-
<div className='bg-white sm:w-[350px] w-full mx-5 rounded-lg sm:p-6 p-4'>
6+
<div className='bg-white sm:w-[350px] text-start w-full mx-5 rounded-lg sm:p-6 p-4'>
77
<h2 className='font-medium text-xl'>
88
{title}
99
</h2>
10+
<hr className='my-2' />
1011
<p className='text-grayish-blue text-sm my-5'>
1112
{message}
1213
</p>
1314
<div className='flex gap-3 text-white'>
14-
<button onClick={cancel} className='bg-grayish-blue text-sm rounded-md sm:px-4 p-2 sm:py-3 flex-grow uppercase font-medium hover:opacity-50'>
15-
{negative}
16-
</button>
15+
{
16+
cancel &&
17+
<button onClick={cancel} className='bg-grayish-blue text-sm rounded-md sm:px-4 p-2 sm:py-3 flex-grow uppercase font-medium hover:opacity-50'>
18+
{negative}
19+
</button>
20+
}
1721
<button onClick={confirm} className='bg-soft-red text-white text-sm sm:px-4 p-2 sm:py-3 rounded-md flex-grow uppercase font-medium hover:opacity-50'>
1822
{positive}
1923
</button>

components/UpvoteDownvote.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use client'
2+
import React from 'react'
23
import { UserContext } from '@/context/UserContext'
34
import { CommentProps } from '@/types/props'
4-
import React from 'react'
5+
import Modal from './Modal'
56

67
const UpvoteDownvote = ({ _id, score, author, commentID }: CommentProps) => {
78

89
const [vote, setVote] = React.useState(score || 0)
910
const [voting, setVoting] = React.useState(false)
11+
const [needsToShowModal, setNeedsToShowModal] = React.useState({
12+
show: false,
13+
message: '',
14+
})
1015

1116
const { values } = React.useContext(UserContext)
1217

@@ -38,7 +43,10 @@ const UpvoteDownvote = ({ _id, score, author, commentID }: CommentProps) => {
3843
setVoting(false)
3944

4045
if (data.error) {
41-
alert(data.error)
46+
setNeedsToShowModal({
47+
show: true,
48+
message: data.error,
49+
})
4250
return
4351
}
4452

@@ -48,6 +56,17 @@ const UpvoteDownvote = ({ _id, score, author, commentID }: CommentProps) => {
4856

4957
return (
5058
<div className='flex sm:flex-col sm:py-3 px-3 sm:gap-2 gap-x-5 bg-very-light-gray w-fit h-fit rounded-xl text-center'>
59+
60+
{
61+
needsToShowModal.show &&
62+
<Modal
63+
title='Error'
64+
positive='Ok'
65+
message={needsToShowModal.message}
66+
confirm={() => setNeedsToShowModal({ show: false, message: '' })}
67+
/>
68+
}
69+
5170
<button
5271
aria-label='upvote'
5372
onClick={() => handleUpvote()}

0 commit comments

Comments
 (0)