Commit d1393999 authored by Remy Huet's avatar Remy Huet

Add message

parent 04d02f56
Pipeline #31113 passed with stage
in 54 seconds
......@@ -2,7 +2,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import {
Dimmer, Loader, Container, Card, Button, Divider,
Dimmer, Loader, Container, Card, Button, Divider, Message,
} from 'semantic-ui-react';
import PropTypes from 'prop-types';
......@@ -15,7 +15,16 @@ import CategoryDelete from '../components/Category/Delete';
import CategoryCreate from '../components/Category/Create';
function Categories({
categoriesFetched, categoriesFetching, categories, dispatch, userToken, match, history,
categoriesFetched,
categoriesFetching,
categories,
dispatch,
userToken,
match,
history,
categoriesAction,
categoriesActionInfo,
categoriesActionSuccess,
}) {
const { params } = match;
// Map all categories in array of <Category />
......@@ -52,6 +61,16 @@ function Categories({
<Loader>Chargement en cours</Loader>
</Dimmer>)}
{categoriesAction && categoriesActionSuccess !== null && (
<Message
floated="center"
header={categoriesActionInfo}
positive={categoriesActionSuccess}
negative={!categoriesActionSuccess}
onDismiss={() => dispatch({ type: 'CATEGORY_MESSAGE_DISMISS' })}
/>
)}
<Button floated="right" style={{ marginBottom: '1em' }} color="orange" icon="redo" onClick={() => dispatch(fetchCategories(userToken))} />
<Button floated="right" style={{ marginBottom: '1em' }} positive icon="plus" as={Link} to="/categories/create" />
......@@ -81,12 +100,12 @@ function Categories({
<CategoryDelete
id={id}
name={name}
callback={() => history.push('categories')}
callback={() => history.push('/categories')}
/>
),
create: (
<CategoryCreate
callback={() => history.push('categories')}
callback={() => history.push('/categories')}
/>
),
}[params.action])}
......@@ -108,6 +127,9 @@ Categories.propTypes = {
).isRequired,
dispatch: PropTypes.func.isRequired,
userToken: PropTypes.string.isRequired,
categoriesAction: PropTypes.string.isRequired,
categoriesActionInfo: PropTypes.string.isRequired,
categoriesActionSuccess: PropTypes.bool.isRequired,
match: PropTypes.shape({
params: PropTypes.object,
}).isRequired,
......@@ -120,5 +142,8 @@ export default connect(store => ({
categoriesFetched: store.categories.fetched,
categoriesFetching: store.categories.fetching,
categories: store.categories.categories,
categoriesAction: store.categories.action,
categoriesActionSuccess: store.categories.success,
categoriesActionInfo: store.categories.info,
userToken: store.user.token,
}))(Categories);
const initialState = {
fetching: false,
fetched: false,
action: null,
success: null,
info: null,
categories: [],
};
......@@ -11,6 +14,9 @@ export default function reducer(state = initialState, action) {
...state,
fetching: true,
fetched: false,
action: null,
success: null,
info: null,
};
}
case ('FETCH_CATEGORIES_FULFILLED'): {
......@@ -23,24 +29,54 @@ export default function reducer(state = initialState, action) {
}
case ('DELETE_CATEGORY_PENDING'): {
return {
...state, // TODO
...state,
action: 'DELETE',
success: null,
};
}
case ('DELETE_CATEGORY_FULFILLED'): {
return {
...state,
categories: state.categories.filter(category => category.id !== action.payload.data.id),
success: true,
info: 'Catégorie supprimée avec succès',
};
}
case ('DELETE_CATEGORY_REJECTED'): {
return {
...state,
success: false,
info: 'Impossible de supprimer la catégorie',
};
}
case ('CREATE_CATEGORY_PENDING'): {
return {
...state, // TODO
...state,
action: 'CREATE',
success: null,
};
}
case ('CREATE_CATEGORY_FULFILLED'): {
return {
...state,
categories: [...state.categories, action.payload.data],
success: true,
info: 'Catégorie créée avec succès',
};
}
case ('CREATE_CATEGORY_REJECTED'): {
return {
...state,
success: false,
info: 'Impossible de créer la catégorie',
};
}
case ('CATEGORY_MESSAGE_DISMISS'): {
return {
...state,
action: null,
success: null,
info: null,
};
}
default: {
......
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