From 9a97ef61ed574b705dd8f894f09d92f4158dd396 Mon Sep 17 00:00:00 2001 From: Spythere Date: Fri, 22 Jul 2022 15:00:02 +0200 Subject: [PATCH] =?UTF-8?q?Dodano=20aktualizowanie=20zapisanych=20rozkaz?= =?UTF-8?q?=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/OrderList.vue | 5 +-- src/components/OrderMessage.vue | 32 +++++++++++++++++-- src/components/SideBar.vue | 2 ++ src/mixins/orderStorageMixin.ts | 56 +++++++++++++++++++++++---------- src/store/store.ts | 2 ++ src/types/orderTypes.ts | 3 +- 6 files changed, 77 insertions(+), 23 deletions(-) diff --git a/src/components/OrderList.vue b/src/components/OrderList.vue index 73ef376..c2578f4 100644 --- a/src/components/OrderList.vue +++ b/src/components/OrderList.vue @@ -9,7 +9,8 @@ {{ order.orderBody['header']['trainNo'] }}
- Dodano: {{ new Date(order.createdAt).toLocaleString('pl-PL') }} + {{ order.createdAt ? 'Dodano: ' : 'Zaktualizowano: ' }} + {{ new Date(order.createdAt || order.updatedAt || 0).toLocaleString('pl-PL') }}
@@ -58,7 +59,7 @@ export default defineComponent({ computed: { sortedOrderList() { - return this.localOrderList.sort((a, b) => b.createdAt - a.createdAt); + return this.localOrderList.sort((a, b) => (b.createdAt || b.updatedAt!) - (a.createdAt || a.updatedAt!)); }, }, diff --git a/src/components/OrderMessage.vue b/src/components/OrderMessage.vue index cb2666c..183da89 100644 --- a/src/components/OrderMessage.vue +++ b/src/components/OrderMessage.vue @@ -5,8 +5,11 @@
- - + + +
@@ -85,7 +88,7 @@ export default defineComponent({ }, saveOrder() { - const savedOrderStatus = this.saveOrderToStorage(); + const savedOrderStatus = this.saveLocalOrder(); switch (savedOrderStatus) { case -1: @@ -104,6 +107,24 @@ export default defineComponent({ break; } }, + + updateOrder() { + const updatedOrderStatus = this.updateLocalOrder(); + + switch (updatedOrderStatus) { + case -1: + this.showActionMonit('Wystąpił błąd podczas aktualizowania tego rozkazu! :/'); + break; + + case 0: + this.showActionMonit('Nie wybrałeś żadnego zapisanego rozkazu!'); + break; + + case 1: + this.showActionMonit('Zaktualizowano treść rozkazu!'); + break; + } + }, }, }); @@ -148,6 +169,11 @@ export default defineComponent({ vertical-align: text-bottom; margin-right: 0.5em; } + + button[data-disabled='true'] { + user-select: none; + color: #aaa; + } } .action_monit { diff --git a/src/components/SideBar.vue b/src/components/SideBar.vue index 1f31337..c42bcd7 100644 --- a/src/components/SideBar.vue +++ b/src/components/SideBar.vue @@ -50,6 +50,8 @@ export default defineComponent({ methods: { selectOrderType(type: any) { + if (type != this.store.chosenOrderType) this.store.chosenLocalOrderId = ''; + this.store.chosenOrderType = type; }, }, diff --git a/src/mixins/orderStorageMixin.ts b/src/mixins/orderStorageMixin.ts index cfe1c4f..980c2ce 100644 --- a/src/mixins/orderStorageMixin.ts +++ b/src/mixins/orderStorageMixin.ts @@ -10,29 +10,29 @@ export default defineComponent({ }, methods: { - saveOrderToStorage() { + saveLocalOrder() { let orderObj: LocalStorageOrder = { id: '', orderType: this.store.chosenOrderType, - orderBody: {}, + orderBody: this.store[this.store.chosenOrderType], orderFooter: this.store.orderFooter, createdAt: Date.now(), }; - switch (this.store.chosenOrderType) { - case 'orderN': - orderObj['orderBody'] = this.store.orderN; - break; - case 'orderS': - orderObj['orderBody'] = this.store.orderS; - break; - case 'orderO': - orderObj['orderBody'] = this.store.orderO; - break; + // switch (this.store.chosenOrderType) { + // case 'orderN': + // orderObj['orderBody'] = this.store.orderN; + // break; + // case 'orderS': + // orderObj['orderBody'] = this.store.orderS; + // break; + // case 'orderO': + // orderObj['orderBody'] = this.store.orderO; + // break; - default: - break; - } + // default: + // break; + // } const headerInfo = orderObj['orderBody']['header']; @@ -51,10 +51,31 @@ export default defineComponent({ } const nextOrderCount = Number(localOrderCount) + 1; - orderObj['id'] = `order-${nextOrderCount}`; + const orderId = `order-${nextOrderCount}`; + orderObj['id'] = orderId; localStorage.setItem('orderCount', `${nextOrderCount}`); - localStorage.setItem(`order-${nextOrderCount}`, JSON.stringify(orderObj)); + localStorage.setItem(orderId, JSON.stringify(orderObj)); + + this.store.chosenLocalOrderId = orderId; + return 1; + }, + + updateLocalOrder() { + if (!this.store.chosenLocalOrderId) return 0; + const localOrder = window.localStorage.getItem(this.store.chosenLocalOrderId); + + if (!localOrder) return -1; + + let orderObj: LocalStorageOrder = { + id: this.store.chosenLocalOrderId, + orderType: this.store.chosenOrderType, + orderBody: this.store[this.store.chosenOrderType], + orderFooter: this.store.orderFooter, + updatedAt: Date.now(), + }; + + window.localStorage.setItem(this.store.chosenLocalOrderId, JSON.stringify(orderObj)); return 1; }, @@ -66,6 +87,7 @@ export default defineComponent({ selectLocalOrder(order: LocalStorageOrder) { this.store.chosenOrderType = order.orderType; + this.store.chosenLocalOrderId = order.id; const localOrder = JSON.parse(JSON.stringify(order)); const localOrderBody = localOrder['orderBody']; diff --git a/src/store/store.ts b/src/store/store.ts index d576896..d87f372 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -4,6 +4,8 @@ export const useStore = defineStore('store', { state: () => { return { chosenOrderType: 'orderN' as 'orderO' | 'orderS' | 'orderN', + chosenLocalOrderId: '', + orderMode: 'OrderMessage', orderFooter: { diff --git a/src/types/orderTypes.ts b/src/types/orderTypes.ts index 7e6f909..0199486 100644 --- a/src/types/orderTypes.ts +++ b/src/types/orderTypes.ts @@ -3,5 +3,6 @@ export interface LocalStorageOrder { orderType: 'orderO' | 'orderS' | 'orderN'; orderBody: any; orderFooter: any; - createdAt: number; + createdAt?: number; + updatedAt?: number; }