Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Allow tag library to be loaded from dynamoDB (#1328)

Stanimir2f83f3a59b34
diff --git a/.prettierignore b/.prettierignore
index 4e8ddee0a..e29dbb014 100755
--- a/.prettierignore
+++ b/.prettierignore
@@ -16,3 +16,4 @@ tests/testdata
tests/testdata-tmp
tests/file-structure
.ycache
+app/aws-exports.js
diff --git a/app/reducers/taglibrary.ts b/app/reducers/taglibrary.ts
index 8cc6369e6..c3f6efc7c 100644
--- a/app/reducers/taglibrary.ts
+++ b/app/reducers/taglibrary.ts
@@ -17,8 +17,8 @@
*/
import uuidv1 from 'uuid';
-import { immutablySwapItems, formatDateTime4Tag, extend } from '../utils/misc';
-import { saveAsTextFile } from '../services/utils-io';
+import { immutablySwapItems, formatDateTime4Tag, extend } from '-/utils/misc';
+import { saveAsTextFile } from '-/services/utils-io';
import versionMeta from '../version.json';
import defaultTagLibrary from './taglibrary-default';
import AppConfig from '../config';
@@ -397,7 +397,7 @@ export default (state: Array<TagGroup> = defaultTagLibrary, action: any) => {
return state;
}
case types.IMPORT_TAGGROUP: {
- const arr = [...state];
+ const arr = action.replace ? [] : [...state];
console.log(arr);
if (action.entry[0].key) {
action.entry.forEach((tagGroup, index) => {
@@ -419,10 +419,10 @@ export default (state: Array<TagGroup> = defaultTagLibrary, action: any) => {
});
} else {
action.entry.forEach(tagGroup => {
- const index = state.findIndex(obj => obj.uuid === tagGroup.uuid);
+ const index = arr.findIndex(obj => obj.uuid === tagGroup.uuid);
if (index > -1) {
tagGroup.children.forEach(tag => {
- const stateTag = state[index].children.find(
+ const stateTag = arr[index].children.find(
obj => obj.title === tag.title
);
if (stateTag === undefined) {
@@ -502,7 +502,27 @@ export const actions = {
fromTagGroupId: fromTagGroupUuid,
toTagGroupId: toTagGroupUuid
}),
- importTagGroups: entry => ({ type: types.IMPORT_TAGGROUP, entry }),
+ importTagGroups: (entry, replace: boolean = false) => ({
+ type: types.IMPORT_TAGGROUP,
+ entry,
+ replace
+ }),
+ /**
+ * GraphQL API return TagGroup Tags array with tags.items: []
+ * This migrate tagGroups model tag.items to children
+ * @param entries
+ */
+ addTagGroups: (entries: Array<any>) => (
+ dispatch: (actions: Object) => void
+ ) => {
+ if (entries && entries.length > 0) {
+ const tagGroups: Array<TagGroup> = entries.map(tagGroup => {
+ const { tags, ...tagGroupProps } = tagGroup;
+ return { children: tags.items, ...tagGroupProps };
+ });
+ dispatch(actions.importTagGroups(tagGroups, true));
+ }
+ },
exportTagGroups: (entry: Array<Object>) => (
dispatch: (actions: Object) => void,
getState: () => any
diff --git a/app/utils/HandleAuth.ts b/app/utils/HandleAuth.ts
index 68d808904..4737db091 100644
--- a/app/utils/HandleAuth.ts
+++ b/app/utils/HandleAuth.ts
@@ -8,12 +8,14 @@ import { API, Auth } from 'aws-amplify';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { actions as LocationActions, Location } from '-/reducers/locations';
+import { actions as TagGroupActions, TagGroup } from '-/reducers/taglibrary';
import { actions as AppActions } from '-/reducers/app';
interface Props {
loggedIn: (user: CognitoUserInterface) => void;
initApp: () => void;
addLocations: (locations: Array<Location>, override: boolean) => void;
+ addTagGroups: (tagGroups: Array<TagGroup>) => void;
}
const HandleAuth = React.memo((props: Props) => {
const username = useRef(undefined);
@@ -39,15 +41,8 @@ const HandleAuth = React.memo((props: Props) => {
if (username.current !== authData.username && queries) {
fetchTenant()
.then(async tenant => {
- // @ts-ignore
- const { data } = await API.graphql({
- query: queries.getExtconfig,
- variables: { id: tenant }
- });
- if (data) {
- // console.log(data.getExtconfig.Locations.items);
- props.addLocations(data.getExtconfig.Locations.items, false);
- }
+ await addLocations(tenant, queries.getExtconfig);
+ await addTagGroups(tenant, queries.listTagGroups);
return true;
})
@@ -67,6 +62,34 @@ const HandleAuth = React.memo((props: Props) => {
});
}, []);
+ const addLocations = async (tenant: string, query: any) => {
+ // @ts-ignore
+ const { data } = await API.graphql({
+ query,
+ variables: { id: tenant }
+ });
+ if (data) {
+ // console.log(data.getExtconfig.Locations.items);
+ props.addLocations(data.getExtconfig.Locations.items, false);
+ }
+ };
+
+ const addTagGroups = async (tenant: string, query: any) => {
+ const filter = {
+ tenant: {
+ eq: tenant
+ }
+ };
+ // @ts-ignore
+ const { data } = await API.graphql({
+ query,
+ variables: { filter }
+ });
+ if (data) {
+ props.addTagGroups(data.listTagGroups.items);
+ }
+ };
+
const fetchTenant = () =>
// get the access token of the signed in user
Auth.currentSession()
@@ -87,7 +110,8 @@ function mapDispatchToProps(dispatch) {
{
loggedIn: AppActions.loggedIn,
initApp: AppActions.initApp,
- addLocations: LocationActions.addLocations
+ addLocations: LocationActions.addLocations,
+ addTagGroups: TagGroupActions.addTagGroups
},
dispatch
);