Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3915fa8

Browse files
committed
Add a deadline to goals
1 parent 85eddba commit 3915fa8

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

src/components/goals/EditGoal.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { useForm } from 'react-hook-form'
33
import { useMutation, useQueryClient } from 'react-query'
44
import toast, { Toaster } from 'react-hot-toast'
55
import { useEffect, useRef, useState } from 'react'
6-
import { useSession } from 'next-auth/client'
7-
import { User } from 'src/pages/members'
86
import { Markdown, GoalType } from '@/components'
97

108
type Inputs = {
119
title: string
1210
description: string
11+
deadline: Date
1312
}
1413

1514
export default function EditGoal({
@@ -26,7 +25,6 @@ export default function EditGoal({
2625
const queryClient = useQueryClient()
2726
const { register, handleSubmit, errors, trigger } = useForm<Inputs>()
2827
const toastId = useRef('')
29-
const [session] = useSession()
3028
const { mutate } = useMutation(
3129
(data: Inputs) =>
3230
fetch(`/api/fauna/goals/update-goal`, {
@@ -39,6 +37,7 @@ export default function EditGoal({
3937
title: data.title,
4038
description: data.description,
4139
creatorId: goal.creatorId,
40+
deadline: data.deadline,
4241
}),
4342
}).then((res) => {
4443
if (!res.ok) {
@@ -71,7 +70,7 @@ export default function EditGoal({
7170
<div className="mt-6">
7271
<div className="flex space-x-3">
7372
<div className="min-w-0 flex-1">
74-
<form onSubmit={handleSubmit(onSubmit)}>
73+
<form onSubmit={handleSubmit(onSubmit)} className="space-y-3">
7574
<Input
7675
ref={register({ required: true, maxLength: 50 })}
7776
defaultValue={goal.title}
@@ -87,6 +86,16 @@ export default function EditGoal({
8786
}
8887
/>
8988

89+
<Input
90+
type="date"
91+
label="Goal Deadline"
92+
ref={register({ valueAsDate: true, required: true })}
93+
name="deadline"
94+
defaultValue={goal.deadline.toISODate()}
95+
hasError={Boolean(errors.deadline)}
96+
errorMessage="You must set the deadline."
97+
/>
98+
9099
<TextArea
91100
ref={register}
92101
defaultValue={goal.description}

src/components/goals/GoalFeed.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default function GoalFeed({
5656
participants={participants}
5757
createdAt={createdAt}
5858
updatesCount={updatesCount}
59+
deadline={goal.deadline}
5960
/>
6061
</div>
6162
</div>
@@ -67,6 +68,7 @@ export default function GoalFeed({
6768
participants={participants}
6869
createdAt={createdAt}
6970
updatesCount={updatesCount}
71+
deadline={goal.deadline}
7072
/>
7173
</div>
7274
</div>
@@ -79,4 +81,5 @@ export type GoalType = {
7981
title: string
8082
description: string
8183
creatorId: string
84+
deadline: DateTime
8285
}

src/components/goals/GoalMeta.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ export default function GoalMeta({
77
className = '',
88
participants,
99
createdAt,
10+
deadline,
1011
updatesCount,
1112
}: {
1213
className?: string
1314
participants: User[]
1415
createdAt: DateTime
16+
deadline: DateTime
1517
updatesCount: number
1618
}) {
1719
return (
@@ -100,6 +102,28 @@ export default function GoalMeta({
100102
</li>
101103
))}
102104
</ul>
105+
<div className="flex items-center space-x-2 mt-5">
106+
<svg
107+
className="h-5 w-5 text-gray-400"
108+
data-todo-x-description="Heroicon name: solid/calendar"
109+
xmlns="http://www.w3.org/2000/svg"
110+
viewBox="0 0 20 20"
111+
fill="currentColor"
112+
aria-hidden="true"
113+
>
114+
<path
115+
fillRule="evenodd"
116+
d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z"
117+
clipRule="evenodd"
118+
></path>
119+
</svg>
120+
<span className="text-gray-900 text-sm font-medium">
121+
Deadline on{' '}
122+
<time dateTime={createdAt.toISO()}>
123+
{deadline.toLocaleString(DateTime.DATE_FULL)}
124+
</time>
125+
</span>
126+
</div>
103127
</div>
104128
<div>
105129
<h2 className="text-sm font-medium text-gray-500">Tags</h2>

src/components/goals/NewGoal.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import { useMutation, useQueryClient } from 'react-query'
44
import toast, { Toaster } from 'react-hot-toast'
55
import { useRef, useState } from 'react'
66
import { useSession } from 'next-auth/client'
7-
import { User } from 'src/pages/members'
87
import { Markdown } from '@/components'
98

109
type Inputs = {
1110
title: string
1211
description: string
12+
deadline: Date
1313
}
1414

1515
export default function NewGoal() {
1616
const [descriptionStorage, setDescriptionStorage] = useState('')
1717
const queryClient = useQueryClient()
1818
const { register, handleSubmit, errors, trigger } = useForm<Inputs>()
1919
const toastId = useRef('')
20-
const [session] = useSession()
2120
const { mutate } = useMutation(
2221
(data: Inputs) =>
2322
fetch(`/api/fauna/goals/create-and-participate-in-a-goal`, {
@@ -28,6 +27,7 @@ export default function NewGoal() {
2827
body: JSON.stringify({
2928
title: data.title,
3029
description: data.description,
30+
deadline: data.deadline,
3131
}),
3232
}).then((res) => {
3333
if (!res.ok) {
@@ -76,6 +76,15 @@ export default function NewGoal() {
7676
}
7777
/>
7878

79+
<Input
80+
type="date"
81+
label="Goal Deadline"
82+
ref={register({ valueAsDate: true, required: true })}
83+
name="deadline"
84+
hasError={Boolean(errors.deadline)}
85+
errorMessage="You must set the deadline."
86+
/>
87+
7988
<TextArea
8089
ref={register}
8190
name="description"

src/pages/[username]/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type GoalResponse = {
2626
id: string
2727
title: string
2828
description: string
29+
deadline: number
2930
createdAt: number
3031
createdBy: User
3132
participants: {
@@ -99,6 +100,7 @@ export default function UserProfile({
99100
title: goalResponse.title,
100101
description: goalResponse.description,
101102
creatorId: goalResponse.createdBy.id,
103+
deadline: DateTime.fromMillis(goalResponse.deadline),
102104
}}
103105
participants={goalResponse.participants.data}
104106
createdAt={DateTime.fromMillis(goalResponse.createdAt)}

src/pages/api/fauna/goals/all-goals-by-user.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ const FaunaCreateHandler: NextApiHandler = async (
3333
id: q.Select(['ref', 'id'], goalDoc),
3434
title: q.Select(['data', 'title'], goalDoc),
3535
description: q.Select(['data', 'description'], goalDoc),
36+
deadline: q.If(
37+
q.IsNull(q.Select(['data', 'deadline'], goalDoc, null)),
38+
0,
39+
q.ToMillis(q.Select(['data', 'deadline'], goalDoc, null))
40+
),
3641
createdAt: q.ToMillis(
3742
q.Select(['data', 'timestamps', 'createdAt'], goalDoc, 0)
3843
),

src/pages/api/fauna/goals/update-goal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const FaunaCreateHandler: NextApiHandler = async (
2121
if (!session) {
2222
return res.status(401).json({ message: 'Unauthorized' })
2323
}
24-
const { id, title, description, creatorId } = req.body
24+
const { id, title, description, creatorId, deadline } = req.body
2525
const userId = (session.user as User).id
2626

2727
if (userId !== creatorId) {
@@ -36,6 +36,7 @@ const FaunaCreateHandler: NextApiHandler = async (
3636
data: {
3737
title,
3838
description,
39+
deadline: q.Time(new Date(deadline).toISOString()),
3940
timestamps: {
4041
updatedAt: q.Now(),
4142
},

0 commit comments

Comments
 (0)