Skip to content

Instantly share code, notes, and snippets.

@carsonfarmer
Last active April 3, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carsonfarmer/083f85c97dd4324d5b5b13bb923944f8 to your computer and use it in GitHub Desktop.
Save carsonfarmer/083f85c97dd4324d5b5b13bb923944f8 to your computer and use it in GitHub Desktop.
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