Skip to content

Commit 7aac3d5

Browse files
fabienjuifguillaumecrespel
authored andcommitted
✨ pass context from angularjs to react
1 parent 02a219b commit 7aac3d5

File tree

11 files changed

+76
-97
lines changed

11 files changed

+76
-97
lines changed

angularjs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<script type="text/ng-template" id="todomvc-index.html">
1414
<section id="todoapp">
1515
<header id="header">
16-
<h1>todos</h1>
16+
<h1>AngularJS</h1>
1717
<form id="todo-form" ng-submit="addTodo()">
1818
<input id="new-todo" placeholder="What needs to be done?" ng-model="newTodo" ng-disabled="saving" autofocus>
1919
</form>

angularjs/js/controllers/todoCtrl.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,58 @@ angular.module('todomvc')
4747
$scope.saving = false;
4848

4949
// go to the react application
50+
// - save to localStorage
51+
const time = Date.now()
52+
const angularData = JSON.parse(window.localStorage.getItem('todos-angularjs'))
53+
54+
const array = []
55+
const keys = []
56+
const data = {}
57+
58+
const completedArray = []
59+
const completedKeys = []
60+
const completedData = {}
61+
62+
angularData.forEach((todo, index) => {
63+
const id = index
64+
65+
// todo
66+
const mappedTodo = { id, todo: todo.title }
67+
keys.push(id)
68+
array.push(mappedTodo)
69+
data[id] = mappedTodo
70+
71+
// completed
72+
if (todo.completed) {
73+
const completed = { id, completed: true }
74+
75+
completedArray.push(completed)
76+
completedKeys.push(id)
77+
completedData[id] = completed
78+
}
79+
})
80+
81+
const initState = {
82+
data: {
83+
todos: {
84+
keys,
85+
data,
86+
array,
87+
},
88+
},
89+
ui: {
90+
completed: {
91+
keys: completedKeys,
92+
data: completedData,
93+
array: completedArray,
94+
},
95+
},
96+
}
97+
98+
window.localStorage.setItem('redux-store', JSON.stringify(initState))
99+
console.log('Temps de creation du store redux pour react', `${Date.now() - time} ms`)
100+
101+
// - go to react
50102
window.history.pushState({}, 'react', '/react')
51103
});
52104
};

angularjs/js/services/todoStorage.js

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,78 +13,7 @@ angular.module('todomvc')
1313

1414
// Detect if an API backend is present. If so, return the API module, else
1515
// hand off the localStorage adapter
16-
return $http.get('/api')
17-
.then(function () {
18-
return $injector.get('api');
19-
}, function () {
20-
return $injector.get('localStorage');
21-
});
22-
})
23-
24-
.factory('api', function ($resource) {
25-
'use strict';
26-
27-
var store = {
28-
todos: [],
29-
30-
api: $resource('/api/todos/:id', null,
31-
{
32-
update: { method:'PUT' }
33-
}
34-
),
35-
36-
clearCompleted: function () {
37-
var originalTodos = store.todos.slice(0);
38-
39-
var incompleteTodos = store.todos.filter(function (todo) {
40-
return !todo.completed;
41-
});
42-
43-
angular.copy(incompleteTodos, store.todos);
44-
45-
return store.api.delete(function () {
46-
}, function error() {
47-
angular.copy(originalTodos, store.todos);
48-
});
49-
},
50-
51-
delete: function (todo) {
52-
var originalTodos = store.todos.slice(0);
53-
54-
store.todos.splice(store.todos.indexOf(todo), 1);
55-
return store.api.delete({ id: todo.id },
56-
function () {
57-
}, function error() {
58-
angular.copy(originalTodos, store.todos);
59-
});
60-
},
61-
62-
get: function () {
63-
return store.api.query(function (resp) {
64-
angular.copy(resp, store.todos);
65-
});
66-
},
67-
68-
insert: function (todo) {
69-
var originalTodos = store.todos.slice(0);
70-
71-
return store.api.save(todo,
72-
function success(resp) {
73-
todo.id = resp.id;
74-
store.todos.push(todo);
75-
}, function error() {
76-
angular.copy(originalTodos, store.todos);
77-
})
78-
.$promise;
79-
},
80-
81-
put: function (todo) {
82-
return store.api.update({ id: todo.id }, todo)
83-
.$promise;
84-
}
85-
};
86-
87-
return store;
16+
return $injector.get('localStorage');
8817
})
8918

