Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HelpComponent #418

Merged
merged 26 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@

- Fix infinite loop bug on userinfo, when expiration time is set to small values (INDIGO Sprint 230414, [!410](https://github.com/TeskaLabs/asab-webui/pull/410))

- Add HelpCache for HelpComponent (INDIGO Sprint 230414, [!418](https://github.com/TeskaLabs/asab-webui/pull/418))

## v23.5

### Features
Expand Down
23 changes: 17 additions & 6 deletions src/services/HelpService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ export default class HelpService extends Service {
constructor(app, serviceName="HeaderService"){
super(app, serviceName);
this.App = app;
this.HelpCache = {};
}

async setData(path) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aringoaway It does not have to be async since there is no async action

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aringoaway btw. setData is not a good name for it, since all you do is setting the path

let pathWithExtension;

if ((/\.[^/.]+$/.test(path))) {
pathWithExtension = path;
} else {
pathWithExtension = `${path}.json`
if (this.HelpCache[path] != undefined) {
if (this.HelpCache[path] != "") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aringoaway I was thinking, do we really need this condition? Seems redundant to me, because we are setting an empty string as a value to the cache, if the content has not been retrieved

this.HelpCache[path] = "";

So I suggest a refactoring

if (this.HelpCache[path] != undefined) {
	this.App.Store.dispatch({
		type: HELP_CONTENT,
		content: this.HelpCache[path]
	});
	return;
}

this.App.Store.dispatch({
type: HELP_CONTENT,
content: this.HelpCache[path]
});
return;
}
this.App.Store.dispatch({
type: HELP_CONTENT,
content: ""
});
return;
}

try {
const ASABLibraryAPI = this.App.axiosCreate('asab_library');
let response = await ASABLibraryAPI.get(`/library/item/Help/${pathWithExtension}`);
let response = await ASABLibraryAPI.get(`/library/item/Help/${path}`);
if ((response.status == 200) && response.data) {
this.App.Store.dispatch({
type: HELP_CONTENT,
content: response.data.content
});
this.HelpCache[path] = response.data.content;
}
} catch (e) {
console.warn(`Help service can't retrieve data for ${path}`, e);
Expand All @@ -32,6 +42,7 @@ export default class HelpService extends Service {
type: HELP_CONTENT,
content: ""
});
this.HelpCache[path] = "";
}
}
}
Expand Down