Skip to content

Commit

Permalink
Merge pull request #172 from sanskar345/dxp-components/#170
Browse files Browse the repository at this point in the history
Improved: ShopifyImg component migrated to .vue from .ts (#170)
  • Loading branch information
ravilodhi authored Sep 22, 2023
2 parents 2712314 + ebcb265 commit 444d762
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 54 deletions.
52 changes: 0 additions & 52 deletions src/components/ShopifyImg.ts

This file was deleted.

47 changes: 47 additions & 0 deletions src/components/ShopifyImg.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<img :src="imageUrl" />
</template>

<script setup lang="ts">
import { onMounted, onUpdated, ref } from "vue";
import { shopifyImgContext as context } from "../index";
const props = defineProps(['src', 'size']);
const imageUrl = ref(context.defaultImgUrl);
const checkIfImageExists = (src: string) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(true);
img.onerror = () => reject(false);
img.src = src;
})
};
const prepareImgUrl = (src: string, size?: string) => {
// return original size if no size is given
if (!size) return src
// remove any current image size then add the new image size
return src
.replace(/_(pico|icon|thumb|small|compact|medium|large|grande|original|1024x1024|2048x2048|master)+\./g, '.')
.replace(/\.jpg|\.png|\.gif|\.jpeg/g, function (match) {
return '_' + size + match;
})
};
const setImageUrl = () => {
if (props.src) {
const src: string = prepareImgUrl(props.src, props.size)
checkIfImageExists(src).then(() => imageUrl.value = src)
}
};
onMounted(() => {
setImageUrl();
});
onUpdated(() => {
setImageUrl();
});
</script>
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ import '@ionic/vue/css/display.css';

export { default as ProductIdentifier } from "./ProductIdentifier.vue";
export { default as LanguageSwitcher } from './LanguageSwitcher.vue';
export { default as ShopifyImg } from './ShopifyImg.vue';
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ declare var process: any;
import { createPinia } from "pinia";
import { useProductIdentificationStore } from "./store/productIdentification";
import { useAuthStore } from "./store/auth";
import { LanguageSwitcher, ProductIdentifier } from "./components";
import { LanguageSwitcher, ProductIdentifier, ShopifyImg } from "./components";
import Login from "./components/Login";
import ShopifyImg from "./components/ShopifyImg";
import { goToOms } from "./utils";
import { initialiseFirebaseApp } from "./utils/firebase"
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
Expand Down

0 comments on commit 444d762

Please sign in to comment.