eletrotupi / tcc/ frontend/app/triggers/new.tsx4dba6a8c834bf68ca293b235e85eae141a8c99ba
6.0 KB | 212 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 { TRIGGER_CATEGORY_DEFINITIONS } from "@/constants/trigger-categories";
21
import { TimePicker } from "@/components/ui/TimePicker";
22
import { getMood } from "@/constants/moods";
23
import { LinkRow } from "@/components/ui/LinkRow";
24

25
const TRIGGER_CATEGORIES: CategoryOption<TriggerCategory>[] =
26
  TRIGGER_CATEGORY_DEFINITIONS.map((d) => ({
27
    label: d.label,
28
    value: d.id,
29
    icon: <MaterialIcons size={16} color={d.color} name={d.icon} />,
30
  }));
31

32
export default function NewTrigger() {
33
  const [moment, setMoment] = useState(new Date());
34
  const [comment, setComment] = useState("");
35
  const [category, setCategory] = useState("WORK");
36
  const createTrigger = useCreateTrigger();
37
  const [linkMood, setLinkMood] = useState(false);
38
  const { data: moodEntries, isLoading: isLoadingMood } = useMoodEntries({
39
    limit: 1,
40
  });
41

42
  const latestMood = moodEntries?.entries[0] ?? null;
43
  const moodDef = latestMood
44
    ? getMood(latestMood.selectedMood.toLowerCase())
45
    : null;
46

47
  const save = async () => {
48
    const data = {
49
      moment,
50
      comment,
51
      category,
52
      ...(linkMood && latestMood ? { moodId: latestMood.id } : null),
53
    };
54

55
    await createTrigger.mutateAsync(data);
56
    router.replace("/(tabs)/actions");
57
  };
58

59
  return (
60
    <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
61
      <ScrollView scrollEventThrottle={16}>
62
        <View style={styles.container}>
63
          <View style={styles.header}>
64
            <View style={styles.innerHeaderWrapper}>
65
              <View style={[styles.headerIcon, { backgroundColor: "#FFF7ED" }]}>
66
                <Ionicons name="flash-outline" size={20} color="#EA580C" />
67
              </View>
68

69
              <View style={styles.headerTitleSubtitle}>
70
                <Text style={styles.headerTitle}>Aconteceu algo?</Text>
71
                <Text style={styles.headerSubtitle}>
72
                  Registre acontecimentos fora do padrão
73
                </Text>
74
              </View>
75
            </View>
76

77
            <Text style={styles.description}>
78
              Identificar gatilhos ajuda a entender melhor seus padrões
79
              emocionais
80
            </Text>
81
          </View>
82

83
          <Section>
84
            <SectionHeader title="Horário do Evento" />
85
            <TimePicker value={moment} onChange={setMoment} />
86
          </Section>
87

88
          <Section>
89
            <SectionHeader title="Selecionar uma categoria" />
90
            <CategoryChips
91
              options={TRIGGER_CATEGORIES}
92
              active={category}
93
              onChange={setCategory}
94
            />
95
          </Section>
96

97
          <Section>
98
            <SectionHeader title="Adicione detalhes" />
99

100
            <Card style={styles.mainCard}>
101
              <TextArea
102
                label="Comentários sobre"
103
                variant="darkGhost"
104
                onChangeText={(val) => setComment(val)}
105
                value={comment}
106
                minRows={5}
107
                maxRows={10}
108
                placeholder="Descreva o que gerou esse sentimento"
109
              />
110
            </Card>
111
          </Section>
112

113
          <Section>
114
            <SectionHeader title="Vincular registro" />
115

116
            <LinkRow
117
              icon={
118
                <View
119
                  style={[styles.iconWrap, { backgroundColor: "transparent" }]}
120
                >
121
                  <Text style={styles.moodIconEmoji}>
122
                    {moodDef?.icon ?? "😐"}
123
                  </Text>
124
                </View>
125
              }
126
              label="Humor atual"
127
              sublabel={
128
                isLoadingMood ? "..." : (moodDef?.label ?? "Nenhum registrado")
129
              }
130
              disabled={!latestMood}
131
              value={linkMood && !!latestMood}
132
              onToggle={setLinkMood}
133
            />
134

135
            <LinkRow
136
              icon={
137
                <MaterialIcons
138
                  name="psychology"
139
                  size={40}
140
                  color={Colors.light.disabled}
141
                />
142
              }
143
              label="Prática"
144
              sublabel={"Nenhuma registrada"}
145
              disabled={true}
146
              value={false}
147
              onToggle={() => {
148
                console.log("When we get that");
149
              }}
150
            />
151
          </Section>
152

153
          <Button title="Salvar Registro" onPress={save} />
154
        </View>
155
      </ScrollView>
156
    </KeyboardAvoidingView>
157
  );
158
}
159

160
const styles = StyleSheet.create({
161
  container: {
162
    gap: 24,
163
    paddingHorizontal: Spacing.containerPadding,
164
    paddingVertical: Spacing.sectionGap,
165
    marginBottom: 50,
166
  },
167
  mainCard: {
168
    padding: Spacing.cardGap,
169
  },
170
  // Header
171
  header: {},
172
  headerIcon: {
173
    paddingVertical: 15,
174
    paddingHorizontal: 14,
175
    width: 48,
176
    borderRadius: BorderRadius.md,
177
  },
178
  innerHeaderWrapper: {
179
    flex: 1,
180
    flexDirection: "row",
181
    alignItems: "center",
182
  },
183
  headerTitleSubtitle: {
184
    marginLeft: 10,
185
  },
186
  headerTitle: {
187
    ...Typography.headlineMd,
188
  },
189
  headerSubtitle: {
190
    ...Typography.bodyMd,
191
    color: Colors.light.textSecondary,
192
  },
193
  description: {
194
    ...Typography.bodyMd,
195
    color: Colors.light.textSecondary,
196
    marginTop: 20,
197
  },
198
  keyboardView: {
199
    flex: 1,
200
  },
201
  moodIconEmoji: {
202
    fontSize: 26,
203
  },
204
  iconWrap: {
205
    width: 40,
206
    height: 40,
207
    borderRadius: 20,
208
    alignItems: "center",
209
    justifyContent: "center",
210
    marginTop: 1,
211
  },
212
});