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

feat: add favorites management #840

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions packages/apps/dms/src/api/document-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,19 @@ export async function countAttachedFiles({model, modelId}) {
provider: 'model',
});
}

export async function addToFavorites({documentId}) {
return getActionApi().send({
url: `ws/aos/dms/favorites/${documentId}`,
method: 'post',
description: 'add to favorites',
});
}

export async function removeFromFavorites({documentId}) {
return getActionApi().send({
url: `ws/aos/dms/favorites/${documentId}`,
method: 'delete',
description: 'remove from favorites',
});
}
2 changes: 2 additions & 0 deletions packages/apps/dms/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/

export {
addToFavorites as addToFavoritesApi,
countAttachedFiles as countAttachedDocumentsApi,
createDocument as createDocumentApi,
fetchDirectory as fetchDirectoryApi,
removeFromFavorites as removeFromFavoritesApi,
searchDirectory as searchDirectoryApi,
searchDocument as searchDocumentApi,
updateDocument as updateDocumentApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React from 'react';
import React, {useMemo} from 'react';
import {
useDispatch,
useNavigation,
useSelector,
useTranslator,
} from '@axelor/aos-mobile-core';
import {ActionCard, useThemeColor} from '@axelor/aos-mobile-ui';
import {
addToFavorites,
removeFromFavorites,
} from '../../../features/documentSlice';
import {DocumentCard} from '../../atoms';

interface DocumentActionCardProps {
Expand All @@ -33,8 +38,20 @@ const DocumentActionCard = ({document}: DocumentActionCardProps) => {
const I18n = useTranslator();
const Colors = useThemeColor();
const navigation = useNavigation();
const dispatch = useDispatch();

const {mobileSettings} = useSelector((state: any) => state.appConfig);
const {user} = useSelector(state => state.user);

const isFavorite = useMemo(
() => user?.favouriteFileSet.some(({id}) => id === document.id),
[document.id, user?.favouriteFileSet],
);

const sliceFunction = useMemo(
() => (isFavorite ? removeFromFavorites : addToFavorites),
[isFavorite],
);

return (
<ActionCard
Expand All @@ -47,10 +64,16 @@ const DocumentActionCard = ({document}: DocumentActionCardProps) => {
hidden: !mobileSettings?.isDownloadAllowed,
},
{
iconName: 'star',
iconName: isFavorite ? 'star-fill' : 'star',
iconColor: Colors.progressColor.background,
helper: I18n.t('Dms_AddToFavorites'),
onPress: () => console.log('Add to favorites'),
onPress: () =>
dispatch(
(sliceFunction as any)({
documentId: document.id,
userId: user?.id,
}),
),
hidden: !mobileSettings?.isFavoritesManagementEnabled,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ import React, {useMemo, useState} from 'react';
import {
SearchTreeView,
useNavigation,
useDispatch,
useSelector,
useTranslator,
} from '@axelor/aos-mobile-core';
import {ChipSelect, Screen, useThemeColor} from '@axelor/aos-mobile-ui';
import {searchDocument, searchDirectory} from '../../../features/documentSlice';
import {
addToFavorites,
removeFromFavorites,
searchDocument,
searchDirectory,
} from '../../../features/documentSlice';
import {searchDocumentApi} from '../../../api';
import {AuthorFilter, DirectoryCard} from '../../atoms';
import {DocumentActionCard} from '../../molecules';
Expand All @@ -38,6 +44,7 @@ const DocumentList = ({defaultParent}: DocumentListProps) => {
const I18n = useTranslator();
const Colors = useThemeColor();
const navigation = useNavigation();
const dispatch = useDispatch();

const [author, setAuthor] = useState(null);
const [selectedExtensions, setSelectedExtensions] = useState([]);
Expand All @@ -50,6 +57,7 @@ const DocumentList = ({defaultParent}: DocumentListProps) => {
directoryList,
} = useSelector((state: any) => state.dms_document);
const {mobileSettings} = useSelector((state: any) => state.appConfig);
const {user} = useSelector(state => state.user);

const sliceParentFunctionData = useMemo(
() => ({
Expand Down Expand Up @@ -93,30 +101,46 @@ const DocumentList = ({defaultParent}: DocumentListProps) => {
searchPlaceholder={I18n.t('Base_Search')}
parentFieldName="parent"
renderBranch={({item}) => <DirectoryCard directory={item} />}
getBranchActions={branch => [
{
iconName: 'star',
iconColor: Colors.progressColor.background,
helper: I18n.t('Dms_AddToFavorites'),
onPress: () => console.log('branch: ', branch),
hidden: !mobileSettings?.isFavoritesManagementEnabled,
},
{
iconName: 'info-circle',
helper: I18n.t('Dms_Details'),
onPress: () => console.log('branch: ', branch),
},
{
iconName: 'pencil-fill',
helper: I18n.t('Dms_Rename'),
large: true,
onPress: () =>
navigation.navigate('DocumentFormScreen', {
document: branch.item,
}),
hidden: !mobileSettings?.isRenamingAllowed,
},
]}
getBranchActions={branch => {
const isFavorite = user?.favouriteFolderSet.some(
({id}) => id === branch.item.id,
);

const sliceFunction = isFavorite
? removeFromFavorites
: addToFavorites;

return [
{
iconName: isFavorite ? 'star-fill' : 'star',
iconColor: Colors.progressColor.background,
helper: I18n.t('Dms_AddToFavorites'),
onPress: () =>
dispatch(
(sliceFunction as any)({
documentId: branch.item.id,
userId: user?.id,
}),
),
hidden: !mobileSettings?.isFavoritesManagementEnabled,
},
{
iconName: 'info-circle',
helper: I18n.t('Dms_Details'),
onPress: () => console.log('branch: ', branch),
},
{
iconName: 'pencil-fill',
helper: I18n.t('Dms_Rename'),
large: true,
onPress: () =>
navigation.navigate('DocumentFormScreen', {
document: branch.item,
}),
hidden: !mobileSettings?.isRenamingAllowed,
},
];
}}
renderLeaf={({item}) => <DocumentActionCard document={item} />}
headerChildren={<AuthorFilter author={author} setAuthor={setAuthor} />}
chipComponent={
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/dms/src/features/asyncFunctions-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/

export {
addToFavorites,
createDocument,
removeFromFavorites,
searchDirectory,
searchDocument,
searchFavoriteDocument,
updateDocument,
} from './documentSlice';
29 changes: 29 additions & 0 deletions packages/apps/dms/src/features/documentSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';
import {
fetchActiveUser,
generateInifiniteScrollCases,
handlerApiCall,
} from '@axelor/aos-mobile-core';
import {
addToFavorites as _addToFavorites,
createDocument as _createDocument,
removeFromFavorites as _removeFromFavorites,
searchDirectory as _searchDirectory,
searchDocument as _searchDocument,
updateDocument as _updateDocument,
Expand Down Expand Up @@ -93,6 +96,32 @@ export const updateDocument = createAsyncThunk(
},
);

export const addToFavorites = createAsyncThunk(
'dms_document/addToFavorites',
async function (data, {getState, dispatch}) {
return handlerApiCall({
fetchFunction: _addToFavorites,
data,
action: 'Dms_SliceAction_AddToFavorites',
getState,
responseOptions: {isArrayResponse: false, showToast: true},
}).then(() => dispatch(fetchActiveUser(data.userId)));
},
);

export const removeFromFavorites = createAsyncThunk(
'dms_document/removeFromFavorites',
async function (data, {getState, dispatch}) {
return handlerApiCall({
fetchFunction: _removeFromFavorites,
data,
action: 'Dms_SliceAction_RemoveFromFavorites',
getState,
responseOptions: {isArrayResponse: false, showToast: true},
}).then(() => dispatch(fetchActiveUser(data.userId)));
},
);

const initialState = {
loadingDocuments: false,
moreLoadingDocument: false,
Expand Down
4 changes: 3 additions & 1 deletion packages/apps/dms/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
"Dms_SliceAction_CreateDocument": "create document",
"Dms_SliceAction_UpdateDocument": "update document",
"Dms_SliceAction_FetchDirectory": "fetch directory",
"Dms_SliceAction_CountAttachedFiles": "count attached files"
"Dms_SliceAction_CountAttachedFiles": "count attached files",
"Dms_SliceAction_AddToFavorites": "add to favorites",
"Dms_SliceAction_RemoveFromFavorites": "remove from favorites"
}
4 changes: 3 additions & 1 deletion packages/apps/dms/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
"Dms_SliceAction_CreateDocument": "création d'un document",
"Dms_SliceAction_UpdateDocument": "mise à jour d'un document",
"Dms_SliceAction_FetchDirectory": "récupération d'un dossier",
"Dms_SliceAction_CountAttachedFiles": "comptage des fichiers joints"
"Dms_SliceAction_CountAttachedFiles": "comptage des fichiers joints",
"Dms_SliceAction_AddToFavorites": "ajout aux favoris",
"Dms_SliceAction_RemoveFromFavorites": "suppression des favoris"
}
Loading