collection list, move, delete
This commit is contained in:
@ -2,7 +2,7 @@ import { createSlice, configureStore } from '@reduxjs/toolkit';
|
||||
import { ClipMetadata, MetadataState } from './types';
|
||||
|
||||
const initialState: MetadataState = {
|
||||
collections: {},
|
||||
collections: [],
|
||||
};
|
||||
const metadataSlice = createSlice({
|
||||
name: 'metadata',
|
||||
@ -13,32 +13,87 @@ const metadataSlice = createSlice({
|
||||
},
|
||||
setCollections(state, action) {
|
||||
const { collection, newMetadata } = action.payload;
|
||||
state.collections[collection] = newMetadata;
|
||||
const index = state.collections.findIndex(
|
||||
(col) => col.name === collection,
|
||||
);
|
||||
if (index !== -1) {
|
||||
state.collections[index] = newMetadata;
|
||||
}
|
||||
},
|
||||
addCollection(state, action) {
|
||||
const name = action.payload;
|
||||
if (!state.collections.find((col) => col.name === name)) {
|
||||
state.collections.push({ name, id: Date.now(), clips: [] });
|
||||
}
|
||||
},
|
||||
editClip(state, action) {
|
||||
const { collection, clip } = action.payload;
|
||||
const clips = state.collections[collection];
|
||||
const collectionState = state.collections.find(
|
||||
(col) => col.name === collection,
|
||||
);
|
||||
// console.log('Editing clip in collection:', collection, clip);
|
||||
if (clips) {
|
||||
const index = clips.findIndex((c) => c.filename === clip.filename);
|
||||
if (collectionState) {
|
||||
const index = collectionState.clips.findIndex(
|
||||
(c) => c.filename === clip.filename,
|
||||
);
|
||||
if (index !== -1) {
|
||||
clips[index] = clip;
|
||||
collectionState.clips[index] = clip;
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteClip(state, action) {
|
||||
const { collection, clip } = action.payload;
|
||||
const collectionState = state.collections.find(
|
||||
(col) => col.name === collection,
|
||||
);
|
||||
if (collectionState) {
|
||||
collectionState.clips = collectionState.clips.filter(
|
||||
(c) => c.filename !== clip.filename,
|
||||
);
|
||||
}
|
||||
},
|
||||
moveClip(state, action) {
|
||||
const { sourceCollection, targetCollection, clip } = action.payload;
|
||||
const sourceState = state.collections.find(
|
||||
(col) => col.name === sourceCollection,
|
||||
);
|
||||
const targetState = state.collections.find(
|
||||
(col) => col.name === targetCollection,
|
||||
);
|
||||
if (sourceState && targetState) {
|
||||
sourceState.clips = sourceState.clips.filter(
|
||||
(c) => c.filename !== clip.filename,
|
||||
);
|
||||
targetState.clips.push(clip);
|
||||
}
|
||||
},
|
||||
addNewClips(state, action) {
|
||||
const { collections } = action.payload;
|
||||
Object.keys(collections).forEach((collection) => {
|
||||
if (!state.collections[collection]) {
|
||||
state.collections[collection] = [];
|
||||
const collectionState = state.collections.find(
|
||||
(col) => col.name === collection,
|
||||
);
|
||||
if (!collectionState) {
|
||||
state.collections.push({
|
||||
name: collection,
|
||||
id: Date.now(),
|
||||
clips: [],
|
||||
});
|
||||
}
|
||||
const existingFilenames = new Set(
|
||||
state.collections[collection].map((clip) => clip.filename),
|
||||
state.collections
|
||||
.find((col) => col.name === collection)
|
||||
?.clips.map((clip) => clip.filename) || [],
|
||||
);
|
||||
const newClips = collections[collection].filter(
|
||||
(clip: ClipMetadata) => !existingFilenames.has(clip.filename),
|
||||
);
|
||||
state.collections[collection].push(...newClips);
|
||||
// const collectionState = state.collections.find(
|
||||
// (col) => col.name === collection,
|
||||
// );
|
||||
if (collectionState) {
|
||||
collectionState.clips.push(...newClips);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
@ -58,5 +113,6 @@ export type RootState = ReturnType<AppStore['getState']>;
|
||||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
|
||||
export type AppDispatch = AppStore['dispatch'];
|
||||
|
||||
export const { setCollections, addNewClips } = metadataSlice.actions;
|
||||
export const { setCollections, addNewClips, addCollection } =
|
||||
metadataSlice.actions;
|
||||
export default metadataSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user