Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: ShopifyImg component migrated to .vue from .ts (#170) #172

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
5 changes: 4 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ import '@ionic/vue/css/text-transformation.css';
import '@ionic/vue/css/flex-utils.css';
import '@ionic/vue/css/display.css';

export { default as ProductIdentifier } from "./ProductIdentifier.vue";
import ProductIdentifier from './ProductIdentifier.vue';
import ShopifyImg from './ShopifyImg.vue';

export { ProductIdentifier, ShopifyImg };

3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { useProductIdentificationStore } from "./store/productIdentification";
import { useAuthStore } from "./store/auth";

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'
import { ProductIdentifier } from "./components";
import { ProductIdentifier, ShopifyImg } from "./components";

// TODO: handle cases when the store from app or pinia store are not available
// creating a pinia store for the plugin
Expand Down
Loading