-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathprofile.js
More file actions
112 lines (100 loc) · 3.09 KB
/
profile.js
File metadata and controls
112 lines (100 loc) · 3.09 KB
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
import Controller from '@ember/controller';
import { APPS } from '../constants/urls';
import { tracked } from '@glimmer/tracking';
import { action, set } from '@ember/object';
import { inject as service } from '@ember/service';
import { TOAST_OPTIONS } from '../constants/toast-options';
import { profile_fields } from '../constants/profile-field';
const BASE_URL = APPS.API_BACKEND;
export default class ProfileController extends Controller {
@service toast;
get imageUploadUrl() {
return `${BASE_URL}/users/picture`;
}
@tracked formDataKeyName = 'profile';
@tracked showEditProfilePictureModal = false;
@tracked title = 'Profile Details';
@tracked isSubmitDisabled = true;
@tracked userId = this.model.id || '';
@tracked formData = {
first_name: this.model.first_name,
last_name: this.model.last_name,
company: this.model.company,
designation: this.model.designation,
linkedin_id: this.model.linkedin_id,
twitter_id: this.model.twitter_id,
website: this.model.website,
};
@tracked fields = profile_fields;
@action handleFieldChange(name, value) {
set(this.formData, name, value);
const anyErrors = this.fields.map((field) => {
let hasError = false;
if (field.required && this.formData[field.id] === '') {
hasError = true;
}
return hasError;
});
this.isSubmitDisabled = anyErrors.filter(Boolean).length;
}
@action handleFieldValidation(id, isValid) {
const index = this.fields.findIndex((field) => field.id === id);
if (isValid) {
set(this.fields[index], 'showError', false);
} else {
set(this.fields[index], 'showError', true);
}
}
removeEmptyFields(reqObject) {
const objectRequested = reqObject;
for (const field in objectRequested) {
if (!objectRequested[field]) {
delete objectRequested[field];
} else if (field === 'yoe') {
objectRequested[field] = parseInt(objectRequested[field]);
}
}
return objectRequested;
}
@action async handleSubmit(e) {
e.preventDefault();
const cleanReqObject = this.removeEmptyFields(this.formData);
try {
const response = await fetch(
`${BASE_URL}/users/${this.userId}?profile=true&dev=true`,
{
method: 'PATCH',
body: JSON.stringify(cleanReqObject),
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
},
);
const { status } = response;
if (status === 204) {
this.toast.success('Updated details successfully', '', {
...TOAST_OPTIONS,
timeOut: '1000',
});
} else if (status !== 204) {
this.toast.error(
'Something went wrong. Please check console errors.',
'',
TOAST_OPTIONS,
);
}
} catch (error) {
console.error('Error : ', error);
}
}
@action handleShowEditProfilePictureModal() {
this.showEditProfilePictureModal = true;
}
@action closeModal() {
this.showEditProfilePictureModal = false;
}
@action refreshRoute() {
this.send('refreshModel');
}
}