Commit 09279597 authored by Maksym Tishchenko's avatar Maksym Tishchenko
Browse files

Admin CMS Menu deleting items

parent a9463eb9
......@@ -39,6 +39,7 @@ const URL_FETCH_ALL_FROM_TRANSIFEX = `/api/v1/cms/transifex/fetchAll`;
const URL_UPDATE_GLOBAL_ARTICLE = `/api/v1/cms/update-global-article`;
const URL_UPDATE_MENU = UrlTemplate.parse(`/api/v1/cms/update-menu/{menuKey}`);
const URL_UPDATE_MENU_ITEM = UrlTemplate.parse(`/api/v1/cms/menu-item/{id}`);
const URL_DELETE_MENU_ITEM = UrlTemplate.parse(`/api/v1/cms/menu-item/{id}`);
const URL_GET_ARTICLE = UrlTemplate.parse(`/api/v1/cms/{slug}/{clazz}/{targetId}/{language}`);
const URL_DELETE_ARTICLES = `/api/v2/cms/delete-articles`;
......@@ -601,6 +602,26 @@ class CmsService {
}).then(({ data }) => data as MenuItem);
}
/**
* deleteMenuItem at /api/v1/cms/menu-item/{id}
*
* @param id undefined
* @param xhrConfig additional xhr config
*/
public deleteMenuItem = (id: number, xhrConfig?: AxiosRequestConfig): Promise<MenuItem> => {
const apiUrl = URL_DELETE_MENU_ITEM.expand({ id });
// console.log(`Fetching from ${apiUrl}`);
const content = { /* No content in request body */ };
return this._axios.request({
...xhrConfig,
url: apiUrl,
method: 'DELETE',
...content,
}).then(({ data }) => data as MenuItem);
}
/**
* updateMenu at /api/v1/cms/update-menu/{menuKey}
*
......
......@@ -14,6 +14,7 @@ import {
UPLOAD_ACTIVITY_POST_FILE,
UPLOAD_ARTICLE_FILE,
RECEIVE_MENU, RECEIVE_MENU_ITEM,
REMOVE_MENU_ITEM,
} from 'cms/constants';
// model
import Article from '@genesys-pgr/client/model/cms/Article';
......@@ -49,6 +50,7 @@ const apiPostToTransifex = createPureApiCaller(CmsService.postToTransifex);
const apiDeleteFromTransifex = createPureApiCaller(CmsService.deleteFromTransifex);
const apiUploadActivityPostFile = createApiCaller(CmsService.uploadActivityPostFile, UPLOAD_ACTIVITY_POST_FILE);
const apiUploadArticleFile = createApiCaller(CmsService.uploadArticleFile, UPLOAD_ARTICLE_FILE);
const apiDeleteMenuItem = createApiCaller(CmsService.deleteMenuItem, REMOVE_MENU_ITEM);
const apiUpdateMenu = createPureApiCaller(CmsService.updateMenu);
const apiUpdateMenuItem = createPureApiCaller(CmsService.updateMenuItem);
......@@ -65,6 +67,10 @@ export const updateMenuItem = (item: MenuItem, id: number) => (dispatch) => {
return dispatch(apiUpdateMenuItem(item, id));
};
export const deleteMenuItem = (id: number) => (dispatch) => {
return dispatch(apiDeleteMenuItem(id));
};
export const receiveMenuItemSuccess = (item: MenuItem, itemIndex: number, menuKey: string) => (dispatch) => {
dispatch({ type: RECEIVE_MENU_ITEM, payload: { item, itemIndex, menuKey } });
};
......
......@@ -2,6 +2,7 @@ export const RECEIVE_ARTICLE = 'cms/public/RECEIVE_ARTICLE';
export const RECEIVE_DOCUMENTATION = 'cms/public/RECEIVE_DOCUMENTATION';
export const RECEIVE_MENU = 'cms/public/RECEIVE_MENU';
export const RECEIVE_MENU_ITEM = 'cms/public/RECEIVE_MENU_ITEM';
export const REMOVE_MENU_ITEM = 'cms/admin/REMOVE_MENU_ITEM';
export const RECEIVE_LAST_NEWS = 'cms/public/RECEIVE_LAST_NEWS';
export const RECEIVE_POST = 'cms/public/RECEIVE_POST';
export const APPEND_ACTIVITY_POSTS = 'cms/public/APPEND_ACTIVITY_POSTS';
......
......@@ -6,18 +6,20 @@ import {
RECEIVE_POST,
APPEND_ACTIVITY_POSTS,
RECEIVE_MENU_ITEM,
REMOVE_MENU_ITEM,
} from 'cms/constants';
import update from 'immutability-helper';
import ApiCall from '@genesys-pgr/client/model/ApiCall';
import FilteredPage from '@genesys-pgr/client/model/FilteredPage';
import ActivityPost from '@genesys-pgr/client/model/cms/ActivityPost';
import Menu from '@genesys-pgr/client/model/cms/Menu';
const INITIAL_STATE: {
articles: any,
documentations: any,
loadingSlug: string,
apiError: any,
menus: any,
menus: { [key: string]: Menu },
activityPost: {
paged: ApiCall<FilteredPage<ActivityPost>>,
currentPost: ApiCall<ActivityPost>,
......@@ -91,6 +93,30 @@ export default (state = INITIAL_STATE, action: { type?: string, payload?: any }
},
}) : state;
}
case REMOVE_MENU_ITEM: {
const { apiCall: { data } } = action.payload;
if (data) {
if (state.menus) {
for (const menu of Object.values(state.menus)) {
if (! menu.items) {
continue;
}
const updatedItems = menu.items.filter((item) => item.id !== data.id);
return update(state, {
menus: {
[menu.key]: {
items: { $set: updatedItems }
},
},
});
}
}
return update(state, {
menus: { $set: null },
});
}
return state;
}
case RECEIVE_LAST_NEWS: {
const { apiCall } = action.payload;
return update(state, {
......
......@@ -19,11 +19,15 @@ import { toggleUserModal } from 'user/actions/admin';
import Card from 'ui/common/Card';
import CardContent from 'ui/common/Card';
import MenuGroupForm from 'cms/ui/admin/c/MenuGroupForm';
import { deleteMenuItem } from 'cms/actions/admin';
import confirm from '@genesys-pgr/client/utilities/confirmAlert';
import MenuItem from '@genesys-pgr/client/model/cms/MenuItem';
interface IMenuPageProps extends WithTranslation, WithStyles {
loadMenu: (menuKey) => void;
menus: Menu[];
toggleUserModal: (open: boolean) => void;
deleteMenuItem: (id: number) => void;
}
interface IMenuPageState {
......@@ -35,6 +39,9 @@ class MenuPage extends React.Component<IMenuPageProps, IMenuPageState> {
public constructor(props) {
super(props);
this.handleChangeGroup = this.handleChangeGroup.bind(this);
this.openEditDialog = this.openEditDialog.bind(this);
this.handleRemoveItem = this.handleRemoveItem.bind(this);
}
public state = {
......@@ -51,6 +58,20 @@ class MenuPage extends React.Component<IMenuPageProps, IMenuPageState> {
this.setState({ menuKey })
}
private handleRemoveItem(item: MenuItem) {
const { t, deleteMenuItem } = this.props;
confirm(<span>{ t('common:action.delete') }
{ " " }
<b>{ item.title && item.title.toLowerCase() || t(`cms.${item.text}`).toLowerCase() }</b>
{ " " }
{ t("cms.admin.p.menu.menuItem").toLowerCase() } ?</span>, {
confirmLabel: t('common:action.delete'),
abortLabel: t('common:action.cancel'),
}).then(() => {
deleteMenuItem(item.id);
})
}
public componentDidMount() {
const { loadMenu, menus } = this.props;
const { menuKey } = this.state;
......@@ -84,7 +105,7 @@ class MenuPage extends React.Component<IMenuPageProps, IMenuPageState> {
<Card>
<CardContent>
<MenuGroupForm
onSubmit={ this.handleChangeGroup.bind(this) }
onSubmit={ this.handleChangeGroup }
initialValues={ { menuKey } }
/>
</CardContent>
......@@ -95,7 +116,8 @@ class MenuPage extends React.Component<IMenuPageProps, IMenuPageState> {
key={ index }
index={ index }
menuItem={ item }
handleEdit={ this.openEditDialog.bind(this) }
handleEdit={ this.openEditDialog }
handleRemove={ this.handleRemoveItem }
/>
)
}
......@@ -117,6 +139,7 @@ const mapStateToProps = (state) => ({
const mapDispatchToProps = (dispatch) => bindActionCreators({
loadMenu,
toggleUserModal,
deleteMenuItem,
}, dispatch);
export default withTranslation()(connect(mapStateToProps, mapDispatchToProps)(MenuPage));
......@@ -14,7 +14,7 @@ const styles = (theme) => ({
},
});
const MenuItemCard = ({ menuItem, handleEdit, classes, index, t, ...other }: WithTranslation & { menuItem: MenuItem, handleEdit: (index) => void, classes: any, index?: number} & React.ClassAttributes<any>) => {
const MenuItemCard = ({ menuItem, handleEdit, handleRemove, classes, index, t, ...other }: WithTranslation & { menuItem: MenuItem, handleEdit: (index) => void, handleRemove: (item: MenuItem) => void, classes: any, index?: number} & React.ClassAttributes<any>) => {
return (
<Card>
......@@ -24,7 +24,7 @@ const MenuItemCard = ({ menuItem, handleEdit, classes, index, t, ...other }: Wit
<div>
{ index !== undefined ? <b><Number value={ index + 1 }/>. </b> : '' }
<MenuItemLink menuItem={ menuItem }>
{ t ? t(`cms.${menuItem.text}`) : menuItem.text }
{ menuItem.text ? t(`cms.${menuItem.text}`) : menuItem.title }
</MenuItemLink>
</div>
}
......@@ -34,6 +34,7 @@ const MenuItemCard = ({ menuItem, handleEdit, classes, index, t, ...other }: Wit
/>
<CardActions>
<Button onClick={ () => handleEdit(index) }>{ t('common:action.edit') }</Button>
<Button onClick={ () => handleRemove(menuItem) }>{ t('common:action.delete') }</Button>
</CardActions>
</Card>
);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment