|
1 | | -import React, { Component } from 'react'; |
2 | | -import ExplorerView from './ExplorerView'; |
3 | | -import Editor from './Editor'; |
| 1 | +import React, { Component, Fragment } from 'react'; |
| 2 | +import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; |
| 3 | +import AdminView from './AdminView'; |
| 4 | +import StudentView from './StudentView'; |
4 | 5 | import Modals from './Modals'; |
5 | | -import request from './request'; |
6 | 6 |
|
7 | 7 | class App extends Component { |
8 | | - constructor(props) { |
9 | | - super(props); |
10 | | - this.state = { |
11 | | - tags: [], |
12 | | - files: [], |
13 | | - folder: { |
14 | | - path: [], |
15 | | - active: {} |
16 | | - }, |
17 | | - editor: { |
18 | | - visible: false, |
19 | | - questions: [], |
20 | | - model: { |
21 | | - questions: [] |
22 | | - } |
23 | | - } |
24 | | - }; |
25 | | - |
26 | | - this.requestFolder = this.requestFolder.bind(this); |
27 | | - this.searchByTags = this.searchByTags.bind(this); |
28 | | - this.create = this.create.bind(this); |
29 | | - this.edit = this.edit.bind(this); |
30 | | - this.save = this.save.bind(this); |
31 | | - this.closeEditor = this.closeEditor.bind(this); |
32 | | - this.updateEditor = this.updateEditor.bind(this); |
33 | | - this.refreshEditor = this.refreshEditor.bind(this); |
34 | | - this.refresh = this.refresh.bind(this); |
35 | | - } |
36 | | - |
37 | | - requestFolder(_id) { |
38 | | - this.setState({ tags: [] }); |
39 | | - request('ListFolder', { _id }).then(res => res.json()).then(({folder, files}) => { |
40 | | - this.setState({ folder, files }); |
41 | | - }); |
42 | | - } |
43 | | - |
44 | | - searchByTags(tags) { |
45 | | - if (tags.length) { |
46 | | - this.setState({ tags }); |
47 | | - request('GetQuestionsByTags', { tags, idParent: this.state.folder.active._id }).then(res => res.json()).then(files => { |
48 | | - this.setState({ files }); |
49 | | - }); |
50 | | - } else { |
51 | | - this.refresh(); |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - create() { |
56 | | - this.setState({ |
57 | | - editor: { |
58 | | - visible: true, |
59 | | - questions: [], |
60 | | - model: { |
61 | | - questions: [] |
62 | | - } |
63 | | - } |
64 | | - }); |
65 | | - } |
66 | | - |
67 | | - edit(model) { |
68 | | - request('GetQuestionsByIds', { _ids: model.questions.map(quest => quest.idQuestion) }).then(res => res.json()).then(questions => { |
69 | | - this.setState({ |
70 | | - editor: { |
71 | | - visible: true, |
72 | | - model, |
73 | | - questions |
74 | | - } |
75 | | - }); |
76 | | - }); |
77 | | - } |
78 | | - |
79 | | - updateEditor(editor) { |
80 | | - this.setState({ editor }); |
81 | | - } |
82 | | - |
83 | | - save(name) { |
84 | | - const { editor, folder } = this.state; |
85 | | - const idParent = editor.model.idParent || folder.active._id; |
86 | | - |
87 | | - request('SaveMultipleChoice', { ...editor.model, idParent, name, questions: editor.questions.map(quest => { |
88 | | - return { idQuestion: quest._id }; |
89 | | - }) }).then(this.closeEditor).then(this.refresh); |
90 | | - } |
91 | | - |
92 | | - closeEditor() { |
93 | | - this.setState((state) => { |
94 | | - return { editor: { ...state.editor, visible: false } }; |
95 | | - }); |
96 | | - } |
97 | | - |
98 | | - refreshEditor() { |
99 | | - const { editor } = this.state; |
100 | | - request('GetQuestionsByIds', { _ids: editor.questions.map(quest => quest._id) }).then(res => res.json()).then(questions => { |
101 | | - this.setState(state => { |
102 | | - return { |
103 | | - editor: { |
104 | | - ...state.editor, |
105 | | - questions |
106 | | - } |
107 | | - }; |
108 | | - }); |
109 | | - }); |
110 | | - } |
111 | | - |
112 | | - refresh() { |
113 | | - const { editor, folder } = this.state; |
114 | | - if (editor.visible) { |
115 | | - this.refreshEditor(); |
116 | | - } |
117 | | - this.requestFolder(folder.active._id); |
118 | | - } |
119 | | - |
120 | 8 | render() { |
121 | | - const { folder, files, tags, editor } = this.state; |
122 | 9 | return ( |
123 | | - <div id="app"> |
124 | | - <ExplorerView editing={editor.visible} folder={folder} files={files} tags={tags} |
125 | | - requestFolder={this.requestFolder} searchByTags={this.searchByTags} |
126 | | - create={this.create} edit={this.edit} refresh={this.refresh}/> |
127 | | - |
128 | | - <Editor editor={editor} folder={folder} update={this.updateEditor} save={this.save} closeEditor={this.closeEditor}/> |
| 10 | + <Fragment> |
| 11 | + <Router> |
| 12 | + <Switch> |
| 13 | + <Route path="/qcm/:url" component={StudentView}/> |
| 14 | + <Route path="/" component={AdminView}/> |
| 15 | + </Switch> |
| 16 | + </Router> |
129 | 17 | {Modals.get()} |
130 | | - </div> |
| 18 | + </Fragment> |
131 | 19 | ); |
132 | 20 | } |
133 | 21 | } |
|
0 commit comments