Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Store Diff
diff --git a/src/Store.js b/src/Store.js
--- a/src/Store.js
+++ b/src/Store.js
@@ -1,7 +1,10 @@
-import { observe, action, observable } from 'mobx'
+import { observe, action, observable, runInAction } from 'mobx'
+import { utc } from 'moment'
import { Textile } from '@textile/js-http-client'
import { toast } from 'react-semantic-toasts'
+const THREAD_KEY = 'avatarThreadKey'
+
const textile = new Textile({
url: 'http://127.0.0.1',
port: 40602
@@ -40,6 +43,48 @@ class Store {
})
})
}
+ @action async setProfile(userString, avatarFile) {
+ if (userString) {
+ textile.profile.setUsername(userString).then(() => {
+ runInAction('setUsername', () => {
+ this.profile.name = userString
+ this.profile.updated = utc().format()
+ })
+ })
+ }
+ if (avatarFile) {
+ let avatarThread
+ const threads = await textile.threads.list()
+ for (const thread of threads.items) {
+ if (thread.key === THREAD_KEY) {
+ avatarThread = thread
+ break
+ }
+ }
+ if (!avatarThread) {
+ const schemas = await textile.schemas.defaults()
+ const avatarSchema = schemas.avatar
+ const file = await textile.schemas.add(avatarSchema)
+ avatarThread = await textile.threads.add(
+ 'avatars',
+ file.hash,
+ THREAD_KEY,
+ 'public',
+ 'notshared'
+ )
+ }
+ const addedFile = await textile.files.addFile(avatarFile, 'avatar', avatarThread.id)
+ await textile.profile.setAvatar(addedFile.files[0].links.large.hash)
+ runInAction('setAvatar', () => {
+ this.profile.avatar = addedFile.target
+ this.profile.updated = utc().format()
+ })
+ }
+ toast({
+ title: 'Profile updated',
+ description: 'Your profile has been updated!'
+ })
+ }
}
export default Store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.