-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
158 lines (153 loc) · 4.66 KB
/
index.jsx
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
import { View, Text, Pressable } from 'react-native'
import React, { useContext, useState } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { z } from 'zod'
import { zodResolver } from '@hookform/resolvers/zod'
import { Stack, useLocalSearchParams, useRouter } from 'expo-router'
import { useDispatch } from 'react-redux'
import { IconCheck } from '@tabler/icons-react-native'
import GlobalScreen from '../../components/ui/global-screen'
import FullScrollView from '../../components/ui/full-scroll-view'
import RHFTextField from '../../components/inputs/RHFTextField'
import { createPost } from '../../services/post'
import { showToast } from '../../reduxStore/ui-slice'
import { COLORS, SIZE_CONSTANT, SIZES } from '../../constants/theme'
import AutoSuggestInput from './_components/SubforumSuggestion'
import { AuthContext } from '../../auth/context'
import Header from '../../components/ui/header'
import ContentBox from './_components/ContentBox'
import TextField from '../../components/inputs/TextField'
export default function CreatePostScreen() {
const [selectedSubforum, setSelectedSubforum] = useState(null)
const [content, setContent] = useState([])
const [title, setTitle] = useState('')
const { user } = useContext(AuthContext)
const dispatch = useDispatch()
const validationSchema = z.object({
title: z.string().min(1, { message: 'Bu alan gerekli.' }),
})
const router = useRouter()
const { subforumTitle, subforumId } = useLocalSearchParams()
const form = useForm({
defaultValues: {
title: '',
},
resolver: zodResolver(validationSchema),
})
const handleCreate = async (data) => {
try {
const res = await createPost({
username: user.username,
title,
content,
parentID: subforumTitle ? subforumId : selectedSubforum.id,
})
if (res?.successful) {
dispatch(
showToast({ text: 'Post created successfully', variant: 'success' })
)
router.back()
} else {
dispatch(showToast({ text: res.message, variant: 'error' }))
}
} catch (error) {
dispatch(showToast({ text: 'An error occurred', variant: 'error' }))
}
}
return (
<GlobalScreen>
<FullScrollView>
<Stack.Screen
options={{
headerBackTitleVisible: false,
headerTitle: 'Create Post',
}}
/>
<Header
title="Create Post"
headerRight={() => (
<Pressable onPress={handleCreate}>
<IconCheck
strokeWidth={2.4}
size={SIZE_CONSTANT * 2.4}
color={COLORS.white}
/>
</Pressable>
)}
/>
<Text
style={{
fontSize: SIZES.xSmall,
color: '#3C3B3B',
letterSpacing: -0.01,
}}
>
Create Post Under
</Text>
{!subforumTitle && (
<AutoSuggestInput
onClear={() => {
setSelectedSubforum(null)
}}
onSelect={(option) => {
setSelectedSubforum(option)
}}
endpoint={'/post/search-post'}
/>
)}
{subforumTitle && (
<Text
style={{
fontSize: SIZES.medium,
color: COLORS.primary800,
fontWeight: 'bold',
}}
>
{subforumTitle}
</Text>
)}
<FormProvider {...form}>
<View
style={{
marginBottom: SIZE_CONSTANT * 4,
}}
></View>
<View
style={{
display: 'flex',
flexDirection: 'column',
}}
>
<TextField
value={title}
variant="filled"
name="title"
label="Title"
onChangeText={(e) => setTitle(e)}
style={{
width: '100%',
height: SIZE_CONSTANT * 4.8,
backgroundColor: '#FAFAFA',
padding: SIZE_CONSTANT * 1.5,
display: 'flex',
alignItems: 'center',
borderRadius: 6,
color: '#3C3B3B',
fontSize: SIZES.small,
fontWeight: 'regular',
borderWidth: 1,
borderColor: '#F8F8F8',
}}
/>
<ContentBox
onChange={(val) => {
setContent(val)
}}
/>
</View>
<View style={{ height: SIZE_CONSTANT * 24 }}></View>
</FormProvider>
</FullScrollView>
</GlobalScreen>
)
}