Newer
Older
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
<template>
<div
class="md:ring-1 ring-gray-300 rounded-xl overflow-hidden mx-auto mb-auto max-w-md w-full p-4"
>
<!-- Component heading -->
<h3 class="text-xl font-medium text-center text-primary-light mt-4 mb-8">
Rediger gjenstand
</h3>
<!-- Title -->
<div class="mb-6" :class="{ error: v$.updatedItem.title.$errors.length }">
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
id="titleLabel"
>Tittel</label
>
<input
type="text"
id="title"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
v-model="v$.updatedItem.title.$model"
required
/>
<!-- error message for title-->
<div
class="text-error-medium"
v-for="(error, index) of v$.updatedItem.title.$errors"
:key="index"
>
<div class="text-error-medium text-sm">
{{ error.$message }}
</div>
</div>
</div>
<!-- Category -->
<div class="mb-6">
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
id="selectCategoryLabel"
>Kategori</label
>
<select
v-model="v$.updatedItem.selectedCategory.$model"
id="categories"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
>
<option class="text-gray-400" value="" disabled>
Velg en kategori
</option>
<option
:selected="category == updatedItem.selectedCategory"
v-for="category in categories"
:key="category"
class="text-gray-900 text-sm"
>
{{ category }}
</option>
</select>
<!-- error message for select box -->
<div
class="text-error-medium"
v-for="(error, index) of v$.updatedItem.selectedCategory.$errors"
:key="index"
>
<div class="text-error-medium text-sm">
{{ error.$message }}
</div>
</div>
</div>
<!-- Grupper -->
<div class="mb-6">
<label class="block text-sm font-medium text-gray-900 dark:text-gray-400"
>Grupper</label
>
<div
class="overflow-auto w-full max-h-32 mt-2 text-base list-none bg-white rounded divide-y divide-gray-100 dark:bg-gray-700"
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
>
<ul class="py-1" aria-labelledby="dropdownDefault">
<li>
<div
class="form-check"
v-for="community in communities"
:key="community"
>
<input
class="form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-primary-medium focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer"
type="checkbox"
:checked="isInSelectedCommunity(community.communityId)"
:value="community.communityId"
@change="onChangeCommunity($event)"
/>
<label class="form-check-label inline-block text-gray-800">
{{ community.name }}
</label>
</div>
</li>
</ul>
</div>
<label class="text-error-medium text-sm block">{{
communityErrorMessage
}}</label>
</div>
<!-- price -->
<div
class="mb-6 mt-4"
:class="{ error: v$.updatedItem.price.$errors.length }"
>
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
id="priceLabel"
>Pris</label
>
<input
type="number"
v-model="v$.updatedItem.price.$model"
id="price"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
required
/>
<!-- error message for price -->
<div
class="text-error"
v-for="(error, index) of v$.updatedItem.price.$errors"
:key="index"
>
<div class="text-error-medium text-sm">
{{ error.$message }}
</div>
</div>
</div>
<!-- Description -->
<div
class="mb-6"
:class="{ error: v$.updatedItem.description.$errors.length }"
>
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
id="descriptionLabel"
>Beskrivelse</label
>
<textarea
id="description"
rows="4"
v-model="v$.updatedItem.description.$model"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
required
></textarea>
<!-- error message for description -->
<div
class="text-error"
v-for="(error, index) of v$.updatedItem.description.$errors"
:key="index"
>
<div class="text-error-medium text-sm">
{{ error.$message }}
</div>
</div>
</div>
<!-- Address -->
<div class="mb-6" :class="{ error: v$.updatedItem.address.$errors.length }">
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
id="addressLabel"
>Adresse</label
>
<input
type="text"
class="block w-full px-4 py-2 mt-2 text-gray-700 placeholder-gray-500 bg-white border rounded-md dark:bg-gray-800 dark:border-gray-600 dark:placeholder-gray-400 focus:border-primary-light dark:focus:border-primary-light focus:ring-opacity-40 focus:outline-none focus:ring focus:ring-primary-light"
v-model="v$.updatedItem.address.$model"
id="adress"
required
/>
<!-- error message for address-->
<div
class="text-error"
v-for="(error, index) of v$.updatedItem.address.$errors"
:key="index"
>
<div class="text-error-medium text-sm">
{{ error.$message }}
</div>
</div>
</div>
<!-- Images -->
<div>
<label
class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
id="imageLabel"
>
</label>
<input
type="file"
ref="file"
style="display: none"
@change="addImage"
<ColoredButton :text="'Velg bilde'" @click="$refs.file.click()" />
<div v-for="image in updatedItem.images" :key="image" class="m-2">
<form-image-display :image="image" @remove="removeImage(image)" />
</div>
</div>
<!-- Save item button -->
<div class="float-right">
<ColoredButton :text="'Lagre'" @click="saveClicked" id="saveButton" />
</div>
</div>
</template>
<script>
import useVuelidate from "@vuelidate/core";
import ColoredButton from "@/components/BaseComponents/ColoredButton";
import FormImageDisplay from "@/components/BaseComponents/FormImageDisplay.vue";
import ListingService from "@/services/listing.service";
import CommunityService from "@/services/community.service";
import { parseCurrentUser } from "@/utils/token-utils";
import {
required,
helpers,
maxLength,
between,
minLength,
} from "@vuelidate/validators";
export default {
name: "EditNewItem",
components: {
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
},
setup() {
return { v$: useVuelidate() };
},
validations() {
return {
updatedItem: {
title: {
required: helpers.withMessage(
() => "Tittelen kan ikke være tom",
required
),
max: helpers.withMessage(
() => `Tittelen kan inneholde max 50 tegn`,
maxLength(50)
),
},
description: {
required: helpers.withMessage(
() => "Beskrivelsen kan ikke være tom",
required
),
max: helpers.withMessage(
() => `Beskrivelsen kan inneholde max 200 tegn`,
maxLength(200)
),
min: helpers.withMessage(
() => `Beskrivelsen kan ikke være tom`,
minLength(0)
),
},
price: {
required,
between: helpers.withMessage(
() => `Leieprisen kan ikke være større enn 25000`,
between(0, 25000)
),
},
selectedCategory: {
required: helpers.withMessage(() => `Velg en kategori`, required),
},
address: {
required: helpers.withMessage(
() => "Addressen kan ikke være tom",
required
),
max: helpers.withMessage(
() => `Addressen kan inneholde max 50 tegn`,
maxLength(50)
),
},
},
};
},
data() {
return {
updatedItem: {
title: "",
description: "",
address: "",
price: "",
category: "",
selectedCategory: "",
selectedCategories: [],
userId: -1,
selectedCommunityId: -1,
selectedCommunities: [],
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
},
categories: [
"Antikviteter og kunst",
"Dyr og utstyr",
"Elektronikk og hvitevarer",
"Foreldre og barn",
"Fritid, hobby og underholdning",
"Hage, oppussing og hus",
"Klær, kosmetikk og tilbehør",
"Møbler og interiør",
"Næringsvirksomhet",
"Sport og friluftsliv",
"Utstyr til bil, båt og MC",
],
initialItem: {},
communities: [],
communityErrorMessage: "",
images: [],
};
},
methods: {
checkValidation() {
this.v$.updatedItem.$touch();
if (
this.v$.updatedItem.$invalid ||
this.updatedItem.selectedCommunities.length === 0
) {
if (this.updatedItem.selectedCommunities.length === 0) {
this.communityErrorMessage = "Velg gruppe/grupper";
}
return false;
}
return true;
},
async saveClicked() {
if (this.checkValidation()) {
let itemInfo = {
listingID: parseInt(this.initialItem.listingID),
title: this.updatedItem.title,
description: this.updatedItem.description,
pricePerDay: this.updatedItem.price,
address: this.updatedItem.address,
userID: this.updatedItem.userId,
categoryNames: this.updatedItem.selectedCategories,
communityIDs: this.updatedItem.selectedCommunities,
};
await ListingService.putItem(itemInfo);
await ImageService.putListingImages(
this.initialItem.listingID,
this.updatedItem.images
);
this.$router.push("/itempage/" + this.initialItem.listingID);
}
},
async addImage(event) {
var that = this;
let image = event.target.files[0];
let fileReader = new FileReader();
fileReader.onloadend = async function () {
const res = fileReader.result;
const id = await ImageService.postNewImage(res);
const API_URL = process.env.VUE_APP_BASEURL;
let alreadyInGroupList = false;
for (let i = 0; i <= this.updatedItem.selectedCommunities.length; i++) {
if (
this.updatedItem.selectedCommunityId ==
this.updatedItem.selectedCommunities[i]
) {
const index = this.updatedItem.selectedCommunities.indexOf(
}
alreadyInGroupList = true;
}
}
if (!alreadyInGroupList) {
this.updatedItem.selectedCommunities.push(
this.updatedItem.selectedCommunityId
);
onChangeCategory(e) {
this.updatedItem.selectedCategory = e.target.value;
this.updatedItem.selectedCategories = [e.target.value];
},
isInSelectedCommunity(id) {
for (let i in this.updatedItem.selectedCommunities) {
if (this.updatedItem.selectedCommunities[i] == id) {
return true;
}
}
return false;
},
for (let i in this.updatedItem.images) {
if (this.updatedItem.images[i] != image) {
let itemID = await this.$router.currentRoute.value.params.id;
let item = await ListingService.getItem(itemID);
// Check if user is the owner of the item
let userID = await parseCurrentUser().userId;
if (item.userID == userID) {
this.$router.push(this.$router.options.history.state.back);
}
this.initialItem = item;
this.communities = await CommunityService.getUserCommunities();
let imageURLS = [];
for (let i in this.images) {
imageURLS.push(this.images[i].picture);
}
let initialCategories = [];
for (let i in this.initialItem.categoryNames) {
initialCategories.push(this.initialItem.categoryNames[i]);
}
let selectedCategory =
initialCategories.length > 0 ? initialCategories[0] : "";
let initialCommunities = [];
for (let i in this.initialItem.communityIDs) {
initialCommunities.push(this.initialItem.communityIDs[i]);
}
this.updatedItem = {
title: this.initialItem.title,
description: this.initialItem.description,
address: this.initialItem.address,
price: this.initialItem.pricePerDay,
selectedCategories: initialCategories,
selectedCategory: selectedCategory,