mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
we have persistence!
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ node_modules
|
|||||||
dist
|
dist
|
||||||
lib
|
lib
|
||||||
*.log
|
*.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
tmp.json
|
||||||
@@ -116,6 +116,12 @@
|
|||||||
Redux 4: Combine Reducers
|
Redux 4: Combine Reducers
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="Tile">
|
||||||
|
<a target="_blank" href="/step2-09/" class="Tile-link">
|
||||||
|
Step 9<br />
|
||||||
|
Redux 5: Service Calls
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="Container">
|
<div class="Container">
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
|
const fs = require('fs');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
@@ -6,6 +7,20 @@ const app = express();
|
|||||||
|
|
||||||
const store = {
|
const store = {
|
||||||
/** @type {any} */
|
/** @type {any} */
|
||||||
|
read() {
|
||||||
|
if (fs.existsSync('tmp.json')) {
|
||||||
|
store.todos = JSON.parse(fs.readFileSync('tmp.json').toString());
|
||||||
|
} else {
|
||||||
|
store.todos = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return store.todos;
|
||||||
|
},
|
||||||
|
|
||||||
|
save() {
|
||||||
|
fs.writeFileSync('tmp.json', JSON.stringify(store.todos));
|
||||||
|
},
|
||||||
|
|
||||||
todos: {}
|
todos: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -13,24 +28,28 @@ app.use(bodyParser.json());
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
app.get('/todos', (req, res) => {
|
app.get('/todos', (req, res) => {
|
||||||
res.json(store.todos);
|
res.json(store.read());
|
||||||
});
|
});
|
||||||
|
|
||||||
app.put('/todos/:id', (req, res) => {
|
app.put('/todos/:id', (req, res) => {
|
||||||
store.todos[req.params.id] = req.body;
|
store.todos[req.params.id] = req.body;
|
||||||
|
store.save();
|
||||||
res.json('ok');
|
res.json('ok');
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/todos/:id', (req, res) => {
|
app.post('/todos/:id', (req, res) => {
|
||||||
store.todos[req.params.id] = req.body;
|
store.todos[req.params.id] = req.body;
|
||||||
|
store.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete('/todos/:id', (req, res) => {
|
app.delete('/todos/:id', (req, res) => {
|
||||||
delete store.todos[req.params.id];
|
delete store.todos[req.params.id];
|
||||||
|
store.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/todos', req => {
|
app.post('/todos', req => {
|
||||||
store.todos = req.body;
|
store.todos = req.body;
|
||||||
|
store.save();
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/hello', (req, res) => {
|
app.get('/hello', (req, res) => {
|
||||||
|
|||||||
@@ -1,30 +1,35 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { reducer } from './reducers';
|
import { reducer } from './reducers';
|
||||||
import { createStore, compose } from 'redux';
|
import { applyMiddleware, createStore, compose } from 'redux';
|
||||||
|
import thunk from 'redux-thunk';
|
||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
import { TodoApp } from './components/TodoApp';
|
import { TodoApp } from './components/TodoApp';
|
||||||
import { actions } from './actions';
|
|
||||||
import { initializeIcons } from '@uifabric/icons';
|
import { initializeIcons } from '@uifabric/icons';
|
||||||
import { Store } from './store';
|
import { Store, FilterTypes } from './store';
|
||||||
|
import * as service from './service';
|
||||||
|
|
||||||
/* Goop for making the Redux dev tool to work */
|
/* Goop for making the Redux dev tool to work */
|
||||||
declare var window: any;
|
declare var window: any;
|
||||||
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
||||||
function createStoreWithDevTool(reducer, initialStore?: Store) {
|
function createStoreWithDevTool(reducer, initialStore?: Store) {
|
||||||
return createStore(reducer, initialStore, composeEnhancers());
|
return createStore(reducer, initialStore, composeEnhancers(applyMiddleware(thunk)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = createStoreWithDevTool(reducer);
|
(async () => {
|
||||||
|
const preloadStore = {
|
||||||
|
todos: await service.getAll(),
|
||||||
|
filter: 'all' as FilterTypes
|
||||||
|
};
|
||||||
|
|
||||||
store.dispatch(actions.addTodo('hello'));
|
const store = createStoreWithDevTool(reducer, preloadStore);
|
||||||
store.dispatch(actions.addTodo('world'));
|
|
||||||
|
|
||||||
initializeIcons();
|
initializeIcons();
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<TodoApp />
|
<TodoApp />
|
||||||
</Provider>,
|
</Provider>,
|
||||||
document.getElementById('app')
|
document.getElementById('app')
|
||||||
);
|
);
|
||||||
|
})();
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ module.exports = function() {
|
|||||||
devServer: {
|
devServer: {
|
||||||
contentBase: path.resolve(__dirname),
|
contentBase: path.resolve(__dirname),
|
||||||
watchContentBase: true,
|
watchContentBase: true,
|
||||||
|
watchOptions: {
|
||||||
|
ignored: /tmp.json/
|
||||||
|
},
|
||||||
hot: false,
|
hot: false,
|
||||||
stats: 'errors-only',
|
stats: 'errors-only',
|
||||||
overlay: true,
|
overlay: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user