-
Notifications
You must be signed in to change notification settings - Fork 0
/
docsSlice_updt_metrics.ts
159 lines (136 loc) · 4.28 KB
/
docsSlice_updt_metrics.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
AnyAction,
createAsyncThunk,
createSlice,
Dispatch,
} from '@reduxjs/toolkit';
import { RootState } from '../../app/store';
import { StreamID } from '@ceramicnetwork/streamid';
import { CeramicClient } from '@ceramicnetwork/http-client';
import { TileDocument } from '@ceramicnetwork/stream-tile';
import { EthereumAuthProvider, ThreeIdConnect } from '@3id/connect';
import ThreeIdResolver from '@ceramicnetwork/3id-did-resolver';
import { DID } from 'dids';
type AsyncThunkConfig = {
state: RootState;
dispatch?: Dispatch<AnyAction>;
extra?: unknown;
rejectValue?: unknown;
};
// Make API call
// Make sure it's in TileDoc format (JSON)
// Assign variable names to writeStream
export const writeDocument = createAsyncThunk<
{ doc: CeramicDoc; ceramic: CeramicClient },
void,
AsyncThunkConfig
>('docs/writeDocument', async (action, thunkAPI) => {
try {
let ceramic = thunkAPI.getState().docs.ceramic;
if (!ceramic) {
const { provider, address } = thunkAPI.getState().wallet;
const authProvider = new EthereumAuthProvider(provider, address);
const threeIdConnect = new ThreeIdConnect();
console.log('connecting to 3id');
await threeIdConnect.connect(authProvider);
const DEFAULT_CERAMIC_HOST = 'https://ceramic-clay.3boxlabs.com';
ceramic = new CeramicClient(DEFAULT_CERAMIC_HOST);
const resolver = {
...ThreeIdResolver.getResolver(ceramic),
};
const did = new DID({ resolver });
ceramic.setDID(did);
const didProvider = await threeIdConnect.getDidProvider();
console.log('ceramic.did:', ceramic.did);
if (ceramic.did !== undefined && ceramic.did !== null) {
await ceramic.did.setProvider(didProvider);
await ceramic.did.authenticate();
}
}
if (
ceramic !== undefined &&
ceramic !== null &&
ceramic.did !== undefined &&
ceramic.did !== null
) {
const writeStream = await TileDocument.create(
ceramic,
{
date: new Date(),
version: 1, // Schema version recommended
latitude: -87.6298,
longitude: 41.8781,
name: 'WEATHER STN #1',
stationID: 'IWILLE44', // Not sure if relevant.
prcpRate: 1.2, // millimeters
dewpt: 22, // Dewpoint
windSpd: 15.6, // km/h
windDir: 93, // arc-degrees
airPres: 1013.41, // hPa
relHum: 0.60, // percentage
solRad: 654, // Solar Radiation in Watt/m2
tMax: 29, // Highest temp in 24 hrs
tMin: 23, // Lowest temp in 24 hrs
tActual: 29, // Temperature at time of record
},
{
controllers: [ceramic.did.id],
}
);
const readStream = await ceramic.loadStream<TileDocument>(writeStream.id);
console.log('read TileDocument:', readStream);
const doc: CeramicDoc = {
docID: readStream.id,
docContent: readStream.content,
};
return {
doc: doc,
ceramic: ceramic,
};
} else {
throw new Error('Ceramic or did provider not initialized');
}
} catch (error) {
console.log('Error writing document', error);
throw error;
}
});
export interface CeramicDoc {
docID: StreamID;
docContent: Record<string, string>;
}
export interface docsState {
ceramic: CeramicClient | null;
isBusy: boolean;
docs: CeramicDoc[];
}
const initialState: docsState = {
ceramic: null,
isBusy: false,
docs: [],
};
export const docsSlice = createSlice({
name: 'docs',
initialState,
reducers: {
addDoc: (state, action) => {
state.docs.push(action.payload);
},
},
extraReducers: (builder) => {
builder.addCase(writeDocument.pending, (state) => {
state.isBusy = true;
});
builder.addCase(writeDocument.rejected, (state) => {
state.isBusy = false;
});
builder.addCase(writeDocument.fulfilled, (state, { payload }) => {
state.isBusy = false;
state.docs.push(payload.doc);
state.ceramic = payload.ceramic;
});
},
});
export const { addDoc } = docsSlice.actions;
export const selectDocs = (state: RootState) => state.docs;
export default docsSlice.reducer;