Commit 7eaf3f24 authored by Étienne Després's avatar Étienne Després

WIP: move away from Redux

parent 18af7b68
import { createContext, useEffect, useReducer } from 'react';
import useActions from './actions';
import { initialState, reducer } from './reducers';
const StoreContext = createContext(initialState);
console.log('initialState', initialState, 'reducer', reducer);
const StoreProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const actions = useActions(state, dispatch);
useEffect(() => console.log({ newState: state }), [state]);
return (
<StoreContext.Provider value={{ state, dispatch, actions }}>
{ children }
</StoreContext.Provider>
);
};
export { StoreContext, StoreProvider };
import { types } from "./reducers";
const useActions = (state, dispatch) => {
function eventIndex(query) {
dispatch({ type: types.EVENT_INDEX, payload: ['yo', 'trop cool'] });
}
return {
eventIndex
};
};
export default useActions;
\ No newline at end of file
const initialState = {
events: [],
};
const types = {
EVENT_INDEX: 'EVENT_INDEX',
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case types.EVENT_INDEX:
return {
...state,
events: action.payload
};
default:
throw new Error(`Unknown action ${action.type}`);
}
};
export { initialState, types, reducer };
import Config from './config';
import withEventData from './containers/withEventData';
import withEventsData from './containers/withEventsData';
import withReduxStore from './redux/withReduxStore';
import { StoreContext, StoreProvider } from './context/StoreContext';
export { Config, withReduxStore, withEventData, withEventsData };
export { Config, StoreContext, StoreProvider };
{}
{
"d03dbf70-f923-11e9-ba0b-5f33822bf7d8": {
"token_type": "Bearer",
"expires_in": 3599,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjMzNmVkOWIwMjUwY2NmNjYxMDc3ZWY3YzVjOWYzOTYzNjY5N2JkODdmYWI5NmM2YWUyMTA5MjBhNjc4OWQwMjc1ZWQ5NjZiNzc4NmFlMjExIn0.eyJhdWQiOiJUYlBqenN0Wkp0QVFDWjhqIiwianRpIjoiMzM2ZWQ5YjAyNTBjY2Y2NjEwNzdlZjdjNWM5ZjM5NjM2Njk3YmQ4N2ZhYjk2YzZhZTIxMDkyMGE2Nzg5ZDAyNzVlZDk2NmI3Nzg2YWUyMTEiLCJpYXQiOjE1NzIyMjY3ODcsIm5iZiI6MTU3MjIyNjc4NywiZXhwIjoxNTcyMjMwMzg2LCJzdWIiOiIiLCJzY29wZXMiOltdfQ.oM0o1Fhpofp6lY8tGK9ilnwcpUZRf6w2abbVSe0uM2l3UWFjZwUYaE-5s5D9lF_LHYiNcMCWXGCZb1EpXD0mmuBc8utod1_PeEI-xx5-5bm_zEpyUL0cXJUi5glOHYDIjvLWizrlIWhZy6N-YWOmJZEXcaPAQm8zaTZcaQDbirhUP2BXTqc1zHfvjLhNx6KUGJxDA-dePQR01-oANvkrmmN0d672lQ5YIhELj6XUEqgaROrt-Ov71GR7ZTGddzTADYTcDM9XfqIuu1sWXNNOyBpiAffpa6yaf4L1g5gm8FqDOKRrFuZ_5ncwGuqcraMe--ar5KxmCqOmeWWkC8ne7fRmAQfMPrEJyxJehmauzyz8TeewNMkmKp5dyKXKj-iu__lTqZ2U9-8Vo0KNWJiLJ_WTPci54gS-ei4dVpJsvz4O4X7bd1IfJHNTkUf4abx5o0vvc94IHN7m5BpnODlC60zryGJdsRi-Swyjc-8pU6WR74lplj9klAs1aHQPDuT47wdaUd7MKYE2SC5fnOXg44g7E8Rv3gHlz3nexqpqC5MEVGKUK2ypzBw-TYBntawkR_7ousVZ7h0mAJZIwpDyEkMHxyyH21OCk7Wv4lNaItB1sIiAiBilGRFV2YTCC-9NGnSyOnEITUJknsPDEgB48drrTDZ8JYxE69sh560RioI",
"expired_at": "2019-10-28T02:39:41.439Z",
"id": "d03dbf70-f923-11e9-ba0b-5f33822bf7d8"
}
}
\ No newline at end of file
import { useState } from 'react';
import { SearchBar } from 'caligram-react';
export default function Filters({ searchItems, autocomplete }) {
const [value, setValue] = useState();
const searchBarProps = {
onClear: () => {},
onFetch: () => {},
onSelect: () => {},
onSubmit: () => {},
onChange: (ev, { newValue }) => { setValue(newValue) },
query: value,
renderSectionTitle: () => {},
renderSuggestion: () => {},
suggestions: [],
};
return (
<aside>
<SearchBar {...searchBarProps} />
</aside>
);
}
import Filters from './Filters';
import Footer from './Footer';
import Header from './Header';
import Menu from './Menu';
export { Footer, Header, Menu };
\ No newline at end of file
export { Filters, Footer, Header, Menu };
\ No newline at end of file
......@@ -13,7 +13,6 @@
"@babel/runtime-corejs2": "^7.4.3",
"@zeit/next-sass": "^1.0.1",
"bluebird": "^3.5.4",
"caligram-base": "git+https://[email protected]/territoires/calendriers/caligram-base.git#next",
"caligram-react": "^0.9.0",
"dotenv": "^7.0.0",
"express": "^4.16.4",
......
import App, { Container } from 'next/app';
import React from 'react';
import { Provider } from 'react-redux';
import { Config, withReduxStore } from 'caligram-base';
import { Config, StoreProvider } from 'caligram-base';
import { Footer, Header } from '../components';
import config from '../config';
import '../scss/styles.scss';
......@@ -10,17 +9,17 @@ Config.set(config);
class Caligram extends App {
render() {
const { Component, pageProps, reduxStore } = this.props;
const { Component, pageProps } = this.props;
return (
<Container>
<Provider store={reduxStore}>
<StoreProvider>
<Container>
<Header/>
<Component {...pageProps} />
<Footer/>
</Provider>
</Container>
</Container>
</StoreProvider>
)
}
}
export default withReduxStore(Caligram);
export default Caligram;
import moment from 'moment';
import { connect } from 'react-redux';
import { Config, withEventsData } from 'caligram-base';
import { Grid, EventList } from 'caligram-react';
import { useContext } from 'react';
import { Config, StoreContext } from 'caligram-base';
import { Col, Grid, EventList } from 'caligram-react';
function Index({ showcases, events }) {
function Index(props) {
const appConfig = Config.get();
const dateFormat = ev => moment(ev.start_date).format('Do MMMM');
const featuredEvents = showcases[appConfig.showcases.featured];
const { state, dispatch, actions } = useContext(StoreContext);
console.log('state from app!!', state);
return (
<div className="Home">
{ !!appConfig.showcases && appConfig.showcases.featured && featuredEvents && featuredEvents.length &&
<Grid>
<EventList layout="carousel" groupBy="none" dateFormat={dateFormat} title="En vedette" events={featuredEvents} />
</Grid>
}
<Grid>
<EventList dateFormat={dateFormat} events={events} />
</Grid>
Home
</div>
);
}
const mapStateToProps = state => ({
showcases: state.showcase,
events: state.events.data,
});
export default connect(mapStateToProps)(withEventsData(Index));
export default Index;
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