Skip to content

Commit

Permalink
feat: manage attached files header action
Browse files Browse the repository at this point in the history
  • Loading branch information
vhu-axelor committed Dec 10, 2024
1 parent 9599e38 commit 16e7dd4
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 15 deletions.
78 changes: 76 additions & 2 deletions packages/apps/dms/src/api/document-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {
axiosApiProvider,
createStandardSearch,
formatRequestBody,
getActionApi,
Expand Down Expand Up @@ -107,6 +108,45 @@ const createDocumentCriteria = ({
return criteria;
};

const createFetchDirectoryCriteria = ({model, modelId}) => {
return [
{
fieldName: 'isDirectory',
operator: '=',
value: true,
},
{
fieldName: 'relatedId',
operator: '=',
value: modelId,
},
{
fieldName: 'relatedModel',
operator: '=',
value: model,
},
{
fieldName: 'parent.relatedModel',
operator: '=',
value: model,
},
{
operator: 'or',
criteria: [
{
fieldName: 'parent.relatedId',
operator: 'isNull',
},
{
fieldName: 'parent.relatedId',
operator: '=',
value: 0,
},
],
},
];
};

export async function searchDocument({
searchValue = null,
authorId,
Expand Down Expand Up @@ -148,14 +188,18 @@ export async function searchDirectory({searchValue, authorId, page = 0}) {
});
}

export async function createDocument({document}) {
export async function createDocument({document, model, modelId}) {
const {matchers} = formatRequestBody(document, 'data');

return getActionApi().send({
url: '/ws/rest/com.axelor.dms.db.DMSFile',
method: 'put',
body: {
data: document,
data: {
relatedModel: model,
relatedId: modelId,
...document,
},
},
description: 'create document',
matchers: {
Expand Down Expand Up @@ -185,3 +229,33 @@ export async function updateDocument({document}) {
},
});
}

export async function fetchDirectory({model, modelId}) {
return createStandardSearch({
model: 'com.axelor.dms.db.DMSFile',
criteria: createFetchDirectoryCriteria({model, modelId}),
fieldKey: 'dms_document',
sortKey: 'dms_document',
page: 0,
numberElementsByPage: 1,
provider: 'model',
});
}

export async function countAttachedFiles({model, modelId}) {
return axiosApiProvider.post({
url: '/ws/rest/com.axelor.dms.db.DMSFile/search',
data: {
data: {
_domain:
'self.relatedModel = :name AND self.relatedId = :id ' +
'AND COALESCE(self.isDirectory, FALSE) = FALSE',
_domainContext: {
name: model,
id: modelId,
},
},
fields: ['id'],
},
});
}
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,7 +17,9 @@
*/

export {
countAttachedFiles as countAttachedDocumentsApi,
createDocument as createDocumentApi,
fetchDirectory as fetchDirectoryApi,
searchDirectory as searchDirectoryApi,
searchDocument as searchDocumentApi,
updateDocument as updateDocumentApi,
Expand Down
24 changes: 24 additions & 0 deletions packages/apps/dms/src/hooks/use-dms-header-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import {useEffect} from 'react';
import {
headerActionsProvider,
useNavigation,
useSelector,
useTranslator,
} from '@axelor/aos-mobile-core';
import {useThemeColor} from '@axelor/aos-mobile-ui';
import {getAction} from '../utils';

export const useDMSHeaders = () => {
useAllDocumentsActions();
useAttachedFilesGenericAction();
};

const useAllDocumentsActions = () => {
Expand All @@ -49,3 +52,24 @@ const useAllDocumentsActions = () => {
});
}, [Colors, I18n, navigation]);
};

const useAttachedFilesGenericAction = () => {
const I18n = useTranslator();
const navigation = useNavigation();

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

useEffect(() => {
headerActionsProvider.registerGenericAction(
'attached_files_generic_action',
async ({model, modelId}) =>
await getAction({
model,
modelId,
isFolderCreationAllowed: mobileSettings?.isFolderCreationAllowed,
navigation,
translator: I18n.t,
}),
);
}, [I18n.t, mobileSettings?.isFolderCreationAllowed, navigation]);
};
7 changes: 6 additions & 1 deletion packages/apps/dms/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
"Dms_Folder": "Folder",
"Dms_File": "File",
"Dms_Root": "Root",
"Dms_AttachedFiles": "Attached files",
"Dms_NoAttachedFiles": "No attached files",
"Dms_DoYouWantToAddFile": "Do you want to add a file?",
"Dms_SliceAction_SearchDocument": "search document",
"Dms_SliceAction_SearchDirectory": "search directory",
"Dms_SliceAction_SearchFavoriteDocument": "search favorite document",
"Dms_SliceAction_CreateDocument": "create document",
"Dms_SliceAction_UpdateDocument": "update document"
"Dms_SliceAction_UpdateDocument": "update document",
"Dms_SliceAction_FetchDirectory": "fetch directory",
"Dms_SliceAction_CountAttachedFiles": "count attached files"
}
7 changes: 6 additions & 1 deletion packages/apps/dms/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
"Dms_Folder": "Dossier",
"Dms_File": "Fichier",
"Dms_Root": "Racine",
"Dms_AttachedFiles": "Fichiers joints",
"Dms_NoAttachedFiles": "Pas de fichiers joints",
"Dms_DoYouWantToAddFile": "Vouslez-vous ajouter un fichier ?",
"Dms_SliceAction_SearchDocument": "recherche sur les documents",
"Dms_SliceAction_SearchDirectory": "recherche sur les dossiers",
"Dms_SliceAction_SearchFavoriteDocument": "recherche sur les documents favoris",
"Dms_SliceAction_CreateDocument": "création d'un document",
"Dms_SliceAction_UpdateDocument": "mise à jour 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"
}
4 changes: 3 additions & 1 deletion packages/apps/dms/src/models/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export const dms_formsRegister: FormConfigs = {
type: 'object',
widget: 'custom',
customComponent: ParentDirectorySearchBar,
requiredIf: ({objectState}) => !objectState.parent?.fileName,
hideIf: ({objectState}) => objectState.isAttachedFileCreation,
requiredIf: ({objectState}) =>
!objectState.parent?.fileName && !objectState.isAttachedFileCreation,
},
fileName: {
titleKey: 'Dms_Name',
Expand Down
85 changes: 85 additions & 0 deletions packages/apps/dms/src/screens/AttachedFilesScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2024 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React, {useEffect, useState} from 'react';
import {
handlerApiCall,
useIsFocused,
useTranslator,
} from '@axelor/aos-mobile-core';
import {Alert, Text} from '@axelor/aos-mobile-ui';
import {fetchDirectory} from '../api/document-api';
import {DocumentList} from '../components';

const AttachedFilesScreen = ({navigation, route}) => {
const _parent = route?.params?.parent;
const model = route?.params?.model;
const modelId = route?.params?.modelId;
const I18n = useTranslator();
const isFocused = useIsFocused();

const [isVisible, setIsVisible] = useState(false);
const [parent, setParent] = useState(_parent);

useEffect(() => {
!parent && setIsVisible(true);
}, [parent]);

useEffect(() => {
if (isFocused && parent == null) {
handlerApiCall({
fetchFunction: fetchDirectory,
data: {model, modelId},
action: 'Dms_SliceAction_FetchDirectory',
getState: () => {},
responseOptions: {isArrayResponse: false},
}).then(directory => setParent(directory));
}
}, [isFocused, model, modelId, parent]);

return (
<>
{!isVisible && <DocumentList defaultParent={parent} />}
<Alert
visible={isVisible}
title={I18n.t('Dms_NoAttachedFiles')}
cancelButtonConfig={{
title: I18n.t('Base_No'),
onPress: () => {
setIsVisible(false);
navigation.goBack();
},
}}
confirmButtonConfig={{
title: I18n.t('Base_Yes'),
onPress: () => {
setIsVisible(false);
navigation.navigate('DocumentFormScreen', {
model,
modelId,
});
},
}}
translator={I18n.t}>
<Text>{I18n.t('Dms_DoYouWantToAddFile')}</Text>
</Alert>
</>
);
};

export default AttachedFilesScreen;
24 changes: 14 additions & 10 deletions packages/apps/dms/src/screens/DocumentFormScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import {createDocument, updateDocument} from '../features/documentSlice';
const DocumentFormScreen = ({navigation, route}) => {
const parent = route?.params?.parent;
const document = route?.params?.document;
const model = route?.params?.model;
const modelId = route?.params?.modelId;
const I18n = useTranslator();

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

const creationDefaultValue = useMemo(
() => ({
isAttachedFileCreation: model && modelId,
parent: parent,
}),
[parent],
[model, modelId, parent],
);

const defaultValue = useMemo(
Expand All @@ -48,25 +51,26 @@ const DocumentFormScreen = ({navigation, route}) => {

const documentAPI = useCallback(
(_document, isCreation, dispatch) => {
const parentId = _document.parent.id;
const parentId = _document.parent?.id;

if (parentId == null) {
_document.parent = null;
}

if (
parentId === user.dmsRoot?.id ||
parentId === mobileSettings.defaultDmsRoot?.id
) {
_document.parent = user.dmsRoot ?? mobileSettings.defaultDmsRoot;
if (parentId === user.dmsRoot?.id) {
_document.parent = user.dmsRoot;
}

if (parentId === mobileSettings.defaultDmsRoot?.id) {
_document.parent = mobileSettings.defaultDmsRoot;
}

const sliceFunction = isCreation ? createDocument : updateDocument;
dispatch((sliceFunction as any)({document: _document}));
dispatch((sliceFunction as any)({document: _document, model, modelId}));

navigation.navigate('AllDocumentsScreen');
navigation.goBack();
},
[mobileSettings.defaultDmsRoot, navigation, user.dmsRoot],
[mobileSettings.defaultDmsRoot, model, modelId, navigation, user.dmsRoot],
);

return (
Expand Down
9 changes: 9 additions & 0 deletions packages/apps/dms/src/screens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import AllDocumentsScreen from './AllDocumentsScreen';
import MyFavoriteDocumentsScreen from './MyFavoriteDocumentsScreen';
import DocumentFormScreen from './DocumentFormScreen';
import AttachedFilesScreen from './AttachedFilesScreen';

export default {
AllDocumentsScreen: {
Expand All @@ -41,8 +42,16 @@ export default {
title: 'Dms_Document',
component: DocumentFormScreen,
},
AttachedFilesScreen: {
title: 'Dms_AttachedFiles',
component: AttachedFilesScreen,
options: {
shadedHeader: false,
},
},
};

export {AllDocumentsScreen};
export {MyFavoriteDocumentsScreen};
export {DocumentFormScreen};
export {AttachedFilesScreen};
Loading

0 comments on commit 16e7dd4

Please sign in to comment.