-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathedit-doc-data-panel.tsx
More file actions
141 lines (127 loc) · 4.01 KB
/
edit-doc-data-panel.tsx
File metadata and controls
141 lines (127 loc) · 4.01 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
import React, { useEffect, useState } from "react"
import "react-calendar/dist/Calendar.css"
import DatePicker from "react-date-picker"
import "react-date-picker/dist/DatePicker.css"
import { HiPencilAlt } from "react-icons/hi/index"
import { IoCheckmarkSharp } from "react-icons/io5/index"
import {
unstable_Form as Form,
unstable_FormInput as FormInput,
unstable_FormLabel as FormLabel,
} from "reakit"
import { unstable_FormSubmitButton as FormSubmitButton } from "reakit"
import { UserRole, useUserRole } from "./auth"
import { IconButton } from "./components"
import { IconTextButton } from "./components/button"
import { useForm } from "./edit-doc-data-form-context"
import * as css from "./edit-word-feature.css"
import * as Dailp from "./graphql/dailp"
import { EditingProvider, useEditing } from "./pages/documents/editing-context"
/** Button that allows user to enter edit mode in the word panel, and edit fields of a word. */
export const EditButton = () => {
const { isEditing, setIsEditing } = useEditing()
const form = useForm()
return (
<Form className={css.form}>
{isEditing ? (
// Displays a "Cancel" button and "Save" button in editing mode.
<>
<IconButton
className={css.cancelButton}
round={false}
onClick={() => {
setIsEditing(false)
}}
>
Cancel
</IconButton>
<IconTextButton
icon={<IoCheckmarkSharp />}
className={css.editPanelButton}
as={FormSubmitButton}
>
Save
</IconTextButton>
</>
) : (
<IconTextButton
icon={<HiPencilAlt />}
className={css.editPanelButton}
onClick={() => {
setIsEditing(true)
}}
>
Edit
</IconTextButton>
)}
</Form>
)
}
/** Displays a FormInput with its corresponding feature data from the Reakit form. */
export const EditDocPanel = (props: { document?: Dailp.AnnotatedDoc }) => {
let docData = props.document as unknown as Dailp.AnnotatedDoc
const { form } = useForm()
if (!form || !form.values.document) {
return null
}
form.values.document["id "] = docData.id
const [selectedDate, setSelectedDate] = useState<Date | undefined>(undefined)
const [day, setDay] = useState<Number>()
const [month, setMonth] = useState<Number>()
const [year, setYear] = useState<Number>()
const handleDateChange = (e: any) => {
const selectedDateValue = e as Date
if (selectedDateValue) {
setSelectedDate(selectedDateValue)
const selectedDay = selectedDateValue.getDate()
const selectedMonth = selectedDateValue.getMonth() + 1
const selectedYear = selectedDateValue.getFullYear()
setDay(selectedDay)
setMonth(selectedMonth)
setYear(selectedYear)
}
}
// Use form.push to update the form state manually
useEffect(() => {
form.push(["document", "id"], [docData.id.toString()]) // push manually
if (selectedDate) {
form.push(["document", "date"], [{ day: day, month: month, year: year }])
} else {
form.push(["document", "date"], [])
}
}, [day, month, year])
let userRole = useUserRole()
return (
<>
{/* Display a label for the form input if it exists. */}
<FormLabel
{...form}
className={css.formInputLabel}
name={"title"}
label={"Title"}
/>
<FormInput
{...form}
className={css.formInput}
name={["document", "title"]}
disabled={!(userRole == UserRole.Editor)}
/>
<p />
<FormLabel
{...form}
className={css.formInputLabel}
name={"written_at"}
label={"Written At"}
/>
<div className={css.dateInputConatiner}>
<DatePicker
onChange={(date: any) => handleDateChange(date)}
value={selectedDate}
format="dd-MM-y"
disabled={!(userRole == UserRole.Editor)}
/>
</div>
</>
)
}
export default EditDocPanel