Skip to content

Commit 9cba9ff

Browse files
committed
feat(events): use self_html link for hash with type-specific overridable context; focused -> focussed; error i18n
1 parent 788ae96 commit 9cba9ff

File tree

11 files changed

+80
-37
lines changed

11 files changed

+80
-37
lines changed

invenio_requests/assets/semantic-ui/js/invenio_requests/api/InvenioRequestApi.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
import _isEmpty from "lodash/isEmpty";
88
import { http } from "react-invenio-forms";
9+
import { i18next } from "@translations/invenio_requests/i18next";
910

1011
export class RequestLinksExtractor {
1112
#urls;
1213

1314
constructor(request) {
1415
if (!request?.links) {
15-
throw TypeError("Request resource links are undefined");
16+
throw TypeError(
17+
i18next.t("{{link_name}} links are undefined. Please refresh the page.", {
18+
link_name: "Request resource",
19+
})
20+
);
1621
}
1722
this.#urls = request.links;
1823
}
@@ -23,28 +28,44 @@ export class RequestLinksExtractor {
2328

2429
get timeline() {
2530
if (!this.#urls.timeline) {
26-
throw TypeError("Timeline link missing from resource.");
31+
throw TypeError(
32+
i18next.t("{{link_name}} link missing from resource.", {
33+
link_name: "Timeline",
34+
})
35+
);
2736
}
2837
return this.#urls.timeline;
2938
}
3039

31-
get timelineFocussed() {
40+
get timelineFocused() {
3241
if (!this.#urls.timeline) {
33-
throw TypeError("Timeline focussed link missing from resource.");
42+
throw TypeError(
43+
i18next.t("{{link_name}} link missing from resource.", {
44+
link_name: "Focused timeline",
45+
})
46+
);
3447
}
35-
return this.#urls.timeline_focussed;
48+
return this.#urls.timeline_focused;
3649
}
3750

3851
get comments() {
3952
if (!this.#urls.comments) {
40-
throw TypeError("Comments link missing from resource.");
53+
throw TypeError(
54+
i18next.t("{{link_name}} link missing from resource.", {
55+
link_name: "Comments",
56+
})
57+
);
4158
}
4259
return this.#urls.comments;
4360
}
4461

4562
get actions() {
4663
if (!this.#urls.actions) {
47-
throw TypeError("Actions link missing from resource.");
64+
throw TypeError(
65+
i18next.t("{{link_name}} link missing from resource.", {
66+
link_name: "Actions",
67+
})
68+
);
4869
}
4970
return this.#urls.actions;
5071
}
@@ -70,8 +91,8 @@ export class InvenioRequestsAPI {
7091
});
7192
};
7293

73-
getTimelineFocussed = async (focusEventId, params) => {
74-
return await http.get(this.#urls.timelineFocussed, {
94+
getTimelineFocused = async (focusEventId, params) => {
95+
return await http.get(this.#urls.timelineFocused, {
7596
params: {
7697
expand: 1,
7798
focus_event_id: focusEventId,

invenio_requests/assets/semantic-ui/js/invenio_requests/api/InvenioRequestEventsApi.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Invenio RDM Records is free software; you can redistribute it and/or modify it
55
// under the terms of the MIT License; see LICENSE file for more details.
66
import { http } from "react-invenio-forms";
7+
import { i18next } from "@translations/invenio_requests/i18next";
78

89
export class RequestEventsLinksExtractor {
910
#links;
@@ -14,7 +15,11 @@ export class RequestEventsLinksExtractor {
1415

1516
get eventUrl() {
1617
if (!this.#links.self) {
17-
throw TypeError("Self link missing from resource.");
18+
throw TypeError(
19+
i18next.t("{{link_name}} link missing from resource.", {
20+
link_name: "Self",
21+
})
22+
);
1823
}
1924
return this.#links.self;
2025
}

invenio_requests/assets/semantic-ui/js/invenio_requests/timeline/state/actions.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const fetchTimeline = (focusEventId = undefined) => {
3838
try {
3939
let response;
4040
if (focusEventId) {
41-
response = await config.requestsApi.getTimelineFocussed(focusEventId, {
41+
response = await config.requestsApi.getTimelineFocused(focusEventId, {
4242
size: size,
4343
});
4444
} else {
@@ -102,6 +102,9 @@ export const setPage = (page) => {
102102
type: CHANGE_PAGE,
103103
payload: page,
104104
});
105+
dispatch({
106+
type: IS_LOADING,
107+
});
105108

106109
await dispatch(fetchTimeline());
107110
};

invenio_requests/assets/semantic-ui/js/invenio_requests/timelineEvents/TimelineCommentEvent.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { RichEditor } from "react-invenio-forms";
1616
import RequestsFeed from "../components/RequestsFeed";
1717
import { TimelineEventBody } from "../components/TimelineEventBody";
1818
import { toRelativeTime } from "react-invenio-forms";
19-
import { copyUrlForEvent, getEventIdFromUrl } from "./utils";
19+
import { getEventIdFromUrl } from "./utils";
2020

2121
class TimelineCommentEvent extends Component {
2222
constructor(props) {
@@ -61,9 +61,11 @@ class TimelineCommentEvent extends Component {
6161

6262
copyLink() {
6363
const {
64-
event: { id: eventId },
64+
event: {
65+
links: { self_html },
66+
},
6567
} = this.props;
66-
copyUrlForEvent(eventId);
68+
navigator.clipboard.writeText(self_html);
6769
}
6870

6971
render() {

invenio_requests/assets/semantic-ui/js/invenio_requests/timelineEvents/utils.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
// Invenio Requests is free software; you can redistribute it and/or modify it
55
// under the terms of the MIT License; see LICENSE file for more details.
66

7-
export const copyUrlForEvent = (eventId) => {
8-
const currentUrl = new URL(window.location.href);
9-
currentUrl.hash = `commentevent-${eventId}`;
10-
navigator.clipboard.writeText(currentUrl.toString());
11-
};
12-
137
export const getEventIdFromUrl = () => {
148
const currentUrl = new URL(window.location.href);
159
const hash = currentUrl.hash;

invenio_requests/resources/events/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RequestCommentsResourceConfig(RecordResourceConfig):
3232
"list": "/<request_id>/comments",
3333
"item": "/<request_id>/comments/<comment_id>",
3434
"timeline": "/<request_id>/timeline",
35-
"timeline_focussed": "/<request_id>/timeline_focussed",
35+
"timeline_focused": "/<request_id>/timeline_focused",
3636
}
3737

3838
# Input

invenio_requests/resources/events/resource.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def create_url_rules(self):
6161
route("PUT", routes["item"], self.update),
6262
route("DELETE", routes["item"], self.delete),
6363
route("GET", routes["timeline"], self.search),
64-
route("GET", routes["timeline_focussed"], self.focussed_list),
64+
route("GET", routes["timeline_focused"], self.focused_list),
6565
]
6666

6767
@list_view_args_parser
@@ -145,9 +145,9 @@ def search(self):
145145
@request_extra_args
146146
@search_args_parser
147147
@response_handler(many=True)
148-
def focussed_list(self):
148+
def focused_list(self):
149149
"""List the page containing the event with ID focus_event_id, or the first page of results if this is not found."""
150-
hits = self.service.focussed_list(
150+
hits = self.service.focused_list(
151151
identity=g.identity,
152152
request_id=resource_requestctx.view_args["request_id"],
153153
focus_event_id=resource_requestctx.args.get("focus_event_id"),

invenio_requests/services/events/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from invenio_records_resources.services.records.links import pagination_links
1919
from invenio_records_resources.services.records.results import RecordItem, RecordList
2020

21+
from invenio_requests.proxies import (
22+
current_request_type_registry,
23+
current_requests_service,
24+
)
25+
2126
from ...records.api import Request, RequestEvent
2227
from ..permissions import PermissionPolicy
2328
from ..schemas import RequestEventSchema
@@ -67,9 +72,11 @@ class RequestEventLink(Link):
6772
"""Link variables setter for RequestEvent links."""
6873

6974
@staticmethod
70-
def vars(record, vars):
75+
def vars(obj, vars):
7176
"""Variables for the URI template."""
72-
vars.update({"id": record.id, "request_id": record.request_id})
77+
request_type = current_request_type_registry.lookup(vars["request_type"])
78+
vars.update({"id": obj.id, "request_id": obj.request_id})
79+
vars.update(request_type._update_link_config(**vars))
7380

7481

7582
class RequestEventsServiceConfig(RecordServiceConfig, ConfiguratorMixin):
@@ -90,6 +97,7 @@ class RequestEventsServiceConfig(RecordServiceConfig, ConfiguratorMixin):
9097
# ResultItem configurations
9198
links_item = {
9299
"self": RequestEventLink("{+api}/requests/{request_id}/comments/{id}"),
100+
"self_html": RequestEventLink("{+ui}/requests/{request_id}#commentevent-{id}"),
93101
}
94102
links_search = pagination_links("{+api}/requests/{request_id}/timeline{?args*}")
95103

invenio_requests/services/events/service.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ def expandable_fields(self):
4545
"""Get expandable fields."""
4646
return [EntityResolverExpandableField("created_by")]
4747

48+
def links_item_tpl_for_request(self, request):
49+
"""Include the request's type ID in the link template.
50+
51+
This way, the link URL can be contextualised depending on the type of the event's
52+
parent request.
53+
"""
54+
return LinksTemplate(
55+
self.config.links_item, context={"request_type": str(request.type)}
56+
)
57+
4858
@unit_of_work()
4959
def create(
5060
self,
@@ -119,7 +129,7 @@ def create(
119129
identity,
120130
event,
121131
schema=schema,
122-
links_tpl=self.links_item_tpl,
132+
links_tpl=self.links_item_tpl_for_request(request),
123133
expandable_fields=self.expandable_fields,
124134
expand=expand,
125135
)
@@ -136,7 +146,7 @@ def read(self, identity, id_, expand=False):
136146
identity,
137147
event,
138148
schema=self._wrap_schema(event.type.marshmallow_schema()),
139-
links_tpl=self.links_item_tpl,
149+
links_tpl=self.links_item_tpl_for_request(request),
140150
expandable_fields=self.expandable_fields,
141151
expand=expand,
142152
)
@@ -183,7 +193,7 @@ def update(self, identity, id_, data, revision_id=None, uow=None, expand=False):
183193
identity,
184194
event,
185195
schema=schema,
186-
links_tpl=self.links_item_tpl,
196+
links_tpl=self.links_item_tpl_for_request(request),
187197
expandable_fields=self.expandable_fields,
188198
expand=expand,
189199
)
@@ -269,12 +279,12 @@ def search(
269279
self.config.links_search,
270280
context={"request_id": str(request.id), "args": params},
271281
),
272-
links_item_tpl=self.links_item_tpl,
282+
links_item_tpl=self.links_item_tpl_for_request(request),
273283
expandable_fields=self.expandable_fields,
274284
expand=expand,
275285
)
276286

277-
def focussed_list(
287+
def focused_list(
278288
self,
279289
identity,
280290
request_id,
@@ -283,7 +293,7 @@ def focussed_list(
283293
expand=False,
284294
search_preference=None,
285295
):
286-
"""Return a page of results focussed on a given event, or the first page if the event is not found."""
296+
"""Return a page of results focused on a given event, or the first page if the event is not found."""
287297
# Permissions - guarded by the request's can_read.
288298
request = self._get_request(request_id)
289299
self.require_permission(identity, "read", request=request)
@@ -332,7 +342,7 @@ def focussed_list(
332342
self.config.links_search,
333343
context={"request_id": str(request.id), "args": params},
334344
),
335-
links_item_tpl=self.links_item_tpl,
345+
links_item_tpl=self.links_item_tpl_for_request(request),
336346
expandable_fields=self.expandable_fields,
337347
expand=expand,
338348
)

invenio_requests/services/requests/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class RequestsServiceConfig(RecordServiceConfig, ConfiguratorMixin):
107107
"self_html": RequestLink("{+ui}/requests/{id}"),
108108
"comments": RequestLink("{+api}/requests/{id}/comments"),
109109
"timeline": RequestLink("{+api}/requests/{id}/timeline"),
110-
"timeline_focussed": RequestLink("{+api}/requests/{id}/timeline_focussed"),
110+
"timeline_focused": RequestLink("{+api}/requests/{id}/timeline_focused"),
111111
}
112112
links_search = pagination_links("{+api}/requests{?args*}")
113113
links_user_requests_search = pagination_links("{+api}/user/requests{?args*}")

0 commit comments

Comments
 (0)