1
1
<script setup>
2
2
import axios from ' axios'
3
3
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'
6
6
7
7
const props = defineProps ({
8
- image : { default: avatar },
8
+ avatar : { default: avatar_default },
9
9
label: { default: ' Select image' },
10
- remove_avatar_url: { default: ' web/api/remove/avatar' },
10
+ remove_avatar_url: { default: ' / web/api/remove/avatar' },
11
11
remove_success: { default: ' Avatar removed.' },
12
12
remove_error: { default: ' Avatar not removed.' },
13
13
})
14
14
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
+ }
16
30
17
31
function getImagePath (e ) {
18
32
const path = URL .createObjectURL (e .target .files [0 ])
19
33
if (path) {
20
- image .value = path
34
+ avatar_path .value = path
21
35
}
22
36
}
23
37
24
38
async function removeAvatar () {
25
39
try {
26
40
await axios .post (props .remove_avatar_url , [])
27
- image .value = avatar
41
+ avatar_path .value = avatar_default
28
42
alert (props .remove_success )
29
43
} catch (err) {
30
44
alert (props .remove_error )
@@ -45,9 +59,9 @@ async function removeAvatar() {
45
59
</svg >
46
60
</div >
47
61
48
- <img :src =" image ?? avatar " class =" avatar-image" />
62
+ <img :src =" avatar_path " class =" avatar-image" />
49
63
50
- <Input @change =" getImagePath" :label =" props.label" name =" image " type =" file" />
64
+ <Input @change =" getImagePath" :label =" props.label" name =" avatar " type =" file" />
51
65
</div >
52
66
</div >
53
67
</template >
0 commit comments