Skip to content

Commit 24cc8c1

Browse files
Add types to query-suggestions' dependencies, import, and refactor to remove immutableJS
which is no longer needed for flatMap etc.
1 parent fc79b71 commit 24cc8c1

4 files changed

Lines changed: 144 additions & 86 deletions

File tree

kahuna/public/js/components/gr-cql-input/gr-cql-input.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ grCqlInput.directive<
2626
"querySuggestions",
2727
function (querySuggestions: QuerySuggestionsService) {
2828
const fields: TypeaheadField[] = querySuggestions.typeaheadFields.map(
29-
({ fieldName, resolver }) => {
29+
({ fieldName, resolver, type = "TEXT" }) => {
3030
const mappedResolver = resolver
3131
? async (fieldName: string) => {
3232
const suggestions = await (Array.isArray(resolver)
@@ -35,7 +35,7 @@ grCqlInput.directive<
3535

3636
return suggestions.map((suggestion) => ({
3737
label: suggestion,
38-
value: suggestion
38+
value: suggestion,
3939
}));
4040
}
4141
: undefined;
@@ -45,6 +45,7 @@ grCqlInput.directive<
4545
fieldName,
4646
undefined,
4747
mappedResolver,
48+
type,
4849
);
4950
},
5051
);
Lines changed: 72 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import * as angular from "angular";
2-
import { List, Map } from "immutable";
32

4-
import { mediaApi } from "../../services/api/media-api";
3+
import { MediaApi, mediaApi } from "../../services/api/media-api";
4+
import { EditsApi } from "../../services/api/edits-api";
55

66
export const querySuggestions = angular.module("querySuggestions", [
7-
mediaApi.name
7+
mediaApi.name,
88
]);
99

1010
export type FieldAlias = {
11+
elasticsearchPath: string;
12+
label: string;
13+
displayInAdditionalMetadata: boolean;
1114
displaySearchHint: boolean;
1215
alias: string;
13-
searchHintOptions: any;
16+
searchHintOptions: string[];
1417
};
1518

1619
export type Chip = {
@@ -39,7 +42,7 @@ const subjects = [
3942
"social",
4043
"sport",
4144
"war",
42-
"weather"
45+
"weather",
4346
];
4447

4548
const fileTypes = ["jpeg", "tiff", "png"];
@@ -52,35 +55,28 @@ const isSearch = [
5255
`${staffPhotographerOrganisation}-owned-illustration`,
5356
`${staffPhotographerOrganisation}-owned`,
5457
"under-quota",
55-
"deleted"
58+
"deleted",
5659
];
5760

5861
if (window._clientConfig.useReaper === true) {
5962
isSearch.push("reapable");
6063
}
6164

65+
type TypeaheadField = {
66+
fieldName: string;
67+
resolver?: ((value: string) => string[]) | string[];
68+
type?: "DATE" | "TEXT";
69+
};
70+
6271
export type QuerySuggestionsService = {
63-
getChipSuggestions: (chip: Chip) => string[],
64-
typeaheadFields: (
65-
| {
66-
fieldName: string;
67-
resolver: (value: string) => string[];
68-
}
69-
| {
70-
fieldName: string;
71-
resolver?: undefined;
72-
}
73-
| {
74-
fieldName: string;
75-
resolver: string[];
76-
}
77-
)[];
72+
getChipSuggestions: (chip: Chip) => string[];
73+
typeaheadFields: TypeaheadField[];
7874
};
7975

8076
querySuggestions.factory("querySuggestions", [
8177
"mediaApi",
8278
"editsApi",
83-
function (mediaApi: any, editsApi: any): QuerySuggestionsService {
79+
function (mediaApi: MediaApi, editsApi: EditsApi): QuerySuggestionsService {
8480
const fieldAliases = window._clientConfig.fieldAliases
8581
.filter((fieldAlias: FieldAlias) => fieldAlias.displaySearchHint === true)
8682
.reduce(
@@ -95,50 +91,50 @@ querySuggestions.factory("querySuggestions", [
9591
{
9692
fieldName: "by",
9793
resolver: (value: string) =>
98-
listPhotographers().then(prefixFilter(value))
94+
listPhotographers().then(prefixFilter(value)),
9995
},
10096
{
10197
fieldName: "category",
102-
resolver: (value: string) => listCategories().then(prefixFilter(value))
98+
resolver: (value: string) => listCategories().then(prefixFilter(value)),
10399
},
104100
{ fieldName: "city" },
105101
{ fieldName: "copyright" },
106102
{ fieldName: "country" },
107103
{
108104
fieldName: "credit",
109-
resolver: (value: string) => suggestCredit(value)
105+
resolver: (value: string) => suggestCredit(value),
110106
},
111107
{ fieldName: "description" },
112108
{
113109
fieldName: "fileType",
114-
resolver: (value: string) => prefixFilter(value)(fileTypes)
110+
resolver: (value: string) => prefixFilter(value)(fileTypes),
115111
},
116112
{
117113
fieldName: "illustrator",
118114
resolver: (value: string) =>
119-
listIllustrators().then(prefixFilter(value))
115+
listIllustrators().then(prefixFilter(value)),
120116
},
121117
{ fieldName: "in" },
122118
{ fieldName: "keyword" },
123119
{
124120
fieldName: "label",
125-
resolver: (value: string) => suggestLabels(value)
121+
resolver: (value: string) => suggestLabels(value),
126122
},
127123
{ fieldName: "location" },
128124
{ fieldName: "person" },
129125
{
130126
fieldName: "source",
131-
resolver: (value: string) => suggestSource(value)
127+
resolver: (value: string) => suggestSource(value),
132128
},
133129
{ fieldName: "specialInstructions" },
134130
{ fieldName: "state" },
135131
{
136132
fieldName: "subject",
137-
resolver: (value: string) => prefixFilter(value)(subjects)
133+
resolver: (value: string) => prefixFilter(value)(subjects),
138134
},
139135
{
140136
fieldName: "supplier",
141-
resolver: (value: string) => listSuppliers().then(prefixFilter(value))
137+
resolver: (value: string) => listSuppliers().then(prefixFilter(value)),
142138
},
143139
{ fieldName: "suppliersReference" },
144140
{ fieldName: "title" },
@@ -148,26 +144,35 @@ querySuggestions.factory("querySuggestions", [
148144
{ fieldName: "usages@platform", resolver: listUsagePlatforms },
149145
{
150146
fieldName: "usages@status",
151-
resolver: ["published", "pending", "removed"]
147+
resolver: ["published", "pending", "removed"],
152148
},
153149
{ fieldName: "usages@reference" },
154150
{ fieldName: "has" },
155151
{ fieldName: "croppedBy" },
156152
{ fieldName: "filename" },
157153
{
158154
fieldName: "photoshoot",
159-
resolver: (value: string) => suggestPhotoshoot(value)
155+
resolver: (value: string) => suggestPhotoshoot(value),
160156
},
161157
{ fieldName: "leasedBy" },
162158
{ fieldName: "is", resolver: isSearch },
159+
{ fieldName: "dateTaken" } as const,
160+
{ fieldName: "date" },
163161
...Object.keys(fieldAliases).map((fieldName) => ({
164162
fieldName,
165163
resolver: fieldAliases.hasOwnProperty(fieldName)
166164
? (value: string) =>
167165
prefixFilter(value)(suggestFieldAliasOptions(fieldName))
168-
: undefined
169-
}))
170-
].sort();
166+
: undefined,
167+
})),
168+
].sort((a, b) => {
169+
if (a.fieldName > b.fieldName) {
170+
return 1;
171+
} else if (a.fieldName === b.fieldName) {
172+
return 0;
173+
}
174+
return -1;
175+
}) as TypeaheadField[];
171176

172177
const fieldNames = typeaheadFields.map((field) => field.fieldName);
173178

@@ -178,88 +183,71 @@ querySuggestions.factory("querySuggestions", [
178183
}
179184

180185
function listSuppliers() {
181-
return editsApi
182-
.getUsageRightsCategories()
183-
.then(
184-
(results: {
185-
value: string;
186-
properties: { name: string; options: any }[];
187-
}) => {
188-
return List<typeof results>(results)
189-
.filter((res: any) => res.value === "agency")
190-
.flatMap((res: any) => res.properties)
191-
.filter(
192-
(prop: { name: string; options: string }) =>
193-
prop.name === "supplier",
194-
)
195-
.flatMap((prop: { options: any }) => prop.options)
196-
.toJS();
197-
},
198-
);
186+
return editsApi.getUsageRightsCategories().then((results) => {
187+
return results
188+
.filter((res) => res.value === "agency")
189+
.flatMap((res) => res.properties)
190+
.filter((prop) => prop.name === "supplier")
191+
.flatMap((prop) => prop.options);
192+
});
199193
}
200194

201195
function listCategories() {
202196
// TODO: would be nice to use user friendly labels and map
203197
// them to the key internally
204-
return editsApi.getUsageRightsCategories().then((results: any) => {
205-
return results
206-
.map((res: any) => res.value)
207-
.filter((key: string) => key !== ""); // no empty category
198+
return editsApi.getUsageRightsCategories().then((results) => {
199+
return results.map((res) => res.value).filter((key) => key !== ""); // no empty category
208200
});
209201
}
210202

211-
const photographerCategories = List.of(
203+
const photographerCategories = [
212204
"staff-photographer",
213205
"contract-photographer",
214-
);
206+
];
215207
function listPhotographers() {
216-
return editsApi.getUsageRightsCategories().then((results: any) => {
217-
return List(results)
218-
.filter((res: any) => photographerCategories.includes(res.value))
219-
.flatMap((res: any) => res.properties)
220-
.filter((prop: { name: string }) => prop.name === "photographer")
221-
.flatMap((prop: { optionsMap: any }) =>
222-
Map(prop.optionsMap).valueSeq(),
223-
)
224-
.flatMap((list) => list)
225-
.sort()
226-
.toJS();
208+
return editsApi.getUsageRightsCategories().then((results) => {
209+
return results
210+
.filter((res) => photographerCategories.includes(res.value))
211+
.flatMap((res) => res.properties)
212+
.filter((prop) => prop.name === "photographer")
213+
.flatMap((prop) => Object.values(prop.optionsMap))
214+
.flat()
215+
.sort();
227216
});
228217
}
229218

230219
function listIllustrators() {
231-
return editsApi.getUsageRightsCategories().then((results: any) => {
232-
return List(results)
233-
.filter((res: any) => res.value === "contract-illustrator")
234-
.flatMap((res: any) => res.properties)
235-
.filter((prop: { name: string }) => prop.name === "creator")
236-
.flatMap((prop: { options: any }) => prop.options)
237-
.toJS();
220+
return editsApi.getUsageRightsCategories().then((results) => {
221+
return results
222+
.filter((res) => res.value === "contract-illustrator")
223+
.flatMap((res) => res.properties)
224+
.filter((prop) => prop.name === "creator")
225+
.flatMap((prop) => prop.options);
238226
});
239227
}
240228

241229
function suggestCredit(prefix: string) {
242230
return mediaApi
243231
.metadataSearch("credit", { q: prefix })
244-
.then((results: any) => results.data.map((res: any) => res.key));
232+
.then((results) => results.data.map((res) => res.key));
245233
}
246234

247235
function suggestSource(prefix: string) {
248236
return mediaApi
249237
.metadataSearch("source", { q: prefix })
250-
.then((results: any) => results.data.map((res: any) => res.key));
238+
.then((results) => results.data.map((res) => res.key));
251239
}
252240

253241
function suggestLabels(prefix: string) {
254242
return mediaApi
255243
.labelsSuggest({ q: prefix })
256-
.then((labels: any) => labels.data);
244+
.then((labels) => labels.data);
257245
}
258246

259247
function suggestPhotoshoot(prefix: string) {
260248
return mediaApi
261249
.metadataSearch("photoshoot", { q: prefix })
262-
.then((results: any) => results.data.map((res: any) => res.key));
250+
.then((results) => results.data.map((res) => res.key));
263251
}
264252

265253
function listUsagePlatforms() {
@@ -301,7 +289,7 @@ querySuggestions.factory("querySuggestions", [
301289

302290
return {
303291
typeaheadFields,
304-
getChipSuggestions
292+
getChipSuggestions,
305293
};
306-
}
294+
},
307295
]);

kahuna/public/js/services/api/edits-api.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,50 @@ export var editsApi = angular.module('kahuna.services.api.edits', [
55
mediaApi.name
66
]);
77

8+
/**
9+
* @typedef {{
10+
* value: string;
11+
* name: string;
12+
* cost: string;
13+
* description: string;
14+
* defaultRestrictions?: string;
15+
* caution?: string;
16+
* properties: UsageRightsProperty[];
17+
* leases: UsageRightsLease[];
18+
* usageRestrictions?: string;
19+
* usageSpecialInstructions?: string;
20+
* }} CategoryResponse
21+
*/
22+
23+
/**
24+
* @typedef {{
25+
* name: string;
26+
* label: string;
27+
* type: string;
28+
* required: Boolean;
29+
* options?: string[];
30+
* optionsMap?: Record<string, string[]>;
31+
* optionsMapKey?: string;
32+
* examples?: string;
33+
* }} UsageRightsProperty
34+
*/
35+
36+
/**
37+
* @typedef {{
38+
* category: string;
39+
* type: string;
40+
* startDate: string;
41+
* duration?: number;
42+
* notes?: string;
43+
* }} UsageRightsLease
44+
*/
45+
46+
/**
47+
* @typedef {{
48+
* getUsageRightsCategories: () => Promise<CategoryResponse[]>
49+
* }} EditsApi
50+
*/
51+
852
editsApi.factory('editsApi', ['$q', 'mediaApi', function($q, mediaApi) {
953

1054
var root;

0 commit comments

Comments
 (0)