-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.tsx
79 lines (71 loc) · 1.5 KB
/
context.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* _____ __
* / ___// /_ _____
* \__ \/ / / / / _ \
* ___/ / / /_/ / __/
* /____/_/\__, /\___/
* /____/
* Copyright 2018 Parsa Ghadimi. All Rights Reserved.
* Licence: MIT License
*/
import React from "react";
import { db } from "./fs";
import * as types from "./types";
const { Provider, Consumer } = React.createContext<types.Context>(undefined);
export { Consumer };
interface State {
values: types.Values;
}
export class SlyeProvider extends React.Component<{}, State> {
state = {
values: {
Auth: {
isLoggedIn: false
},
Presentations: {}
}
};
actions: types.Actions;
constructor(props) {
super(props);
this.actions = {
Auth: {
login() {
db.login();
},
logout() {
db.logout();
}
},
Presentations: {
create() {
// TODO(qti3e) async create(): Promise<id>;
db.create().then(id => {
location.hash = "#/editor/" + id;
});
}
}
};
}
componentWillMount() {
db.onAuthStateChanged(user => {
this.setState({
values: {
...this.state.values,
Auth: {
...this.state.values.Auth,
isLoggedIn: Boolean(user),
currentUser: user
}
}
});
});
}
render() {
return (
<Provider value={{ actions: this.actions, values: this.state.values }}>
{this.props.children}
</Provider>
);
}
}