0.3.0 • Published 4 years ago

vuetify-avatar-uploader v0.3.0

Weekly downloads
116
License
MIT
Repository
github
Last release
4 years ago

vuetify-avatar-uploader

:koala: v-avatar + file uploads

Wraps Vuetify's v-avatar component with file upload magic.

  1. User clicks on their avatar
  2. User selects an image file to use as their new avatar
  3. File is uploaded to your API
  4. User's avatar is replaced :sparkles:

Installation

npm i vuetify-avatar-uploader

Module

import VAvatarUploader from 'vuetify-avatar-uploader'

export default {
  components: {
    VAvatarUploader
  }
}

Browser

<script type="text/javascript" src="node_modules/vuejs/dist/vue.min.js"></script>
<script type="text/javascript" src="node_modules/vuetify/dist/vuetify.min.js"></script>
<script type="text/javascript" src="node_modules/vuetify-avatar-uploader/dist/vuetify-avatar-uploader.min.js"></script>
<script type="text/javascript">
  Vue.use(VAvatarUploader);
</script>

Usage

At a minimum, you must provide :url and :request properties:

<template>
  <v-avatar-uploader
    :url="url"
    :request="request"
  />
</template>

<script>
import VAvatarUploader from 'vuetify-avatar-uploader'

export default {
  computed: {
    // Determine the URL to show, typically from an object representing a user
    url () {
      return 'https://randomuser.me/api/portraits/men/1.jpg'
    }
  },

  methods: {
    // Responsible for performing the upload request to your API
    request (form, config) {
      return this.$http.post('/v1/avatars', form, config)
    }
  },

  components: {
    VAvatarUploader
  }
}
</script>

Example

The following is a real-world example of how the library can be used to support user avatar uploads:

<template>
  <v-avatar-uploader
    :url="url"
    :request="request"
    :clickable="clickable"
    :rename="rename"
    @success="success"
    @failed="failed"
  />
</template>

<script>
import VAvatarUploader from 'vuetify-avatar-uploader'
import events from '@/util/events'

import uuid from 'uuid/v4'
import get from 'dlv'
import { mapState } from 'vuex'

export default {
  name: 'user-avatar-uploader',

  props: {
    user: {
      type: Object,
      required: true
    }
  },

  computed: {
    ...mapState('api/user', ['session']),

    url () {
      return get(this.user, 'avatar.thumb')
    },

    clickable () {
      return this.user.id === this.session.id
    }
  },

  methods: {
    request (form, config) {
      return this.$http.post('/v1/avatars', form, config)
    },

    rename (file) {
      const ext = file.name.split('.')[1]
      const name = `${uuid()}.${ext}`

      return name
    },

    success ({ data }) {
      // Update user avatar with the latest
      Object.assign(this.user.avatar, data)

      // Request the latest user object from the API to update all other references in the app
      this.$store.dispatch('api/user/get')
    },

    failed (error) {
      events.$emit('notify', { text: 'Failed to upload avatar', kind: 'error', icon: 'warning' })
    }
  },

  components: {
    VAvatarUploader
  }
}
</script>

API

Props

NameDescriptionTypeRequiredDefault
urlURL of the avatarStringYes
requestPerforms the file uploadFunctionYes
renameRenames the file before uploadFunctionNofile => file.name
fieldFormData field name to use for file dataStringNo'file'
clickableDetermines if the user can click to uploadBooleanNotrue
maxSizeMaximum file upload size (in KiB)NumberNo2048
headersOptional HTTP headers to send with upload requestObjectNo{}
avatarCore v-avatar configuration objectObjectNo{}

Events

NameDescription
successFile upload succeeded
progressFile upload progress (axios only)
cancelFile upload interrupted (i.e. user clicked on avatar while uploading)
replaceUser clicks on a pre-existing avatar (overrides default file selection flow!)
failedFile upload failed
error-typeFile upload MIME type is unsupported
error-sizeFile upload exceeds maximum size
error-emptyFile upload is empty

Slots

NameDescription
noneDisplayed when the url prop is falsy
loadingDisplayed when an avatar is being uploaded

Future

  • Document more real-world examples
  • Provide configuration options for v-progress-bar
  • Support upload cancellations
  • Allow custom supported MIME types
  • Allow custom form property name for file uploads
  • Emit event on empty files

License

MIT

0.3.0

4 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago