Skip to content

Commit

Permalink
fix: moved shared data to parent component and managed via props
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Sep 26, 2024
1 parent daacd16 commit f79dd5f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 121 deletions.
76 changes: 17 additions & 59 deletions src/components/POS/ItemsGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
class="border border-gray-300 p-1 flex flex-col text-sm text-center"
@click="handleChange(item as POSItem)"
>
<div
class="self-center w-32 h-32 rounded-lg mb-1"
>
<div class="self-center w-32 h-32 rounded-lg mb-1">
<div class="relative">
<img
v-if="item.image"
Expand All @@ -34,7 +32,15 @@
/>
<div
v-else
class="rounded-lg w-32 h-32 flex bg-gray-100 justify-center items-center"
class="
rounded-lg
w-32
h-32
flex
bg-gray-100
justify-center
items-center
"
>
<p class="text-4xl font-semibold text-gray-400">
{{ getExtractedWords(item.name) }}
Expand Down Expand Up @@ -66,70 +72,22 @@
</template>

<script lang="ts">
import { defineComponent, inject } from 'vue';
import { defineComponent } from 'vue';
import { fyo } from 'src/initFyo';
import { ModelNameEnum } from 'models/types';
import { Item } from 'models/baseModels/Item/Item';
import { ItemQtyMap, POSItem } from './types';
import { Money } from 'pesa';
import { POSItem } from './types';
export default defineComponent({
name: 'ItemsGrid',
emits: ['addItem', 'updateValues'],
setup() {
return {
itemQtyMap: inject('itemQtyMap') as ItemQtyMap,
};
},
data() {
return {
items: [] as POSItem[],
};
},
async mounted() {
await this.setItems();
},
watch: {
props: {
items: {
type: Array,
},
itemQtyMap: {
async handler() {
this.setItems();
},
deep: true,
type: Object,
},
},
methods: {
async setItems() {
console.log('setItems');
const items = (await fyo.db.getAll(ModelNameEnum.Item, {
fields: [],
filters: { trackItem: true },
})) as Item[];
this.items = [] as POSItem[];
for (const item of items) {
let availableQty = 0;
if (!!this.itemQtyMap[item.name as string]) {
availableQty = this.itemQtyMap[item.name as string].availableQty;
}
if (!item.name) {
return;
}
this.items.push({
availableQty,
image: item?.image as string,
name: item.name,
rate: item.rate as Money,
unit: item.unit as string,
hasBatch: !!item.hasBatch,
hasSerialNumber: !!item.hasSerialNumber,
});
}
console.log(this.items, 'this.items');
},
getExtractedWords(item: string) {
const initials = item.split(' ').map((word) => {
return word[0].toUpperCase();
Expand Down
64 changes: 5 additions & 59 deletions src/components/POS/ItemsTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="overflow-y-auto" style="height: 80vh">
<Row
v-if="items"
v-for="row in items"
v-for="row in items as any"
:ratio="ratio"
:border="true"
class="
Expand All @@ -35,7 +35,7 @@
px-2
w-full
"
@click="handleChange(row as POSItem)"
@click="handleChange(row)"
>
<FormControl
v-for="df in tableFields"
Expand All @@ -54,32 +54,17 @@
import FormControl from '../Controls/FormControl.vue';
import Row from 'src/components/Row.vue';
import { isNumeric } from 'src/utils';
import { inject } from 'vue';
import { fyo } from 'src/initFyo';
import { defineComponent } from 'vue';
import { ModelNameEnum } from 'models/types';
import { Field } from 'schemas/types';
import { ItemQtyMap } from './types';
import { Item } from 'models/baseModels/Item/Item';
import { POSItem } from './types';
import { Money } from 'pesa';
export default defineComponent({
name: 'ItemsTable',
components: { FormControl, Row },
emits: ['addItem', 'updateValues'],
async mounted() {
await this.setItems();
},
setup() {
return {
itemQtyMap: inject('itemQtyMap') as ItemQtyMap,
};
},
data() {
return {
items: [] as POSItem[],
};
props: {
items: Array,
itemQtyMap: Object,
},
computed: {
ratio() {
Expand Down Expand Up @@ -119,46 +104,7 @@ export default defineComponent({
] as Field[];
},
},
watch: {
itemQtyMap: {
async handler() {
this.setItems();
},
deep: true,
},
},
async activated() {
await this.setItems();
},
methods: {
async setItems() {
const items = (await fyo.db.getAll(ModelNameEnum.Item, {
fields: [],
filters: { trackItem: true },
})) as Item[];
this.items = [] as POSItem[];
for (const item of items) {
let availableQty = 0;
if (!!this.itemQtyMap[item.name as string]) {
availableQty = this.itemQtyMap[item.name as string].availableQty;
}
if (!item.name) {
return;
}
this.items.push({
availableQty,
name: item.name,
rate: item.rate as Money,
unit: item.unit as string,
hasBatch: !!item.hasBatch,
hasSerialNumber: !!item.hasSerialNumber,
});
}
},
handleChange(value: POSItem) {
this.$emit('addItem', value);
this.$emit('updateValues');
Expand Down
51 changes: 48 additions & 3 deletions src/pages/POS/POS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@
/>
</div>
<ItemsTable v-if="tableView" @add-item="addItem" />
<ItemsGrid v-else @add-item="addItem" />
<ItemsTable
v-if="tableView"
:items="items"
:item-qty-map="itemQtyMap"
@add-item="addItem"
/>
<ItemsGrid
v-else
:items="items"
:item-qty-map="itemQtyMap"
@add-item="addItem"
/>
<div
class="bg-gray-100 p-2 fixed bottom-0 mb-7 rounded-md"
@click="toggleView"
Expand Down Expand Up @@ -263,6 +273,8 @@ export default defineComponent({
},
data() {
return {
items: [] as POSItem[],
tableView: true,
isItemsSeeded: false,
Expand Down Expand Up @@ -326,19 +338,52 @@ export default defineComponent({
deep: true,
},
},
async mounted() {
await this.setItems();
},
async activated() {
toggleSidebar(false);
validateIsPosSettingsSet(fyo);
this.setSinvDoc();
this.setDefaultCustomer();
await this.setItemQtyMap();
await this.setItems();
},
deactivated() {
toggleSidebar(true);
},
methods: {
async setItems() {
const items = (await fyo.db.getAll(ModelNameEnum.Item, {
fields: [],
filters: { trackItem: true },
})) as Item[];
this.items = [] as POSItem[];
for (const item of items) {
let availableQty = 0;
if (!!this.itemQtyMap[item.name as string]) {
availableQty = this.itemQtyMap[item.name as string].availableQty;
}
if (!item.name) {
return;
}
this.items.push({
availableQty,
name: item.name,
image: item?.image as string,
rate: item.rate as Money,
unit: item.unit as string,
hasBatch: !!item.hasBatch,
hasSerialNumber: !!item.hasSerialNumber,
});
}
},
toggleView() {
this.tableView = !this.tableView; // Toggle between table and grid view
this.tableView = !this.tableView;
},
setCashAmount(amount: Money) {
this.cashAmount = amount;
Expand Down

0 comments on commit f79dd5f

Please sign in to comment.