|
| 1 | +<script lang="ts" setup> |
| 2 | +import { useMutation, useQuery } from '@vue/apollo-composable' |
| 3 | +import gql from 'graphql-tag' |
| 4 | +import { computed, ref } from 'vue' |
| 5 | +import MessageItem from './MessageItem.vue' |
| 6 | +
|
| 7 | +interface Channel { |
| 8 | + id: string |
| 9 | + label: string |
| 10 | +} |
| 11 | +
|
| 12 | +const channelsQuery = useQuery<{ channels: Channel[] }>(gql` |
| 13 | + query channels { |
| 14 | + channels { |
| 15 | + id |
| 16 | + label |
| 17 | + } |
| 18 | + } |
| 19 | +`) |
| 20 | +
|
| 21 | +const channels = computed(() => channelsQuery.result.value?.channels ?? []) |
| 22 | +
|
| 23 | +const selectedChannelId = ref<string | null>(null) |
| 24 | +
|
| 25 | +const selectedChannelQuery = useQuery(gql` |
| 26 | + query channel ($id: ID!) { |
| 27 | + channel (id: $id) { |
| 28 | + id |
| 29 | + label |
| 30 | + messages { |
| 31 | + id |
| 32 | + text |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +`, () => ({ |
| 37 | + id: selectedChannelId.value, |
| 38 | +}), () => ({ |
| 39 | + enabled: !!selectedChannelId.value, |
| 40 | +})) |
| 41 | +
|
| 42 | +const addMessageMutation = useMutation(gql` |
| 43 | + mutation sendMessage ($input: AddMessageInput!) { |
| 44 | + message: addMessage (input: $input) { |
| 45 | + id |
| 46 | + text |
| 47 | + } |
| 48 | + } |
| 49 | +`) |
| 50 | +
|
| 51 | +const selectedChannel = computed(() => selectedChannelQuery.result.value?.channel) |
| 52 | +
|
| 53 | +const newMessageText = ref('') |
| 54 | +
|
| 55 | +async function sendMessage () { |
| 56 | + if (!newMessageText.value.length) return |
| 57 | +
|
| 58 | + const result = await addMessageMutation.mutate({ |
| 59 | + input: { |
| 60 | + channelId: selectedChannelId.value, |
| 61 | + text: newMessageText.value, |
| 62 | + }, |
| 63 | + }) |
| 64 | +
|
| 65 | + newMessageText.value = '' |
| 66 | +
|
| 67 | + selectedChannelQuery.updateQuery(previousResult => ({ |
| 68 | + ...previousResult, |
| 69 | + channel: { |
| 70 | + ...previousResult.channel, |
| 71 | + messages: [ |
| 72 | + ...previousResult.channel.messages, |
| 73 | + result?.data.message, |
| 74 | + ], |
| 75 | + }, |
| 76 | + })) |
| 77 | +} |
| 78 | +</script> |
| 79 | + |
| 80 | +<template> |
| 81 | + <div class="h-full flex flex-col"> |
| 82 | + <div class="flex h-full"> |
| 83 | + <div class="flex flex-col"> |
| 84 | + <button |
| 85 | + v-for="channel of channels" |
| 86 | + :key="channel.id" |
| 87 | + class="channel-btn p-4" |
| 88 | + :class="{ |
| 89 | + 'bg-green-200': selectedChannelId === channel.id, |
| 90 | + }" |
| 91 | + @click="selectedChannelId = channel.id" |
| 92 | + > |
| 93 | + {{ channel.label }} |
| 94 | + </button> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div |
| 98 | + v-if="selectedChannel" |
| 99 | + class="the-channel flex flex-col w-full h-full overflow-auto" |
| 100 | + > |
| 101 | + <div class="flex-none p-6 border-b border-gray-200 bg-white"> |
| 102 | + # {{ selectedChannel.label }} |
| 103 | + </div> |
| 104 | + |
| 105 | + <div class="flex-1 overflow-y-auto"> |
| 106 | + <MessageItem |
| 107 | + v-for="message of selectedChannel.messages" |
| 108 | + :key="message.id" |
| 109 | + :message="message" |
| 110 | + class="m-2" |
| 111 | + /> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div class="flex gap-x-2 p-4"> |
| 115 | + <div class="border border-gray-200 rounded-lg bg-white flex-1"> |
| 116 | + <input |
| 117 | + v-model="newMessageText" |
| 118 | + type="text" |
| 119 | + class="message-input w-full h-full bg-transparent px-3" |
| 120 | + placeholder="Type a message..." |
| 121 | + > |
| 122 | + </div> |
| 123 | + |
| 124 | + <button |
| 125 | + class="send-message-btn bg-green-200 py-3 px-4 rounded-lg" |
| 126 | + @click="sendMessage" |
| 127 | + > |
| 128 | + Send |
| 129 | + </button> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div |
| 134 | + v-else |
| 135 | + class="no-data" |
| 136 | + > |
| 137 | + No data |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | +</template> |
0 commit comments