9019
.factory('localStorage', function ($q) {

react/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
<body>
1111
<div id="app" />
12-
<script type="text/javascript" src="/react/todomvc.js?75dcb5edd3762df4acd2"></script></body>
12+
<script type="text/javascript" src="/react/todomvc.js?6920eac13c181e8a2897"></script></body>
1313

1414
</html>

react/src/components/header/header.container.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const mapStateToProps = (state) => {
1111

1212
export const mapDispatchToProps = (dispatch) => {
1313
return {
14-
addTodo: e => dispatch(action('NEW_TODO_KEYDOWN_PRESSED', { todo: e.target.value, keyCode: e.keyCode })),
15-
onNewTodoChange: e => dispatch(action('NEW_TODO_CHANGED', e.target.value)),
14+
onFocus: () => dispatch(action('NEW_TODO_FOCUSED')),
1615
}
1716
}
1817

react/src/components/header/header.jsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import { onlyUpdateForPropTypes } from 'recompose'
44

5-
const Header = ({ newTodo, addTodo, onNewTodoChange }) => {
5+
const Header = ({ newTodo, onFocus }) => {
66
return (
77
<header className="header">
8-
<h1>todos</h1>
8+
<h1>React</h1>
99
<input
1010
className="new-todo"
1111
name="newTodo"
1212
placeholder="What needs to be done?"
13-
autoFocus
1413
value={newTodo}
15-
onKeyDown={addTodo}
16-
onChange={onNewTodoChange}
14+
onFocus={onFocus}
1715
/>
1816
</header>
1917
)
2018
}
2119

2220
Header.propTypes = {
2321
newTodo: PropTypes.string,
24-
onNewTodoChange: PropTypes.func.isRequired,
25-
addTodo: PropTypes.func.isRequired,
22+
onFocus: PropTypes.func.isRequired,
2623
}
2724

2825
Header.defaultProps = {

react/src/redux/data/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { combineReducers } from 'redux'
22
import todos from './todos'
33

4+
// const defaultTodosState = JSON.parse(window.localStorage.getItem('redux-store') || { data: { } }).data.todos
5+
46
/* Combine on data reducer */
57
export default combineReducers({
68
todos,
9+
// todos: (state = defaultTodosState, action) => todos(state, action),
710
})

react/src/redux/store.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import reducers from './reducers'
77

88
const sagaMiddleware = createSagaMiddleware()
99

10+
const time = Date.now()
11+
const initState = JSON.parse(window.localStorage.getItem('redux-store') || {})
12+
1013
const store = createStore(
1114
reducers,
15+
initState,
1216
compose(
1317
enhancer,
1418
applyMiddleware(sagaMiddleware, middleware),
@@ -17,6 +21,8 @@ const store = createStore(
1721
),
1822
)
1923

24+
console.log('Temps init store', `${Date.now() - time} ms`)
25+
2026
sagaMiddleware.run(sagas(store))
2127

2228
const initialLocation = store.getState().router

react/src/sagas/intents.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ const checkEscape = action => check(action, ESCAPE_KEY)
1010

1111
export default function* () {
1212
yield takeLatest('REMOVE_TODO_CLICKED', action => todos.remove(action.payload))
13-
yield takeLatest('NEW_TODO_CHANGED', action => ui.setNewTodo(action.payload))
13+
yield takeLatest('NEW_TODO_FOCUSED', todos.goToAngular)
1414
yield takeLatest('COMPLETE_TODO_CLICKED', action => todos.complete(action.payload))
1515
yield takeLatest('COMPLETE_ALL_CHANGED', action => todos.completeAll(action.payload))
1616
yield takeLatest('TODO_DOUBLE_CLICKED', action => ui.edit(action.payload))
1717
yield takeLatest('TODO_BLURED', action => todos.update(action.payload))
1818
yield takeLatest('TODO_CHANGED', action => ui.setEditTodo(action.payload))
1919
yield takeLatest('CLEAR_COMPLETED_CLICKED', todos.clearCompleted)
20-
yield takeLatest('NEW_TODO_KEYDOWN_PRESSED', (action) => {
21-
if (checkEnter(action)) return todos.add(action.payload.todo)
22-
return null
23-
})
2420
yield takeLatest('TODO_KEYDOWN_PRESSED', (action) => {
2521
if (checkEnter(action)) return todos.update(action.payload.id)
2622
if (checkEscape(action)) return ui.cancelEdit(action.payload.id)

react/src/sagas/todos/todos.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { call, put, select } from 'redux-saga/effects'
22
import store from 'redux/data/todos'
3-
import newTodo from 'redux/ui/newTodo'
43
import completed from 'redux/ui/completed'
54
import editing from 'redux/ui/editing'
65

7-
export function* add(todo) {
8-
if (!todo || !todo.trim()) return
9-
yield put(store.add(todo))
10-
yield put(newTodo.reset())
6+
export function goToAngular() {
7+
window.location = '/angularjs'
118
}
129

1310
export function* remove(todoId) {

react/todomvc.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)