Skip to content

Commit 9bf2301

Browse files
authoredAug 6, 2023
Avatar upload input
1 parent 1e827c9 commit 9bf2301

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed
 

‎input/AvatarInput.vue

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
<script setup>
22
import axios from 'axios'
33
import Input from '@/components/input/Input.vue'
4-
import avatar from './profil/avatar.png'
5-
import { toRefs } from 'vue'
4+
import avatar_default from './profil/avatar.png'
5+
import { toRefs, ref } from 'vue'
66
77
const props = defineProps({
8-
image: { default: avatar },
8+
avatar: { default: avatar_default },
99
label: { default: 'Select image' },
10-
remove_avatar_url: { default: 'web/api/remove/avatar' },
10+
remove_avatar_url: { default: '/web/api/remove/avatar' },
1111
remove_success: { default: 'Avatar removed.' },
1212
remove_error: { default: 'Avatar not removed.' },
1313
})
1414
15-
const { image } = toRefs(props)
15+
const avatar_path = ref(null)
16+
17+
if (props.avatar) {
18+
avatar_path.value = '/storage/' + props.avatar
19+
20+
if (props.avatar.toLowerCase().startsWith('http://')) {
21+
avatar_path.value = props.avatar
22+
}
23+
24+
if (props.avatar.toLowerCase().startsWith('https://')) {
25+
avatar_path.value = props.avatar
26+
}
27+
} else {
28+
avatar_path.value = avatar_default
29+
}
1630
1731
function getImagePath(e) {
1832
const path = URL.createObjectURL(e.target.files[0])
1933
if (path) {
20-
image.value = path
34+
avatar_path.value = path
2135
}
2236
}
2337
2438
async function removeAvatar() {
2539
try {
2640
await axios.post(props.remove_avatar_url, [])
27-
image.value = avatar
41+
avatar_path.value = avatar_default
2842
alert(props.remove_success)
2943
} catch (err) {
3044
alert(props.remove_error)
@@ -45,9 +59,9 @@ async function removeAvatar() {
4559
</svg>
4660
</div>
4761

48-
<img :src="image ?? avatar" class="avatar-image" />
62+
<img :src="avatar_path" class="avatar-image" />
4963

50-
<Input @change="getImagePath" :label="props.label" name="image" type="file" />
64+
<Input @change="getImagePath" :label="props.label" name="avatar" type="file" />
5165
</div>
5266
</div>
5367
</template>

0 commit comments

Comments
 (0)