Advertisement
Guest User

Untitled

a guest
Sep 24th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import {Text, View, StyleSheet, TextInput, Button, Alert} from "react-native";
  2. import {Redirect, router, Stack} from "expo-router";
  3. import {useState} from "react";
  4. import Feather from '@expo/vector-icons/Feather';
  5. import {useAuth} from "../../providers/AuthProvider";
  6. import {supabase} from "../../lib/supabase";
  7.  
  8. export default function CreatePoll() {
  9. const [question, setQuestion] = useState('');
  10. const [options, setOptions] = useState(['', '']);
  11. const [error, setError] = useState()
  12. const {session, user} = useAuth();
  13.  
  14. const createPoll = () => {
  15. // @ts-ignore
  16. setError("");
  17. if (!question) {
  18. // @ts-ignore
  19. setError("Please enter a question");
  20. return;
  21. }
  22. const validOptions = options.filter(o => !! o)
  23. if (validOptions.length < 2) {
  24. // @ts-ignore
  25. setError("Please provide at least 2 valid options")
  26. return;
  27. }
  28.  
  29. const { data, error } = await supabase
  30. .from('polls')
  31. .insert([
  32. { question, options },
  33. ])
  34. .select()
  35.  
  36. if (error) {
  37. // @ts-ignore
  38. setError(error.message);
  39. return;
  40. }
  41.  
  42.  
  43. router.back();
  44.  
  45. console.warn("create")
  46. }
  47.  
  48. if (!user) {
  49. return <Redirect href={"/profile"}/>;
  50. }
  51.  
  52. return (
  53. <View style={styles.container}>
  54. <Stack.Screen options={{title: 'Create poll'}}/>
  55.  
  56. <Text style={styles.label}>Title</Text>
  57. <TextInput value={question} onChangeText={setQuestion} placeholder='Type your question here' style={styles.input}/>
  58.  
  59. <Text style={styles.label}>Options</Text>
  60.  
  61. {options.map((option, index) => (
  62. <View key={index} style={{justifyContent: 'center'}}>
  63. <TextInput
  64. value={option}
  65. onChangeText={(text ) => {
  66. const updated = [...options];
  67. updated[index] = text;
  68. setOptions(updated);
  69. }}
  70. placeholder={`Option ${index + 1}`}
  71. style={styles.input}
  72. />
  73. <Feather
  74. name="x"
  75. size={22}
  76. color="black"
  77. onPress={() => {
  78. const updated = [...options];
  79. updated.splice(index, 1);
  80. setOptions(updated);
  81. }}
  82. style={{
  83. position: 'absolute',
  84. right: 10,
  85. }}
  86. />
  87. </View>
  88. ))}
  89. <Button
  90. title='Add Option'
  91. onPress={() => setOptions([...options, ''])}
  92. />
  93.  
  94. <Button title='Create Poll' onPress={createPoll}/>
  95. <Text style={styles.labelError}>{error}</Text>
  96. </View>
  97. );
  98. }
  99.  
  100. const styles = StyleSheet.create ({
  101. container: {
  102. padding: 10,
  103. gap: 5,
  104. },
  105. label: {
  106. fontSize: 18,
  107. marginTop: 7,
  108. },
  109. input: {
  110. borderWidth: 1,
  111. borderColor: '#ddd',
  112. backgroundColor: 'white',
  113. padding: 10,
  114. marginBottom: 7.5,
  115. borderRadius: 5,
  116. fontSize: 16,
  117. },
  118. labelError: {
  119. color:'crimson',
  120. fontSize: 14,
  121. }
  122.  
  123. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement