115 lines
4.0 KiB
JavaScript
115 lines
4.0 KiB
JavaScript
import API from "@/services/imapAPI";
|
|
import { createStore } from "vuex";
|
|
import Room from "./models/Room";
|
|
import Account from "./models/Account";
|
|
|
|
const store = createStore({
|
|
state() {
|
|
return {
|
|
rooms: [new Room({ id: 12, user: "user", roomName: "room name", mailbboxId: 2 })],
|
|
messages: [],
|
|
accounts: [new Account(0, "ALL")],
|
|
activeAccount: 0,
|
|
activeRoom: 0,
|
|
};
|
|
},
|
|
mutations: {
|
|
setactiveAccount(state, payload) {
|
|
state.activeAccount = payload;
|
|
const account = state.accounts.find((account) => account.id == payload);
|
|
store.dispatch("fetchRooms", { accountId: payload, account: account });
|
|
},
|
|
setActiveRoom(state, payload) {
|
|
state.activeRoom = payload;
|
|
const room = state.rooms.find((room) => room.id == payload);
|
|
store.dispatch("fetchMessages", { roomId: payload, room: room });
|
|
},
|
|
addAccounts(state, payload) {
|
|
payload.forEach((account) => {
|
|
state.accounts.push(new Account(account.id, account.email));
|
|
});
|
|
},
|
|
addRooms(state, payload) {
|
|
// todo add if not exist
|
|
payload.rooms.forEach((room) => {
|
|
state.rooms.push(new Room(room));
|
|
});
|
|
},
|
|
addMessages(state, payload) {
|
|
// todo add if not exist
|
|
const room = state.rooms.find((room) => room.id == payload.roomId);
|
|
if (!room) return;
|
|
payload.messages.forEach((message) => {
|
|
room.messages.push(message);
|
|
});
|
|
room.messagesFetched = true;
|
|
},
|
|
addAddress(state, payload) {
|
|
// todo add if not exist
|
|
payload.addresses.forEach((address) => {
|
|
state.addresses.push(address);
|
|
});
|
|
},
|
|
},
|
|
getters: {
|
|
rooms: (state) => () => {
|
|
if (state.activeAccount === 0) return state.rooms;
|
|
return state.rooms.filter((room) => room.mailboxId == state.activeAccount);
|
|
},
|
|
messages: (state) => (roomId) => {
|
|
const room = state.rooms.find((room) => room.id == roomId);
|
|
if (!room) return [];
|
|
if (!room.messagesFetched) {
|
|
store.dispatch("fetchMessages", { roomId: room.id });
|
|
}
|
|
return room.messages;
|
|
},
|
|
},
|
|
actions: {
|
|
fetchAccounts: async (context) => {
|
|
API.getAccounts()
|
|
.then((res) => {
|
|
context.commit("addAccounts", res.data);
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
},
|
|
fetchRooms: async (context, data) => {
|
|
if (data.account?.fetched == false) {
|
|
API.getRooms(data.accountId)
|
|
.then((res) => {
|
|
data.account.fetched = true;
|
|
context.commit("addRooms", { rooms: res.data });
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
}
|
|
},
|
|
fetchMessages: async (context, data) => {
|
|
if (!data.room || data.room?.fetched == false) {
|
|
store.dispatch("fetchAddress", { roomId: data.roomId });
|
|
API.getMessages(data.roomId)
|
|
.then((res) => {
|
|
context.commit("addMessages", { messages: res.data, roomId: data.roomId });
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
}
|
|
},
|
|
fetchRoomMembers: async (context, data) => {
|
|
API.getMembers(data.roomId)
|
|
.then((res) => {
|
|
context.commit("addAddress", { addresses: res.data, roomId: data.roomId });
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
},
|
|
},
|
|
});
|
|
|
|
export default store;
|