-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.js
179 lines (148 loc) · 5.17 KB
/
utils.js
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
/*eslint no-process-env: 0*/
import alertifyjs from 'alertifyjs';
import moment from 'moment';
import {
filter, difference, compact, find, reject, intersectionBy, size, keys, omitBy, isEmpty,
get, includes, map, isArray, values, pick
} from 'lodash';
import { DATE_FORMAT, DATETIME_FORMAT } from './constants';
export const isAtGlobalSearch = () => {
return window.location.hash.includes('#/search');
}
export const formatDate = date => {
return moment(date).format(DATE_FORMAT);
}
export const formatDateTime = date => {
return moment(date).format(DATETIME_FORMAT);
}
export const getIndirectMappings = (mappings, concept) => {
return filter(mappings, {to_concept_code: concept});
}
export const getDirectMappings = (mappings, concept) => {
return filter(mappings, {from_concept_code: concept});
}
export const getDirectExternalMappings = (mappings, concept) => {
return filter(mappings, mapping => Boolean(mapping.from_concept_code === concept && mapping.external_id));
}
export const getLinkedQuestions = (mappings, concept) => {
return filter(mappings, {to_concept_code: concept, map_type: 'Q-AND-A'});
}
export const getLinkedAnswers = (mappings, concept) => {
return filter(mappings, {from_concept_code: concept, map_type: 'Q-AND-A'});
}
export const getSetParents = (mappings, concept) => {
return filter(mappings, {to_concept_code: concept, map_type: 'CONCEPT-SET'});
}
export const getSetMembers = (mappings, concept) => {
return filter(mappings, {from_concept_code: concept, map_type: 'CONCEPT-SET'});
}
export const getMappingsDistributionByMapType = (mappings, concept) => {
const linkedQuestions = getLinkedQuestions(mappings, concept);
const linkedAnswers = getLinkedAnswers(mappings, concept);
const setParents = getSetParents(mappings, concept);
const setMembers = getSetMembers(mappings, concept);
const directExternalMappings = getDirectExternalMappings(
difference(mappings, [...linkedAnswers, ...linkedQuestions, ...setParents, ...setMembers]),
concept
);
const directInternalMappings = getDirectMappings(
difference(mappings, [...linkedAnswers, ...linkedQuestions, ...setParents, ...setMembers, ...directExternalMappings]),
concept
);
const indirectMappings = getIndirectMappings(
difference(mappings, [...linkedAnswers, ...linkedQuestions, ...setParents, ...setMembers, ...directExternalMappings, ...directInternalMappings]),
concept
);
return {
'Linked Question': linkedQuestions,
'Linked Answers': linkedAnswers,
'Set Parent': setParents,
'Set Members': setMembers,
'Direct External Mapping': directExternalMappings,
'Direct Internal Mapping': directInternalMappings,
'Inverse Mapping': indirectMappings,
}
}
export const toFullURL = uri => {
return window.location.origin + '/#' + uri;
}
export const toFullAPIURL = uri => {
/*eslint no-undef: 0*/
const APIURL = window.API_URL || process.env.API_URL;
return APIURL + uri;
}
export const copyURL = url => {
if(url) {
navigator.clipboard.writeText(url);
alertifyjs.success('Copied URL to clipboard!')
}
}
export const toParentURI = uri => {
return uri.split('/').splice(0, 5).join('/');
};
export const toOwnerURI = uri => {
return uri.split('/').splice(0, 3).join('/');
};
export const headFirst = versions => {
return compact([
find(versions, {version: 'HEAD'}), ...reject(versions, {version: 'HEAD'})
]);
};
export const currentUserToken = () => {
return localStorage.token;
};
export const isLoggedIn = () => {
return Boolean(currentUserToken());
};
export const getCurrentUser = () => {
const data = localStorage.user;
if(data)
return JSON.parse(data);
return null;
};
export const getCurrentUserOrgs = () => {
return get(getCurrentUser(), 'subscribed_orgs');
};
export const getCurrentUserUsername = () => {
return get(getCurrentUser(), 'username');
};
export const nonEmptyCount = (object, attributes) => {
return size(intersectionBy(keys(omitBy(
object, val => (isEmpty(val) || includes(['none', 'None'], val))
)), attributes));
}
export const isCurrentUserMemberOf = orgId => {
return orgId && includes(map(getCurrentUserOrgs(), 'id'), orgId);
}
export const defaultCreatePin = (resourceType, resourceId, service, callback) => {
if(service) {
service.post({resource_type: resourceType, resource_id: resourceId}).then(response => {
if(get(response, 'error')) {
let error;
if(isArray(response.error) && !isEmpty(compact(response.error)))
error = compact(response.error)[0];
else
error = get(values(response.error), '0') || 'Something bad happened.';
alertifyjs.error(error);
} else if(callback && get(response, 'status') === 201)
callback(response.data);
});
}
}
export const defaultDeletePin = (service, callback) => {
if(service) {
service.delete().then(response => {
if(callback && get(response, 'status') === 204)
callback();
});
}
}
export const isAdminUser = () => {
const currentUser = getCurrentUser();
return get(currentUser, 'is_staff') || get(currentUser, 'is_superuser');
}
export const toObjectArray = obj => {
if(isEmpty(obj))
return [];
return map(keys(obj), k => pick(obj, k));
}