mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-23 13:29:35 +02:00
Changing bookmark category
This commit is contained in:
parent
96aa1f7d69
commit
08c769b630
5 changed files with 64 additions and 48 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,3 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
|
data/
|
||||||
.env
|
.env
|
||||||
*.sqlite
|
|
|
@ -17,7 +17,7 @@ interface ComponentProps {
|
||||||
addCategory: (formData: NewCategory) => void;
|
addCategory: (formData: NewCategory) => void;
|
||||||
addBookmark: (formData: NewBookmark) => void;
|
addBookmark: (formData: NewBookmark) => void;
|
||||||
updateCategory: (id: number, formData: NewCategory) => void;
|
updateCategory: (id: number, formData: NewCategory) => void;
|
||||||
updateBookmark: (id: number, formData: NewBookmark, categoryWasChanged: boolean) => void;
|
updateBookmark: (id: number, formData: NewBookmark, previousCategoryId: number) => void;
|
||||||
createNotification: (notification: NewNotification) => void;
|
createNotification: (notification: NewNotification) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ const BookmarkForm = (props: ComponentProps): JSX.Element => {
|
||||||
setCategoryName({ name: '' });
|
setCategoryName({ name: '' });
|
||||||
} else if (props.contentType === ContentType.bookmark && props.bookmark) {
|
} else if (props.contentType === ContentType.bookmark && props.bookmark) {
|
||||||
// Update bookmark
|
// Update bookmark
|
||||||
props.updateBookmark(props.bookmark.id, formData, props.bookmark.categoryId !== formData.categoryId);
|
props.updateBookmark(props.bookmark.id, formData, props.bookmark.categoryId);
|
||||||
setFormData({
|
setFormData({
|
||||||
name: '',
|
name: '',
|
||||||
url: '',
|
url: '',
|
||||||
|
|
|
@ -218,13 +218,10 @@ export const deleteBookmark = (bookmarkId: number, categoryId: number) => async
|
||||||
*/
|
*/
|
||||||
export interface UpdateBookmarkAction {
|
export interface UpdateBookmarkAction {
|
||||||
type: ActionTypes.updateBookmark,
|
type: ActionTypes.updateBookmark,
|
||||||
payload: {
|
payload: Bookmark
|
||||||
bookmark: Bookmark,
|
|
||||||
categoryWasChanged: boolean
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateBookmark = (bookmarkId: number, formData: NewBookmark, categoryWasChanged: boolean) => async (dispatch: Dispatch) => {
|
export const updateBookmark = (bookmarkId: number, formData: NewBookmark, previousCategoryId: number) => async (dispatch: Dispatch) => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.put<ApiResponse<Bookmark>>(`/api/bookmarks/${bookmarkId}`, formData);
|
const res = await axios.put<ApiResponse<Bookmark>>(`/api/bookmarks/${bookmarkId}`, formData);
|
||||||
|
|
||||||
|
@ -236,13 +233,31 @@ export const updateBookmark = (bookmarkId: number, formData: NewBookmark, catego
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
dispatch<UpdateBookmarkAction>({
|
// Check if category was changed
|
||||||
type: ActionTypes.updateBookmark,
|
const categoryWasChanged = formData.categoryId !== previousCategoryId;
|
||||||
payload: {
|
|
||||||
bookmark: res.data.data,
|
if (categoryWasChanged) {
|
||||||
categoryWasChanged
|
// Delete bookmark from old category
|
||||||
}
|
dispatch<DeleteBookmarkAction>({
|
||||||
})
|
type: ActionTypes.deleteBookmark,
|
||||||
|
payload: {
|
||||||
|
bookmarkId,
|
||||||
|
categoryId: previousCategoryId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Add bookmark to the new category
|
||||||
|
dispatch<AddBookmarkAction>({
|
||||||
|
type: ActionTypes.addBookmark,
|
||||||
|
payload: res.data.data
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Else update only name/url
|
||||||
|
dispatch<UpdateBookmarkAction>({
|
||||||
|
type: ActionTypes.updateBookmark,
|
||||||
|
payload: res.data.data
|
||||||
|
})
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,28 +30,36 @@ const getCategoriesSuccess = (state: State, action: Action): State => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const addCategory = (state: State, action: Action): State => {
|
const addCategory = (state: State, action: Action): State => {
|
||||||
const tmpCategories = [...state.categories, {
|
|
||||||
...action.payload,
|
|
||||||
bookmarks: []
|
|
||||||
}];
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
categories: tmpCategories
|
categories: [
|
||||||
|
...state.categories,
|
||||||
|
{
|
||||||
|
...action.payload,
|
||||||
|
bookmarks: []
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const addBookmark = (state: State, action: Action): State => {
|
const addBookmark = (state: State, action: Action): State => {
|
||||||
const tmpCategories = [...state.categories];
|
const categoryIndex = state.categories.findIndex((category: Category) => category.id === action.payload.categoryId);
|
||||||
const tmpCategory = tmpCategories.find((category: Category) => category.id === action.payload.categoryId);
|
|
||||||
|
|
||||||
if (tmpCategory) {
|
|
||||||
tmpCategory.bookmarks.push(action.payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
categories: tmpCategories
|
categories: [
|
||||||
|
...state.categories.slice(0, categoryIndex),
|
||||||
|
{
|
||||||
|
...state.categories[categoryIndex],
|
||||||
|
bookmarks: [
|
||||||
|
...state.categories[categoryIndex].bookmarks,
|
||||||
|
{
|
||||||
|
...action.payload
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
...state.categories.slice(categoryIndex + 1)
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,11 +78,14 @@ const pinCategory = (state: State, action: Action): State => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteCategory = (state: State, action: Action): State => {
|
const deleteCategory = (state: State, action: Action): State => {
|
||||||
const tmpCategories = [...state.categories].filter((category: Category) => category.id !== action.payload);
|
const categoryIndex = state.categories.findIndex((category: Category) => category.id === action.payload);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
categories: tmpCategories
|
categories: [
|
||||||
|
...state.categories.slice(0, categoryIndex),
|
||||||
|
...state.categories.slice(categoryIndex + 1)
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +111,7 @@ const deleteBookmark = (state: State, action: Action): State => {
|
||||||
categoryInUpdate.bookmarks = categoryInUpdate.bookmarks.filter((bookmark: Bookmark) => bookmark.id !== action.payload.bookmarkId);
|
categoryInUpdate.bookmarks = categoryInUpdate.bookmarks.filter((bookmark: Bookmark) => bookmark.id !== action.payload.bookmarkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
categories: tmpCategories
|
categories: tmpCategories
|
||||||
|
@ -107,21 +119,8 @@ const deleteBookmark = (state: State, action: Action): State => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateBookmark = (state: State, action: Action): State => {
|
const updateBookmark = (state: State, action: Action): State => {
|
||||||
const { bookmark, categoryWasChanged } = action.payload;
|
let categoryIndex = state.categories.findIndex((category: Category) => category.id === action.payload.categoryId);
|
||||||
const tmpCategories = [...state.categories];
|
let bookmarkIndex = state.categories[categoryIndex].bookmarks.findIndex((bookmark: Bookmark) => bookmark.id === action.payload.id);
|
||||||
|
|
||||||
let categoryIndex = state.categories.findIndex((category: Category) => category.id === bookmark.categoryId);
|
|
||||||
let bookmarkIndex = state.categories[categoryIndex].bookmarks.findIndex((bookmark: Bookmark) => bookmark.id === bookmark.id);
|
|
||||||
|
|
||||||
// if (categoryWasChanged) {
|
|
||||||
// const categoryInUpdate = tmpCategories.find((category: Category) => category.id === bookmark.categoryId);
|
|
||||||
|
|
||||||
// if (categoryInUpdate) {
|
|
||||||
// categoryInUpdate.bookmarks = categoryInUpdate.bookmarks.filter((_bookmark: Bookmark) => _bookmark.id === bookmark.id);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// console.log(categoryInUpdate);
|
|
||||||
// }
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
@ -132,7 +131,7 @@ const updateBookmark = (state: State, action: Action): State => {
|
||||||
bookmarks: [
|
bookmarks: [
|
||||||
...state.categories[categoryIndex].bookmarks.slice(0, bookmarkIndex),
|
...state.categories[categoryIndex].bookmarks.slice(0, bookmarkIndex),
|
||||||
{
|
{
|
||||||
...bookmark
|
...action.payload
|
||||||
},
|
},
|
||||||
...state.categories[categoryIndex].bookmarks.slice(bookmarkIndex + 1)
|
...state.categories[categoryIndex].bookmarks.slice(bookmarkIndex + 1)
|
||||||
]
|
]
|
||||||
|
|
4
db.js
4
db.js
|
@ -2,13 +2,15 @@ const { Sequelize } = require('sequelize');
|
||||||
|
|
||||||
const sequelize = new Sequelize({
|
const sequelize = new Sequelize({
|
||||||
dialect: 'sqlite',
|
dialect: 'sqlite',
|
||||||
storage: './db.sqlite'
|
storage: './data/db.sqlite',
|
||||||
|
logging: false
|
||||||
});
|
});
|
||||||
|
|
||||||
const connectDB = async () => {
|
const connectDB = async () => {
|
||||||
try {
|
try {
|
||||||
await sequelize.authenticate({ logging: false });
|
await sequelize.authenticate({ logging: false });
|
||||||
console.log('Connected to database'.cyan.underline);
|
console.log('Connected to database'.cyan.underline);
|
||||||
|
|
||||||
await sequelize.sync({
|
await sequelize.sync({
|
||||||
// alter: true,
|
// alter: true,
|
||||||
logging: false
|
logging: false
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue