aboutsummaryrefslogtreecommitdiffstats
path: root/components/NoteView.js
blob: 762ac2b2aa05a0aed27978b8523dd934609c0d54 (plain) (blame)
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
import * as React from 'react';
import { SafeAreaView, TextInput, StyleSheet, Text, TouchableOpacity, Dimensions } from 'react-native';
import { Picker } from '@react-native-picker/picker';
import * as SecureStore from 'expo-secure-store';

export default class NoteView extends React.Component {
  constructor(props) {
    super(props)
    const note = props.route.params?.note ?? {};
    this.state = {
      note: note,
      isNew: Object.keys(note).length === 0,
      title: note.title ?? "",
      body: note.body ?? "",
      category: note.category ?? "",
      ti1: React.createRef(),
      ti2: React.createRef(),
      categories: [],
    }
    this.setTitle = this.setTitle.bind(this)
    this.setBody = this.setBody.bind(this)
    this.setCategory = this.setCategory.bind(this)
    this.setCategories = this.setCategories.bind(this)
    this.addNote = this.addNote.bind(this)
    this.funkcja = () => null;
  }
  componentDidMount() {
    const a = () => {
      const n = this.props.route.params?.note ?? {};
      getCategories(this.setCategories, this.setCategory, this.state.category)
      console.log('note', n, '     ', Date.now())
      this.setState({ note: n })
      this.setTitle(n.title ?? "")
      this.setBody(n.body ?? "")
      this.setCategory(n.category ?? "")
    }
    a()
    this.props.navigation.addListener('focus', a)
  }
  componentWillUnmount() {
    this.funkcja();
  }

  setTitle(t) { this.setState({ title: t }) }
  setBody(t) { this.setState({ body: t }) }
  setCategory(t) { this.setState({ category: t }) }
  setCategories(t) { this.setState({ categories: t }) }

  async addNote() {
    try {
      let nk = await SecureStore.getItemAsync('notesKeys')
      let notesKeys = []
      if (!nk) notesKeys = [0]; else notesKeys = JSON.parse(nk);
      const key = this.state.isNew ? Math.max(...notesKeys) + 1 : this.state.note.key;
      const date = Date.now();
      const colors = ["#49d96c", "#f5ac58", "pink", "darkcyan"]
      const color = this.state.isNew ? colors[Math.floor(Math.random() * colors.length)] : this.state.note.color
      if (this.state.isNew)
        notesKeys.push(key);
      let [title, body, category] = [this.state.title, this.state.body, this.state.category]
      if (category === "") category = this.state.categories[0]
      await SecureStore.setItemAsync(key.toString(), JSON.stringify({ title, body, date, color, key, category }))
      await SecureStore.setItemAsync('notesKeys', JSON.stringify(notesKeys.filter(el => el !== 0)))
      console.log(key.toString(), JSON.stringify({ title, body, date, color, key, category }))
      console.log('notesKeys', JSON.stringify(notesKeys.filter(el => el !== 0)))
      console.log('categories', this.state.categories)

      // this.state.ti1.current.clear()
      // this.state.ti2.current.clear()

      this.props.navigation.navigate('notesList')
    } catch (e) {
      console.error(e)
    }
  }
  render() {
    return (
      <SafeAreaView style={styles.cont}>
        <TextInput
          underlineColorAndroid="#FFFFFF"
          placeholder="TITLE..."
          placeholderTextColor="#FFFFFF"
          onChangeText={this.setTitle}
          value={this.state.title}
          style={styles.input}
          ref={this.state.ti1}
        />
        <TextInput
          underlineColorAndroid="#FFFFFF"
          placeholder="BODY..."
          placeholderTextColor="#FFFFFF"
          onChangeText={this.setBody}
          value={this.state.body}
          style={styles.input}
          multiline={true}
          ref={this.state.ti2}
        />
        <Picker
          selectedValue={this.state.category}
          onValueChange={(v, _) => this.setCategory(v)}
          style={styles.picker}
          itemStyle={styles.pickerItem}
        >
          {this.state.categories.map((c, i) => <Picker.Item value={c} label={c} key={i} />)}
        </Picker>
        <TouchableOpacity onPress={this.addNote}><Text style={styles.txt}>{this.state.isNew ? "Add" : "Confirm"}</Text></TouchableOpacity>
      </SafeAreaView>
    )
  }
}
async function getCategories(setCategories, setCategory, category) {
  let categories = await SecureStore.getItemAsync('categories')
  if (!categories) categories = ["default"]; else categories = JSON.parse(categories);
  setCategories(categories);
  if (category === "")
    setCategory(categories[0])
}

const styles = StyleSheet.create({
  cont: {
    flex: 1,
    backgroundColor: "black",
    padding: 20,
    alignItems: "center"
  },
  ico: {
    width: 200,
    height: 200,
  },
  txt: {
    fontSize: 30,
    fontWeight: "bold",
    color: "#FFFFFF"
  },
  picker: {
    color: "white",
    borderColor: "white",
    width: 200,
    height: 30,
    backgroundColor: "darkgrey",
    margin: 15,
  },
  pickerItem: {
    backgroundColor: "black",
    color: "white",
  },
  input: {
    // height: 40,
    width: "100%",
    margin: 12,
    borderWidth: 1,
    padding: 10,
    color: "#FFFFFF",
  }
})