refactors collection ops code
This commit is contained in:
parent
b7497ab647
commit
29d02afba8
|
@ -18,14 +18,8 @@ import { Collection } from 'services/collectionService';
|
||||||
import RemoveIcon from 'components/icons/RemoveIcon';
|
import RemoveIcon from 'components/icons/RemoveIcon';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
addToCollectionHelper: (
|
addToCollectionHelper: (collection: Collection) => void;
|
||||||
collectionName: string,
|
moveToCollectionHelper: (collection: Collection) => void;
|
||||||
collection: Collection
|
|
||||||
) => void;
|
|
||||||
moveToCollectionHelper: (
|
|
||||||
collectionName: string,
|
|
||||||
collection: Collection
|
|
||||||
) => void;
|
|
||||||
showCreateCollectionModal: (opsType: COLLECTION_OPS_TYPE) => () => void;
|
showCreateCollectionModal: (opsType: COLLECTION_OPS_TYPE) => () => void;
|
||||||
setDialogMessage: SetDialogMessage;
|
setDialogMessage: SetDialogMessage;
|
||||||
setCollectionSelectorAttributes: SetCollectionSelectorAttributes;
|
setCollectionSelectorAttributes: SetCollectionSelectorAttributes;
|
||||||
|
@ -75,7 +69,7 @@ const SelectedFileOptions = ({
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const addToCollection = () =>
|
const addToCollection = () =>
|
||||||
setCollectionSelectorAttributes({
|
setCollectionSelectorAttributes({
|
||||||
callback: (collection) => addToCollectionHelper(null, collection),
|
callback: addToCollectionHelper,
|
||||||
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.ADD),
|
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.ADD),
|
||||||
title: constants.ADD_TO_COLLECTION,
|
title: constants.ADD_TO_COLLECTION,
|
||||||
fromCollection: activeCollection,
|
fromCollection: activeCollection,
|
||||||
|
@ -109,7 +103,7 @@ const SelectedFileOptions = ({
|
||||||
|
|
||||||
const moveToCollection = () => {
|
const moveToCollection = () => {
|
||||||
setCollectionSelectorAttributes({
|
setCollectionSelectorAttributes({
|
||||||
callback: (collection) => moveToCollectionHelper(null, collection),
|
callback: moveToCollectionHelper,
|
||||||
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.MOVE),
|
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.MOVE),
|
||||||
title: constants.MOVE_TO_COLLECTION,
|
title: constants.MOVE_TO_COLLECTION,
|
||||||
fromCollection: activeCollection,
|
fromCollection: activeCollection,
|
||||||
|
|
|
@ -25,6 +25,8 @@ import {
|
||||||
getFavItemIds,
|
getFavItemIds,
|
||||||
getLocalCollections,
|
getLocalCollections,
|
||||||
getNonEmptyCollections,
|
getNonEmptyCollections,
|
||||||
|
createCollection,
|
||||||
|
CollectionType,
|
||||||
} from 'services/collectionService';
|
} from 'services/collectionService';
|
||||||
import constants from 'utils/strings/constants';
|
import constants from 'utils/strings/constants';
|
||||||
import billingService from 'services/billingService';
|
import billingService from 'services/billingService';
|
||||||
|
@ -68,9 +70,9 @@ import { AppContext } from 'pages/_app';
|
||||||
import { CustomError, ServerErrorCodes } from 'utils/common/errorUtil';
|
import { CustomError, ServerErrorCodes } from 'utils/common/errorUtil';
|
||||||
import { PAGES } from 'types';
|
import { PAGES } from 'types';
|
||||||
import {
|
import {
|
||||||
copyOrMoveFromCollection,
|
|
||||||
COLLECTION_OPS_TYPE,
|
COLLECTION_OPS_TYPE,
|
||||||
isSharedCollection,
|
isSharedCollection,
|
||||||
|
handleCollectionOps,
|
||||||
} from 'utils/collection';
|
} from 'utils/collection';
|
||||||
import { logError } from 'utils/sentry';
|
import { logError } from 'utils/sentry';
|
||||||
|
|
||||||
|
@ -315,59 +317,30 @@ export default function Gallery() {
|
||||||
if (!files) {
|
if (!files) {
|
||||||
return <div />;
|
return <div />;
|
||||||
}
|
}
|
||||||
const addToCollectionHelper = async (
|
const collectionOpsHelper =
|
||||||
collectionName: string,
|
(ops: COLLECTION_OPS_TYPE) => async (collection: Collection) => {
|
||||||
collection: Collection
|
loadingBar.current?.continuousStart();
|
||||||
) => {
|
try {
|
||||||
loadingBar.current?.continuousStart();
|
await handleCollectionOps(
|
||||||
try {
|
ops,
|
||||||
await copyOrMoveFromCollection(
|
setCollectionSelectorView,
|
||||||
COLLECTION_OPS_TYPE.ADD,
|
selected,
|
||||||
setCollectionSelectorView,
|
files,
|
||||||
selected,
|
clearSelection,
|
||||||
files,
|
syncWithRemote,
|
||||||
clearSelection,
|
setActiveCollection,
|
||||||
syncWithRemote,
|
collection
|
||||||
setActiveCollection,
|
);
|
||||||
collectionName,
|
} catch (e) {
|
||||||
collection
|
setDialogMessage({
|
||||||
);
|
title: constants.ERROR,
|
||||||
} catch (e) {
|
staticBackdrop: true,
|
||||||
setDialogMessage({
|
close: { variant: 'danger' },
|
||||||
title: constants.ERROR,
|
content: constants.UNKNOWN_ERROR,
|
||||||
staticBackdrop: true,
|
});
|
||||||
close: { variant: 'danger' },
|
}
|
||||||
content: constants.UNKNOWN_ERROR,
|
};
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const moveToCollectionHelper = async (
|
|
||||||
collectionName: string,
|
|
||||||
collection: Collection
|
|
||||||
) => {
|
|
||||||
loadingBar.current?.continuousStart();
|
|
||||||
try {
|
|
||||||
await copyOrMoveFromCollection(
|
|
||||||
COLLECTION_OPS_TYPE.MOVE,
|
|
||||||
setCollectionSelectorView,
|
|
||||||
selected,
|
|
||||||
files,
|
|
||||||
clearSelection,
|
|
||||||
syncWithRemote,
|
|
||||||
setActiveCollection,
|
|
||||||
collectionName,
|
|
||||||
collection
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
setDialogMessage({
|
|
||||||
title: constants.ERROR,
|
|
||||||
staticBackdrop: true,
|
|
||||||
close: { variant: 'danger' },
|
|
||||||
content: constants.UNKNOWN_ERROR,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const changeFilesVisibilityHelper = async (
|
const changeFilesVisibilityHelper = async (
|
||||||
visibility: VISIBILITY_STATE
|
visibility: VISIBILITY_STATE
|
||||||
) => {
|
) => {
|
||||||
|
@ -403,40 +376,33 @@ export default function Gallery() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const showCreateCollectionModal = (opsType: COLLECTION_OPS_TYPE) => {
|
const showCreateCollectionModal = (ops: COLLECTION_OPS_TYPE) => {
|
||||||
try {
|
const callback = async (collectionName: string) => {
|
||||||
let callback = null;
|
try {
|
||||||
switch (opsType) {
|
const collection = await createCollection(
|
||||||
case COLLECTION_OPS_TYPE.ADD:
|
collectionName,
|
||||||
callback = (collectionName: string) =>
|
CollectionType.album,
|
||||||
addToCollectionHelper(collectionName, null);
|
collections
|
||||||
break;
|
);
|
||||||
case COLLECTION_OPS_TYPE.MOVE:
|
|
||||||
callback = (collectionName: string) =>
|
await collectionOpsHelper(ops)(collection);
|
||||||
moveToCollectionHelper(collectionName, null);
|
} catch (e) {
|
||||||
break;
|
logError(e, 'create and collection ops failed');
|
||||||
default:
|
setDialogMessage({
|
||||||
throw Error(CustomError.INVALID_COLLECTION_OPERATION);
|
title: constants.ERROR,
|
||||||
}
|
staticBackdrop: true,
|
||||||
return () =>
|
close: { variant: 'danger' },
|
||||||
setCollectionNamerAttributes({
|
content: constants.UNKNOWN_ERROR,
|
||||||
title: constants.CREATE_COLLECTION,
|
|
||||||
buttonText: constants.CREATE,
|
|
||||||
autoFilledName: '',
|
|
||||||
callback,
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
}
|
||||||
logError(
|
};
|
||||||
e,
|
return () =>
|
||||||
'showCreateCollectionModal called with incorrect attributes'
|
setCollectionNamerAttributes({
|
||||||
);
|
title: constants.CREATE_COLLECTION,
|
||||||
setDialogMessage({
|
buttonText: constants.CREATE,
|
||||||
title: constants.ERROR,
|
autoFilledName: '',
|
||||||
staticBackdrop: true,
|
callback,
|
||||||
close: { variant: 'danger' },
|
|
||||||
content: constants.UNKNOWN_ERROR,
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteFileHelper = async () => {
|
const deleteFileHelper = async () => {
|
||||||
|
@ -605,7 +571,9 @@ export default function Gallery() {
|
||||||
{selected.count > 0 &&
|
{selected.count > 0 &&
|
||||||
selected.collectionID === activeCollection && (
|
selected.collectionID === activeCollection && (
|
||||||
<SelectedFileOptions
|
<SelectedFileOptions
|
||||||
addToCollectionHelper={addToCollectionHelper}
|
addToCollectionHelper={collectionOpsHelper(
|
||||||
|
COLLECTION_OPS_TYPE.ADD
|
||||||
|
)}
|
||||||
archiveFilesHelper={() =>
|
archiveFilesHelper={() =>
|
||||||
changeFilesVisibilityHelper(
|
changeFilesVisibilityHelper(
|
||||||
VISIBILITY_STATE.ARCHIVED
|
VISIBILITY_STATE.ARCHIVED
|
||||||
|
@ -616,7 +584,9 @@ export default function Gallery() {
|
||||||
VISIBILITY_STATE.VISIBLE
|
VISIBILITY_STATE.VISIBLE
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
moveToCollectionHelper={moveToCollectionHelper}
|
moveToCollectionHelper={collectionOpsHelper(
|
||||||
|
COLLECTION_OPS_TYPE.ADD
|
||||||
|
)}
|
||||||
showCreateCollectionModal={
|
showCreateCollectionModal={
|
||||||
showCreateCollectionModal
|
showCreateCollectionModal
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import {
|
import {
|
||||||
addToCollection,
|
addToCollection,
|
||||||
Collection,
|
Collection,
|
||||||
CollectionType,
|
|
||||||
createCollection,
|
|
||||||
moveToCollection,
|
moveToCollection,
|
||||||
} from 'services/collectionService';
|
} from 'services/collectionService';
|
||||||
import { getSelectedFiles } from 'utils/file';
|
import { getSelectedFiles } from 'utils/file';
|
||||||
|
@ -15,8 +13,9 @@ import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
||||||
export enum COLLECTION_OPS_TYPE {
|
export enum COLLECTION_OPS_TYPE {
|
||||||
ADD,
|
ADD,
|
||||||
MOVE,
|
MOVE,
|
||||||
|
REMOVE,
|
||||||
}
|
}
|
||||||
export async function copyOrMoveFromCollection(
|
export async function handleCollectionOps(
|
||||||
type: COLLECTION_OPS_TYPE,
|
type: COLLECTION_OPS_TYPE,
|
||||||
setCollectionSelectorView: (value: boolean) => void,
|
setCollectionSelectorView: (value: boolean) => void,
|
||||||
selected: SelectedState,
|
selected: SelectedState,
|
||||||
|
@ -24,19 +23,9 @@ export async function copyOrMoveFromCollection(
|
||||||
clearSelection: () => void,
|
clearSelection: () => void,
|
||||||
syncWithRemote: () => Promise<void>,
|
syncWithRemote: () => Promise<void>,
|
||||||
setActiveCollection: (id: number) => void,
|
setActiveCollection: (id: number) => void,
|
||||||
collectionName: string,
|
collection: Collection
|
||||||
existingCollection: Collection
|
|
||||||
) {
|
) {
|
||||||
setCollectionSelectorView(false);
|
setCollectionSelectorView(false);
|
||||||
let collection: Collection;
|
|
||||||
if (!existingCollection) {
|
|
||||||
collection = await createCollection(
|
|
||||||
collectionName,
|
|
||||||
CollectionType.album
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
collection = existingCollection;
|
|
||||||
}
|
|
||||||
const selectedFiles = getSelectedFiles(selected, files);
|
const selectedFiles = getSelectedFiles(selected, files);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case COLLECTION_OPS_TYPE.ADD:
|
case COLLECTION_OPS_TYPE.ADD:
|
||||||
|
@ -49,6 +38,8 @@ export async function copyOrMoveFromCollection(
|
||||||
selectedFiles
|
selectedFiles
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
case COLLECTION_OPS_TYPE.REMOVE:
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw Error(CustomError.INVALID_COLLECTION_OPERATION);
|
throw Error(CustomError.INVALID_COLLECTION_OPERATION);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ export enum CustomError {
|
||||||
SIGNUP_FAILED = 'signup failed',
|
SIGNUP_FAILED = 'signup failed',
|
||||||
FAV_COLLECTION_MISSING = 'favorite collection missing',
|
FAV_COLLECTION_MISSING = 'favorite collection missing',
|
||||||
INVALID_COLLECTION_OPERATION = 'invalid collection operation',
|
INVALID_COLLECTION_OPERATION = 'invalid collection operation',
|
||||||
|
INVALID_COLLECTION = 'invalid collection attribute',
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseUploadError(error: AxiosResponse) {
|
function parseUploadError(error: AxiosResponse) {
|
||||||
|
|
Loading…
Reference in a new issue