154 lines
4.9 KiB
TypeScript
154 lines
4.9 KiB
TypeScript
import API from "@/services/imapAPI";
|
|
import { createStore, Store } from "vuex";
|
|
import { Room, Account, Address, RoomType, Message } from "./models/model";
|
|
|
|
interface RoomFromBack {
|
|
id: number;
|
|
roomName: string;
|
|
roomType: RoomType;
|
|
mailboxId: number;
|
|
user: string;
|
|
userId: number;
|
|
// unseen: number;
|
|
// todo thread
|
|
}
|
|
|
|
interface AccountFromBack {
|
|
id: number;
|
|
email: string;
|
|
}
|
|
|
|
function createRoom(options: RoomFromBack): Room {
|
|
return {
|
|
id: options.id,
|
|
roomName: options.roomName,
|
|
roomType: options.roomType,
|
|
mailboxId: options.mailboxId,
|
|
userId: options.userId,
|
|
user: options.user,
|
|
unseen: 0,
|
|
messages: [],
|
|
messagesFetched: false,
|
|
threads: [],
|
|
};
|
|
}
|
|
|
|
export interface State {
|
|
rooms: Room[];
|
|
accounts: Account[];
|
|
addresses: Address[];
|
|
activeAccount: number;
|
|
activeRoom: number;
|
|
}
|
|
|
|
// // define injection key todo
|
|
// export const key: InjectionKey<Store<State>> = Symbol()
|
|
|
|
const store = createStore<State>({
|
|
state: {
|
|
rooms: [createRoom({ id: 12, userId: 1, user: "user", roomName: "room name", mailboxId: 2, roomType: 1 })],
|
|
accounts: [{ id: 0, email: "All", fetched: false }],
|
|
addresses: [],
|
|
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: AccountFromBack) => {
|
|
state.accounts.push({ id: account.id, email: account.email, fetched: false });
|
|
});
|
|
},
|
|
addRooms(state, payload) {
|
|
// todo add if not exist
|
|
payload.rooms.forEach((room: RoomFromBack) => {
|
|
state.rooms.push(createRoom(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: Message) => {
|
|
room.messages.push(message);
|
|
});
|
|
room.messagesFetched = true;
|
|
},
|
|
addAddress(state, payload) {
|
|
// todo add if not exist
|
|
payload.addresses.forEach((address: 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: number) => {
|
|
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("fetchRoomMembers", { 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;
|