aboutsummaryrefslogtreecommitdiff
path: root/src/components/EditPage.vue
blob: 0f3ae9d1b5057e3d22d525e3ce09c88723cded98 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
  <v-switch
    v-model="switchModel"
    label="I want to create a new SubCard"
  ></v-switch>

  <v-form v-show="switchModel" lazy-validate ref="form1" v-model="valid1">
    <v-text-field
      v-model="title"
      :counter="titleMaxLength"
      :rules="titleRules"
      label="Title"
      required
    ></v-text-field>

    <v-textarea
      auto-grow
      clearable
      clear-icon="mdi-close-circle"
      outlined
      name="textarea"
      label="Content"
      v-model="content"
      :rules="contentRules"
      required
    ></v-textarea>
    <v-row class="d-flex justify-end">
      <v-btn
        :disabled="!valid1"
        variant="outlined"
        class="mr-4"
        @click="
          validate(this.$refs.form1);
          write(this.$refs.form1);
        "
      >
        Save
      </v-btn>

      <v-btn variant="outlined" class="mr-4" @click="reset(this.$refs.form1)">
        Reset Form
      </v-btn>
    </v-row>
  </v-form>

  <v-form v-show="!switchModel" lazy-validate ref="form2" v-model="valid2">
    <v-select
      v-model="select"
      variant="outlined"
      :items="items"
      :rules="[(v) => !!v || 'Item is required']"
      label="Item"
      required
    ></v-select>

    <v-textarea
      auto-grow
      clearable
      clear-icon="mdi-close-circle"
      outlined
      name="textarea"
      label="Content"
      v-model="content"
      :rules="contentRules"
      required
    ></v-textarea>
    <v-row class="d-flex justify-end">
      <v-btn
        :disabled="!valid2"
        variant="outlined"
        class="mr-4"
        @click="
          validate(this.$refs.form2);
          write(this.$refs.form2);
        "
      >
        Save
      </v-btn>

      <v-btn variant="outlined" class="mr-4" @click="reset(this.$refs.form2)">
        Reset Form
      </v-btn>
      <v-btn variant="outlined" class="mr-4" @click="deleteItem">
        Delete Item
      </v-btn>
    </v-row>
  </v-form>
</template>

<script>
export default {
  name: "EditPage",

  emits: ["snackbarNotification"],

  data: () => ({
    valid1: true,
    valid2: true,
    title: "",
    titleMaxLength: 15,
    titleRules: [
      (v) => !!v || "Title is required",
      (v) => {
        let titleMaxLength = 15;
        return (
          (v && v.length <= titleMaxLength) ||
          `Title must be less than ${titleMaxLength} characters`
        );
      },
    ],
    content: "",
    contentRules: [(v) => !!v || "Content is required"],
    select: null,
    items: [],
    switchModel: false,
    LSData: {},
  }),

  mounted() {
    this.loadFromLS();
    this.readAllItems();
  },

  methods: {
    validate(form) {
      form.validate();
    },
    reset(form) {
      // Resets the state of all registered inputs (inside the form) to {} for arrays and null for all other values.
      // Resets errors bag when using the lazy-validation prop.
      form.reset();
    },
    resetValidation(form) {
      // Resets validation of all registered inputs without modifying their state
      form.resetValidation();
    },
    write(form) {
      if (form == this.$refs.form1 && this.title in this.LSData) {
        this.$emit("snackbarNotification", "Title already exists");
        return;
      }
      if (form == this.$refs.form2) {
        // Sync LSData(title) with form data
        this.title = this.select;
      }
      this.LSData[this.title] = this.content;
      this.writeToLS();
      this.$emit("snackbarNotification", "Done!");
      // Let's do a validation reset.
      this.resetValidation(form);
      // Re-read all items.
      this.readAllItems();
    },
    readAllItems() {
      this.items = []; // Reset items list
      for (let key in this.LSData) {
        this.items.push(key);
      }
    },
    deleteItem() {
      delete this.LSData[this.select];
      this.writeToLS();
      // Re-read all items.
      this.readAllItems();
      this.$emit("snackbarNotification", "Item deleted");
      this.reset(this.$refs.form2);
    },

    // LocalStorage
    loadFromLS() {
      this.LSData = JSON.parse(localStorage.getItem("LSData")) ?? this.LSData;
    },
    writeToLS() {
      localStorage.setItem("LSData", JSON.stringify(this.LSData));
    },
  },

  watch: {
    // eslint-disable-next-line no-unused-vars
    select(newSelected, _) {
      // Load content whenever select changes
      this.content = this.LSData[newSelected];
    },
  },
};
</script>