eletrotupi / tcc/ frontend/app/triggers/new.tsx23893a7b41f6b4b4ee7958a5d186afebd3f12827
5.2 KB | 184 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, useLocalSearchParams } 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, useLinkMoodToTrigger, usePatchCareAction } from "@/hooks";
18
import { useCareActionLinkStore } from "@/stores";
19
import { CategoryChips, CategoryOption } from "@/components/ui/CategoryChips";
20
import { TriggerCategory } from "@/constants/triggers";
21
import { TRIGGER_CATEGORY_DEFINITIONS } from "@/constants/trigger-categories";
22
import { TimePicker } from "@/components/ui/TimePicker";
23

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

31
export default function NewTrigger() {
32
  const [moment, setMoment] = useState(new Date());
33
  const [comment, setComment] = useState("");
34
  const { category: initialCategory, moodId: linkedMoodId } = 
35
    useLocalSearchParams<{ category?: string; moodId?: string }>();
36

37
  const [category, setCategory] = useState(initialCategory ?? "WORK");
38
  const createTrigger = useCreateTrigger();
39
  const linkMoodToTrigger = useLinkMoodToTrigger();
40
  const patchCareAction = usePatchCareAction();
41

42
  const save = async () => {
43
    const data = {
44
      moment,
45
      comment,
46
      category
47
    };
48

49
    const result = await createTrigger.mutateAsync(data);
50

51
    const careActionId = useCareActionLinkStore.getState().linkTrigger();
52
    if (careActionId) {
53
      await patchCareAction.mutateAsync({
54
        id: String(careActionId),
55
        data: { triggerId: result.trigger.id },
56
      });
57
    }
58

59
    if (linkedMoodId) {
60
      await linkMoodToTrigger.mutateAsync({
61
        triggerId: String(result.trigger.id),
62
        moodId: linkedMoodId,
63
        perceivedImpact: 3,
64
      });
65
      router.back();
66
    } else {
67
      router.replace("/(tabs)/actions");
68
    }
69
  };
70

71
  return (
72
    <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
73
      <ScrollView scrollEventThrottle={16}>
74
        <View style={styles.container}>
75
          <View style={styles.header}>
76
            <View style={styles.innerHeaderWrapper}>
77
              <View style={[styles.headerIcon, { backgroundColor: "#FFF7ED" }]}>
78
                <Ionicons name="flash-outline" size={20} color="#EA580C" />
79
              </View>
80

81
              <View style={styles.headerTitleSubtitle}>
82
                <Text style={styles.headerTitle}>Aconteceu algo?</Text>
83
                <Text style={styles.headerSubtitle}>
84
                  Registre acontecimentos fora do padrão
85
                </Text>
86
              </View>
87
            </View>
88

89
            <Text style={styles.description}>
90
              Identificar gatilhos ajuda a entender melhor seus padrões
91
              emocionais
92
            </Text>
93
          </View>
94

95
          <Section>
96
            <SectionHeader title="Horário do Evento" />
97
            <TimePicker value={moment} onChange={setMoment} />
98
          </Section>
99

100
          <Section>
101
            <SectionHeader title="Selecionar uma categoria" />
102
            <CategoryChips
103
              options={TRIGGER_CATEGORIES}
104
              active={category}
105
              onChange={setCategory}
106
            />
107
          </Section>
108

109
          <Section>
110
            <SectionHeader title="Adicione detalhes" />
111

112
            <Card style={styles.mainCard}>
113
              <TextArea
114
                label="Comentários sobre"
115
                variant="darkGhost"
116
                onChangeText={(val) => setComment(val)}
117
                value={comment}
118
                minRows={5}
119
                maxRows={10}
120
                placeholder="Descreva o que gerou esse sentimento"
121
              />
122
            </Card>
123
          </Section>
124

125
          <Button title="Salvar Registro" onPress={save} />
126
        </View>
127
      </ScrollView>
128
    </KeyboardAvoidingView>
129
  );
130
}
131

132
const styles = StyleSheet.create({
133
  container: {
134
    gap: 24,
135
    paddingHorizontal: Spacing.containerPadding,
136
    paddingVertical: Spacing.sectionGap,
137
    marginBottom: 50,
138
  },
139
  mainCard: {
140
    padding: Spacing.cardGap,
141
  },
142
  // Header
143
  header: {},
144
  headerIcon: {
145
    paddingVertical: 15,
146
    paddingHorizontal: 14,
147
    width: 48,
148
    borderRadius: BorderRadius.md,
149
  },
150
  innerHeaderWrapper: {
151
    flex: 1,
152
    flexDirection: "row",
153
    alignItems: "center",
154
  },
155
  headerTitleSubtitle: {
156
    marginLeft: 10,
157
  },
158
  headerTitle: {
159
    ...Typography.headlineMd,
160
  },
161
  headerSubtitle: {
162
    ...Typography.bodyMd,
163
    color: Colors.light.textSecondary,
164
  },
165
  description: {
166
    ...Typography.bodyMd,
167
    color: Colors.light.textSecondary,
168
    marginTop: 20,
169
  },
170
  keyboardView: {
171
    flex: 1,
172
  },
173
  moodIconEmoji: {
174
    fontSize: 26,
175
  },
176
  iconWrap: {
177
    width: 40,
178
    height: 40,
179
    borderRadius: 20,
180
    alignItems: "center",
181
    justifyContent: "center",
182
    marginTop: 1,
183
  },
184
});