Skip to content

Commit

Permalink
refactor(API): transform publication/getAllTags API to a state loaded…
Browse files Browse the repository at this point in the history
… in library from an action dispatched in main process

updated with catalog/get saga logic
(Fixes #1994)
  • Loading branch information
panaC committed Sep 7, 2023
1 parent 50cc655 commit 1d72280
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 80 deletions.
3 changes: 0 additions & 3 deletions src/common/api/interface/publicationApi.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export interface IPublicationApi {
identifier: string,
tags: string[],
) => SagaGenerator<PublicationView>;
getAllTags: (
) => SagaGenerator<string[]>;
importFromLink: (
link: IOpdsLinkView,
pub?: IOpdsPublicationView,
Expand Down Expand Up @@ -58,7 +56,6 @@ export interface IPublicationModuleApi {
"publication/findAll": IPublicationApi["findAll"];
"publication/findByTag": IPublicationApi["findByTag"];
"publication/updateTags": IPublicationApi["updateTags"];
"publication/getAllTags": IPublicationApi["getAllTags"];
"publication/importFromLink": IPublicationApi["importFromLink"];
"publication/importFromFs": IPublicationApi["importFromFs"];
"publication/importFromString": IPublicationApi["importFromString"];
Expand Down
4 changes: 3 additions & 1 deletion src/common/redux/actions/catalog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
// ==LICENSE-END==

import * as getCatalog from "./getCatalog";
import * as setCatalog from "./setCatalog";
import * as setCatalog from "./setCatalogView";
import * as setTagView from "./setTagView";

export {
getCatalog,
setCatalog,
setTagView,
};
27 changes: 27 additions & 0 deletions src/common/redux/actions/catalog/setTagView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { Action } from "readium-desktop/common/models/redux";

export const ID = "CATALOG_SET_TAG_VIEW";

export interface Payload {
tags: string[];
}

export function build(tags: string[]):
Action<typeof ID, Payload> {

return {
type: ID,
payload: {
tags,
},
};
}
build.toString = () => ID; // Redux StringableActionCreator
export type TAction = ReturnType<typeof build>;
1 change: 1 addition & 0 deletions src/main/redux/middleware/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const SYNCHRONIZABLE_ACTIONS: string[] = [
lcpActions.unlockPublicationWithPassphrase.ID,

catalogActions.setCatalog.ID, // send new catalogView to library
catalogActions.setTagView.ID,
];

export const reduxSyncMiddleware: Middleware
Expand Down
17 changes: 0 additions & 17 deletions src/main/redux/sagas/api/publication/getAllTags.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/main/redux/sagas/api/publication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { deletePublication } from "./delete";
import { exportPublication } from "./export";
import { findAll } from "./findAll";
import { findByTag } from "./findByTag";
import { getAllTags } from "./getAllTags";
import { getPublication } from "./getPublication";
import { importFromFs, importFromLink, importFromString } from "./import";
import { search, searchEqTitle } from "./search";
Expand All @@ -22,12 +21,11 @@ export const publicationApi: IPublicationApi = {
get: getPublication,
delete: deletePublication,
findByTag,
updateTags,
getAllTags,
search,
exportPublication,
importFromFs,
importFromLink,
importFromString,
searchEqTitle,
updateTags,
};
5 changes: 4 additions & 1 deletion src/main/redux/sagas/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ function* getCatalog() {
];

yield* putTyped(catalogActions.setCatalog.build({entries}));

const publicationRepository = diMainGet("publication-repository");
const allTags = yield* callTyped(() => publicationRepository.getAllTags());
yield* putTyped(catalogActions.setTagView.build(allTags));
}

function* updateResumePosition() {
Expand Down Expand Up @@ -261,7 +265,6 @@ export function saga() {
return all([
takeSpawnLatest(
[catalogActions.getCatalog.ID, publicationActions.addPublication.ID, publicationActions.deletePublication.ID],
// catalogActions.getCatalog.ID,
getCatalog,
(e) => error(filename_ + ":getCatalog", e),
),
Expand Down
24 changes: 4 additions & 20 deletions src/renderer/library/components/catalog/Catalog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import {
TranslatorProps, withTranslator,
} from "readium-desktop/renderer/common/components/hoc/translator";
import {
apiClean, apiDispatch, apiRefreshToState, apiState,
apiClean, apiDispatch,
} from "readium-desktop/renderer/common/redux/api/api";
import LibraryLayout from "readium-desktop/renderer/library/components/layout/LibraryLayout";
import { ILibraryRootState } from "readium-desktop/renderer/library/redux/states";
import { DisplayType, IRouterLocationState } from "readium-desktop/renderer/library/routing";
import { Dispatch } from "redux";
import { PUBLICATION_TAGS_API_ID_CHANNEL } from "../../redux/sagas/catalog";

import CatalogGridView from "./GridView";
import Header from "./Header";
Expand All @@ -40,11 +39,6 @@ class Catalog extends React.Component<IProps, undefined> {
public render(): React.ReactElement<{}> {
const { __, catalog, tags } = this.props;

if (this.props.refresh) {
// this.props.api(CATALOG_GET_API_ID_CHANNEL)("catalog/get")();
this.props.api(PUBLICATION_TAGS_API_ID_CHANNEL)("publication/getAllTags")();
}

const displayType = (this.props.location?.state && (this.props.location.state as IRouterLocationState).displayType) || DisplayType.Grid;

const secondaryHeader = <Header/>;
Expand All @@ -59,11 +53,11 @@ class Catalog extends React.Component<IProps, undefined> {
displayType === DisplayType.Grid
? <CatalogGridView
catalogEntries={catalog.entries}
tags={(tags?.data.result) || []}
tags={tags}
/>
: <CatalogListView
catalogEntries={catalog.entries}
tags={(tags?.data.result) || []}
tags={tags}
/>
)
}
Expand All @@ -73,19 +67,9 @@ class Catalog extends React.Component<IProps, undefined> {
}

const mapStateToProps = (state: ILibraryRootState) => ({
// catalog: apiState(state)(CATALOG_GET_API_ID_CHANNEL)("catalog/get"),
tags: apiState(state)(PUBLICATION_TAGS_API_ID_CHANNEL)("publication/getAllTags"),
refresh: apiRefreshToState(state)([
"publication/importFromFs",
"publication/importFromLink",
"publication/delete",
"publication/findAll",
// "catalog/addEntry",
"publication/updateTags",
// "reader/setLastReadingLocation",
]),
location: state.router.location,
catalog: state.publication.catalog,
tags: state.publication.tag,
});

const mapDispatchToProps = (dispatch: Dispatch) => ({
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/library/redux/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ILibraryRootState } from "../states";
import { RouterState } from "redux-first-history";
import { sessionReducer } from "readium-desktop/common/redux/reducers/session";
import { catalogViewReducer } from "./catalog";
import { tagReducer } from "./tag";

export const rootReducer = (routerReducer: Reducer<RouterState>) => {
return combineReducers<ILibraryRootState>({
Expand Down Expand Up @@ -69,6 +70,7 @@ export const rootReducer = (routerReducer: Reducer<RouterState>) => {
load: loadReducer,
publication: combineReducers({
catalog: catalogViewReducer,
tag: tagReducer,
}),
});
};
25 changes: 25 additions & 0 deletions src/renderer/library/redux/reducers/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==

import { catalogActions } from "readium-desktop/common/redux/actions";

const initialState: string[] = [];

export function tagReducer(
state: string[] = initialState,
action: catalogActions.setTagView.TAction,
): string[] {
switch (action.type) {
case catalogActions.setTagView.ID:
const {tags} = action.payload;
return tags; // already immutable RPC serialization

default:
return state;

}
}
30 changes: 0 additions & 30 deletions src/renderer/library/redux/sagas/catalog.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/renderer/library/redux/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { put as putTyped } from "typed-redux-saga/macro";

import * as publicationInfoOpds from "../../../common/redux/sagas/dialog/publicationInfoOpds";
import * as publicationInfoReaderAndLib from "../../../common/redux/sagas/dialog/publicationInfoReaderAndLib";
import * as catalog from "./catalog";
import * as history from "./history";
import * as i18n from "./i18n";
import * as lcp from "./lcp";
Expand Down Expand Up @@ -57,10 +56,6 @@ export function* rootSaga() {
publicationInfoSyncTags.saga(),

load.saga(),

// TODO need to update this
catalog.saga(),

]);

yield* putTyped(catalogActions.getCatalog.build()); // ask to fill catalog view
Expand Down
1 change: 1 addition & 0 deletions src/renderer/library/redux/states/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export interface ILibraryRootState extends IRendererCommonRootState {
load: ILoadState;
publication: {
catalog: CatalogView,
tag: string[],
}
}

0 comments on commit 1d72280

Please sign in to comment.