eletrotupi / tcc/ frontend/app/triggers/new.tsx71fdb8f591445852b09aef1f212869a412d68eb3
6.3 KB | 231 lines Raw
1
import {
2
  View,
3
  Text,
4
  StyleSheet,
5
  KeyboardAvoidingView,
6
  ScrollView,
7
} from "react-native";
8
import { useState } from "react";
9
import { router } from "expo-router";
10

11
import { Card } from "@/components/ui/Cards";
12
import { TextArea } from "@/components/ui/Input";
13
import { Button } from "@/components/ui/Button";
14
import { Section, SectionHeader } from "@/components/ui/Sections";
15
import { Spacing, Typography, Colors, BorderRadius } from "@/constants/theme";
16
import { Ionicons, MaterialIcons } from "@expo/vector-icons";
17
import { useCreateTrigger, useMoodEntries } from "@/hooks";
18
import { CategoryChips, CategoryOption } from "@/components/ui/CategoryChips";
19
import { TriggerCategory } from "@/constants/triggers";
20
import { TimePicker } from "@/components/ui/TimePicker";
21
import { getMood } from "@/constants/moods";
22
import { LinkRow } from "@/components/ui/LinkRow";
23

24
const TRIGGER_CATEGORIES: CategoryOption<TriggerCategory>[] = [
25
  {
26
    label: "Trabalho",
27
    value: "WORK",
28
    icon: <MaterialIcons size={16} color="#60A5FA" name="work" />,
29
  },
30
  {
31
    label: "Social",
32
    value: "SOCIAL",
33
    icon: <MaterialIcons size={16} color="#AF52DE" name="diversity-1" />,
34
  },
35
  {
36
    label: "Saúde",
37
    value: "HEALTH",
38
    icon: <MaterialIcons size={16} color="#34C759" name="healing" />,
39
  },
40
  {
41
    label: "Família",
42
    value: "FAMILY",
43
    icon: <MaterialIcons size={16} color="#FF9500" name="family-restroom" />,
44
  },
45
  {
46
    label: "Outros",
47
    value: "OTHER",
48
    icon: <Ionicons size={16} color="#8E8E93" name="ellipsis-horizontal" />,
49
  },
50
];
51

52
export default function NewTrigger() {
53
  const [moment, setMoment] = useState(new Date());
54
  const [comment, setComment] = useState("");
55
  const [category, setCategory] = useState("WORK");
56
  const createTrigger = useCreateTrigger();
57
  const [linkMood, setLinkMood] = useState(false);
58
  const { data: moodEntries, isLoading: isLoadingMood } = useMoodEntries({
59
    limit: 1,
60
  });
61

62
  const latestMood = moodEntries?.entries[0] ?? null;
63
  const moodDef = latestMood
64
    ? getMood(latestMood.selectedMood.toLowerCase())
65
    : null;
66

67
  const save = async () => {
68
    const data = {
69
      moment,
70
      comment,
71
      category,
72
    };
73

74
    await createTrigger.mutateAsync(data);
75
    router.replace("/(tabs)/actions");
76
  };
77

78
  return (
79
    <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
80
      <ScrollView scrollEventThrottle={16}>
81
        <View style={styles.container}>
82
          <View style={styles.header}>
83
            <View style={styles.innerHeaderWrapper}>
84
              <View style={[styles.headerIcon, { backgroundColor: "#FFF7ED" }]}>
85
                <Ionicons name="flash-outline" size={20} color="#EA580C" />
86
              </View>
87

88
              <View style={styles.headerTitleSubtitle}>
89
                <Text style={styles.headerTitle}>Aconteceu algo?</Text>
90
                <Text style={styles.headerSubtitle}>
91
                  Registre acontecimentos fora do padrão
92
                </Text>
93
              </View>
94
            </View>
95

96
            <Text style={styles.description}>
97
              Identificar gatilhos ajuda a entender melhor seus padrões
98
              emocionais
99
            </Text>
100
          </View>
101

102
          <Section>
103
            <SectionHeader title="Horário do Evento" />
104
            <TimePicker value={moment} onChange={setMoment} />
105
          </Section>
106

107
          <Section>
108
            <SectionHeader title="Selecionar uma categoria" />
109
            <CategoryChips
110
              options={TRIGGER_CATEGORIES}
111
              active={category}
112
              onChange={setCategory}
113
            />
114
          </Section>
115

116
          <Section>
117
            <SectionHeader title="Adicione detalhes" />
118

119
            <Card style={styles.mainCard}>
120
              <TextArea
121
                label="Comentários sobre"
122
                variant="darkGhost"
123
                onChangeText={(val) => setComment(val)}
124
                value={comment}
125
                minRows={5}
126
                maxRows={10}
127
                placeholder="Descreva o que gerou esse sentimento"
128
              />
129
            </Card>
130
          </Section>
131

132
          <Section>
133
            <SectionHeader title="Vincular registro" />
134

135
            <LinkRow
136
              icon={
137
                <View
138
                  style={[styles.iconWrap, { backgroundColor: "transparent" }]}
139
                >
140
                  <Text style={styles.moodIconEmoji}>
141
                    {moodDef?.icon ?? "😐"}
142
                  </Text>
143
                </View>
144
              }
145
              label="Humor atual"
146
              sublabel={
147
                isLoadingMood ? "..." : (moodDef?.label ?? "Nenhum registrado")
148
              }
149
              disabled={!latestMood}
150
              value={linkMood && !!latestMood}
151
              onToggle={setLinkMood}
152
            />
153

154
            <LinkRow
155
              icon={
156
                <MaterialIcons
157
                  name="psychology"
158
                  size={40}
159
                  color={Colors.light.disabled}
160
                />
161
              }
162
              label="Atividade"
163
              sublabel={"Nenhuma registrada"}
164
              disabled={true}
165
              value={false}
166
              onToggle={() => {
167
                console.log("When we get that");
168
              }}
169
            />
170
          </Section>
171

172
          <Button title="Salvar Registro" onPress={save} />
173
        </View>
174
      </ScrollView>
175
    </KeyboardAvoidingView>
176
  );
177
}
178

179
const styles = StyleSheet.create({
180
  container: {
181
    gap: 24,
182
    paddingHorizontal: Spacing.containerPadding,
183
    paddingVertical: Spacing.sectionGap,
184
    marginBottom: 50,
185
  },
186
  mainCard: {
187
    padding: Spacing.cardGap,
188
  },
189
  // Header
190
  header: {},
191
  headerIcon: {
192
    paddingVertical: 15,
193
    paddingHorizontal: 14,
194
    width: 48,
195
    borderRadius: BorderRadius.md,
196
  },
197
  innerHeaderWrapper: {
198
    flex: 1,
199
    flexDirection: "row",
200
    alignItems: "center",
201
  },
202
  headerTitleSubtitle: {
203
    marginLeft: 10,
204
  },
205
  headerTitle: {
206
    ...Typography.headlineMd,
207
  },
208
  headerSubtitle: {
209
    ...Typography.bodyMd,
210
    color: Colors.light.textSecondary,
211
  },
212
  description: {
213
    ...Typography.bodyMd,
214
    color: Colors.light.textSecondary,
215
    marginTop: 20,
216
  },
217
  keyboardView: {
218
    flex: 1,
219
  },
220
  moodIconEmoji: {
221
    fontSize: 26,
222
  },
223
  iconWrap: {
224
    width: 40,
225
    height: 40,
226
    borderRadius: 20,
227
    alignItems: "center",
228
    justifyContent: "center",
229
    marginTop: 1,
230
  },
231
});