Files
web-antrean/stores/loketStore.js
T
2026-01-08 12:44:40 +07:00

137 lines
3.3 KiB
JavaScript

// stores/loketStore.js
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { useClinicStore } from './clinicStore';
export const useLoketStore = defineStore('loket', () => {
const clinicStore = useClinicStore();
// State - Loket Data
const loketData = ref([
{
id: 1,
no: 1,
namaLoket: "Loket 1",
kuota: 500,
pelayanan: ["RT", "RM", "TD"],
pembayaran: "JKN",
keterangan: "ONLINE",
statusPelayanan: "RAWAT JALAN",
},
{
id: 2,
no: 2,
namaLoket: "Loket 2",
kuota: 666,
pelayanan: ["JW", "SR"],
pembayaran: "JKN",
keterangan: "ONLINE",
statusPelayanan: "RAWAT JALAN",
},
{
id: 3,
no: 3,
namaLoket: "Loket 3",
kuota: 666,
pelayanan: ["AS", "JT"],
pembayaran: "JKN",
keterangan: "ONLINE",
statusPelayanan: "RAWAT JALAN",
},
{
id: 4,
no: 4,
namaLoket: "Loket 4",
kuota: 3676,
pelayanan: ["KK", "PR"],
pembayaran: "JKN",
keterangan: "ONLINE",
statusPelayanan: "RAWAT JALAN",
},
]);
// Computed - Available services (reference dari clinicStore)
const availableServices = computed(() => {
const clinics = clinicStore.getAllClinics.value || [];
return clinics.map(c => ({
id: c.kode,
nama: c.name,
kode: c.kode,
}));
});
// Actions - CRUD Operations
const addLoket = (loketPayload) => {
const newId = Math.max(...loketData.value.map(l => l.id), 0) + 1;
const newNo = loketData.value.length + 1;
const newLoket = {
id: newId,
no: newNo,
...loketPayload,
};
loketData.value.push(newLoket);
return { success: true, message: `Loket ${newLoket.namaLoket} berhasil ditambahkan`, data: newLoket };
};
const updateLoket = (loketPayload) => {
const index = loketData.value.findIndex(l => l.id === loketPayload.id);
if (index !== -1) {
loketData.value[index] = {
...loketData.value[index],
...loketPayload,
};
return { success: true, message: `Loket ${loketPayload.namaLoket} berhasil diupdate` };
}
return { success: false, message: 'Loket tidak ditemukan' };
};
const deleteLoket = (loketId) => {
const index = loketData.value.findIndex(l => l.id === loketId);
if (index !== -1) {
const loketName = loketData.value[index].namaLoket;
loketData.value.splice(index, 1);
loketData.value.forEach((l, idx) => {
l.no = idx + 1;
});
return { success: true, message: `Loket ${loketName} berhasil dihapus` };
}
return { success: false, message: 'Loket tidak ditemukan' };
};
const getLoketById = (id) => {
return loketData.value.find(l => l.id === id);
};
return {
// State
loketData,
availableServices,
// Actions
addLoket,
updateLoket,
deleteLoket,
getLoketById,
};
}, {
persist: {
key: 'loket-store-state',
storage: typeof window !== 'undefined' ? localStorage : undefined,
paths: ['loketData'],
serializer: {
deserialize: JSON.parse,
serialize: JSON.stringify,
},
restore: (value) => {
// Ensure loketData is always an array
if (value && value.loketData && !Array.isArray(value.loketData)) {
value.loketData = [];
}
return value;
},
},
});