restructure members storage in rooms store

This commit is contained in:
grimhilt 2023-04-06 20:24:11 +02:00
parent 8c6a2bcfd7
commit 7c98c1eb0c
10 changed files with 76 additions and 67 deletions

View File

@ -7,7 +7,10 @@ export enum RoomType {
} }
export interface Message { export interface Message {
todo: true; fromA: string;
subject: string;
content: string;
date: string;
} }
export enum LoadingState { export enum LoadingState {
@ -23,6 +26,7 @@ export interface Room {
mailboxId: number; mailboxId: number;
user: string; user: string;
userId: number; userId: number;
members: Address[];
notSeen: number; notSeen: number;
messages: Message[]; messages: Message[];
messageLoading: LoadingState; messageLoading: LoadingState;

View File

@ -30,6 +30,7 @@ function createRoom(options: RoomFromBack): Room {
roomType: options.roomType, roomType: options.roomType,
mailboxId: options.mailboxId, mailboxId: options.mailboxId,
userId: options.userId, userId: options.userId,
members: [],
user: options.user, user: options.user,
notSeen: options.notSeen, notSeen: options.notSeen,
messages: [], messages: [],
@ -41,7 +42,6 @@ function createRoom(options: RoomFromBack): Room {
export interface State { export interface State {
rooms: Room[]; rooms: Room[];
accounts: Account[]; accounts: Account[];
addresses: Address[];
activeAccount: number; activeAccount: number;
activeRoom: number; activeRoom: number;
} }
@ -53,31 +53,26 @@ const store = createStore<State>({
state: { state: {
rooms: [], //createRoom({ id: 12, userId: 1, user: "user", roomName: "room name", mailboxId: 2, roomType: 1 }) rooms: [], //createRoom({ id: 12, userId: 1, user: "user", roomName: "room name", mailboxId: 2, roomType: 1 })
accounts: [{ id: 0, email: "All", fetched: false }], accounts: [{ id: 0, email: "All", fetched: false }],
addresses: [],
activeAccount: 0, activeAccount: 0,
activeRoom: 0, activeRoom: 0,
}, },
mutations: { mutations: {
setactiveAccount(state, payload) { setActiveAccount(state, payload) {
state.activeAccount = payload; state.activeAccount = payload;
const account = state.accounts.find((account) => account.id == payload); const account = state.accounts.find((account) => account.id == payload);
store.dispatch("fetchRooms", { accountId: payload, account: account }); store.dispatch("fetchRooms", { accountId: payload, account: account });
}, },
setActiveRoom(state, payload) { setActiveRoom(state, payload) {
state.activeRoom = payload; state.activeRoom = payload;
console.log("set active")
// todo load room on load page // todo load room on load page
// let room; // let room;
// // const room = state.rooms.find((room) => room.id == payload); console.log(payload)
// console.log(state.rooms.length); console.log(state.rooms)
// for (let i = 0; i < state.rooms.length; i++) { const room = state.rooms.find((room) => room.id == payload);
// console.log(state.rooms[i].id, payload.value); console.log(room)
// if (state.rooms[i].id == payload.value) { if (!room) return;
// room = state.rooms[i]; store.dispatch("fetchMessages", { roomId: payload, room: room });
// break;
// }
// }
// console.log(room);
// store.dispatch("fetchMessages", { roomId: payload, room: room });
}, },
addAccounts(state, payload) { addAccounts(state, payload) {
payload.forEach((account: AccountFromBack) => { payload.forEach((account: AccountFromBack) => {
@ -97,7 +92,6 @@ const store = createStore<State>({
const parentRoom = state.rooms.find((room) => room.id == thread.parent_id); // todo debug parent_id to root_id const parentRoom = state.rooms.find((room) => room.id == thread.parent_id); // todo debug parent_id to root_id
if (parentRoom) { if (parentRoom) {
parentRoom.threadIds.push(thread.id); parentRoom.threadIds.push(thread.id);
console.log(parentRoom);
} else { } else {
console.log("yep..."); console.log("yep...");
} }
@ -113,8 +107,10 @@ const store = createStore<State>({
}, },
addAddress(state, payload) { addAddress(state, payload) {
// todo add if not exist // todo add if not exist
const room = state.rooms.find((room) => room.id == payload.roomId);
if (!room) return;
payload.addresses.forEach((address: Address) => { payload.addresses.forEach((address: Address) => {
state.addresses.push(address); room.members.push(address);
}); });
}, },
}, },
@ -133,8 +129,9 @@ const store = createStore<State>({
}, },
address: address:
(state) => (state) =>
(addressId: number): Address | undefined => { (roomId: number, addressId: number): Address | undefined => {
const address = state.addresses.find((address) => address.id == addressId); const room = state.rooms.find((room) => room.id == roomId);
const address = room?.members.find((address) => address.id == addressId);
return address; return address;
}, },
messages: messages:

View File

@ -1,47 +1,40 @@
<script setup lang="ts"> <script setup lang="ts">
import store from "../../store/store"; import { defineProps } from "vue";
import { defineProps, ref, watch } from "vue";
import Badge from "@/components/Badge.vue"; import Badge from "@/components/Badge.vue";
import { RoomType } from "@/store/models/model"; import { RoomType } from "@/store/models/model";
const props = defineProps({ id: Number }); const props = defineProps({ id: Number, room: Object });
const room = ref(store.getters.room(props.id));
// todo auto load
watch(
() => props.id,
(newId) => {
room.value = store.getters.room(newId);
},
);
const roomTitle = () => { const roomTitle = () => {
const type = room.value?.roomType; const type = props.room?.roomType;
if (type === RoomType.DM || type == RoomType.CHANNEL || type == RoomType.ROOM) { if (type === RoomType.DM || type == RoomType.CHANNEL || type == RoomType.ROOM) {
return room.value?.user; return props.room?.user;
} }
return room.value?.roomName; return props.room?.roomName;
}; };
</script> </script>
<template> <template>
<div class="main"> <div class="main">
<div class="context">
<div class="infos"> <div class="infos">
<Badge :value="RoomType[room?.roomType]" /> <Badge :value="RoomType[room?.roomType]" />
{{ roomTitle() }} {{ roomTitle() }}
</div> </div>
<div class="action">action: threads message important</div> <div class="action">action: threads message important</div>
</div> </div>
</div>
</template> </template>
<style scoped> <style scoped>
.main { .context {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
border-bottom: 1px solid #505050; border-bottom: 1px solid #505050;
width: 100%; width: 100%;
height: 51px; height: 35px;
} }
.infos { .infos {

View File

@ -0,0 +1,15 @@
<script setup lang="ts">
import { defineProps } from "vue";
const props = defineProps({ type: String, members: Object });
</script>
<template>
<div class="main">
</div>
</template>
<style scoped>
</style>

View File

@ -5,7 +5,15 @@ import { removeDuplicates } from "../../utils/array";
import DOMPurify from "dompurify"; import DOMPurify from "dompurify";
import store from "@/store/store"; import store from "@/store/store";
const props = defineProps({ data: Object }); const props = defineProps({
data: {
fromA: String,
subject: String,
content: String,
date: String,
},
members: Array
});
const iframe = ref(null); const iframe = ref(null);
// todo dompurify // todo dompurify
@ -47,11 +55,10 @@ onMounted(() => {
}); });
const displayAddresses = (addressesId) => { const displayAddresses = (addressesId) => {
// todo store members in rooms ?
addressesId = removeDuplicates(addressesId); addressesId = removeDuplicates(addressesId);
let res = ""; let res = "";
addressesId.forEach((addressId) => { addressesId.forEach((addressId) => {
const address = store.getters.address(addressId); const address = props.members.find((member) => member.id == addressId);
if (address) res += address.email; if (address) res += address.email;
}); });
return res; return res;
@ -109,8 +116,7 @@ iframe {
max-height: 300px; max-height: 300px;
width: 100%; width: 100%;
border: none; border: none;
padding: 2px 10px; max-width: 600px; /* template width being 600px to 640px up to 750px (experiment and test) */
max-width: 750px; /* template width being 600px to 640px up to 750px (experiment and test) */
background-color: rgb(234, 234, 234); background-color: rgb(234, 234, 234);
} }

View File

@ -9,30 +9,21 @@ import { RoomType } from "@/store/models/model";
const store = useStore(); const store = useStore();
const route = useRoute(); const route = useRoute();
const id = ref(parseInt(route.params?.id)); const id = ref(parseInt(route.params?.id));
// let room; let room;
onBeforeMount(async () => { onBeforeMount(async () => {
// get data store.commit("setActiveRoom", id.value);
// room = store.getters.room(id); room = store.getters.room(id.value);
// if (!room || room?.fetched === false) {
// // todo
// // await store.dispatch("fetchMessages", );
// }
store.commit("setActiveRoom", id);
}); });
onBeforeRouteUpdate(async (to, from) => { onBeforeRouteUpdate(async (to, from) => {
if (to.params.id !== from.params.id) { if (to.params.id !== from.params.id) {
id.value = parseInt(to.params.id); id.value = parseInt(to.params.id);
console.log(id); store.commit("setActiveRoom", id.value);
// room = store.getters.room(id); room = await store.getters.room(id.value);
// console.log(room);
store.commit("setActiveRoom", id);
} }
}); });
const shouldDisplayComposer = () => { const shouldDisplayComposer = () => {
if (!id.value) return false;
const room = store.getters.room(id.value);
if (!room) return false; if (!room) return false;
return room.roomType == RoomType.THREAD || room.roomType == RoomType.GROUP; return room.roomType == RoomType.THREAD || room.roomType == RoomType.GROUP;
}; };
@ -40,12 +31,16 @@ const shouldDisplayComposer = () => {
<template> <template>
<div id="main"> <div id="main">
<Header :id="id"></Header> <Header :id="id" :room="room"></Header>
<div id="RoomViewBody"> <div id="RoomViewBody">
<div class="content"> <div class="content">
<Message v-for="(message, index) in store.getters.messages(id)" :key="index" :data="message" /> <Message
v-for="(message, index) in room?.messages"
:key="index"
:data="message"
:members="room?.members"
/>
</div> </div>
{{ room?.roomType }}
<div id="composer" v-if="shouldDisplayComposer()">COMPOSER</div> <div id="composer" v-if="shouldDisplayComposer()">COMPOSER</div>
</div> </div>
</div> </div>

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="container" @click="setactiveAccount(data.id)" :class="activeAccount == data.id && 'active'"> <div class="container" @click="setActiveAccount(data.id)" :class="activeAccount == data.id && 'active'">
{{ data.email }} {{ data.email }}
</div> </div>
</template> </template>
@ -17,7 +17,7 @@ export default {
...mapState(["activeAccount"]), ...mapState(["activeAccount"]),
}, },
methods: { methods: {
...mapMutations(["setactiveAccount"]), ...mapMutations(["setActiveAccount"]),
}, },
}; };
</script> </script>

View File

@ -14,10 +14,10 @@ const props = defineProps({
userId: Number, userId: Number,
notSeen: Number, notSeen: Number,
mailboxId: Number, mailboxId: Number,
threadIds: [Number], threadIds: Array,
}, },
}); });
console.log(props.data.threadIds); // console.log(props.data.threadIds);
const router = useRouter(); const router = useRouter();
</script> </script>

View File

@ -7,7 +7,6 @@ const props = defineProps({
threadId: Number, threadId: Number,
}); });
const room = store.getters.room(props.threadId); const room = store.getters.room(props.threadId);
console.log(props.thread);
const router = useRouter(); const router = useRouter();
</script> </script>

View File

@ -1,7 +1,7 @@
<script setup> <script setup>
import Thread from "./Thread.vue"; import Thread from "./Thread.vue";
import { defineProps } from "vue"; import { defineProps } from "vue";
const props = defineProps({ threadIds: [Number] }); const props = defineProps({ threadIds: Array });
</script> </script>
<template> <template>