Skip to content

Commit

Permalink
fix: Update frontend after backend changes (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
taustad authored Oct 9, 2023
1 parent 31b413b commit dd082df
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 79 deletions.
11 changes: 9 additions & 2 deletions src/Components/SideSheet/Comments/CommentSideSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
import styled from "styled-components"
import { Button, Icon, Tooltip } from "@equinor/eds-core-react"
import { search, filter_alt } from "@equinor/eds-icons"
import { useCurrentContext } from "@equinor/fusion-framework-react-app/context"
import CommentView from "./Components/CommentView"
import LocalNavigation from "../Components/LocalNavigation"
import TabsTitle from "../Components/TabsTitle"
Expand Down Expand Up @@ -74,6 +75,8 @@ const CommentSideSheet: FC<Props> = ({
}
}, [activeTab])

const currentContext = useCurrentContext()

const getPropertyValue = (property: string, obj: any): any => {
if (obj == null) { return null }
if (Object.prototype.hasOwnProperty.call(obj, property)) {
Expand Down Expand Up @@ -137,8 +140,12 @@ const CommentSideSheet: FC<Props> = ({
useEffect(() => {
(async () => {
try {
if (!activeTagData?.review || !activeTagData?.review.id) { return }
const newConversations = await (await GetConversationService()).getConversationsForTagReview(activeTagData.review.id)
if (!activeTagData?.tagNo || !currentContext.currentContext?.externalId) { return }
const newConversations = await (await GetConversationService()).getConversationsForTag(
currentContext.currentContext?.externalId,
activeTagData.tagNo,
true,
)
setConversations(newConversations)
} catch (error) {
console.error("Error getting messages for conversation: ", error)
Expand Down
26 changes: 15 additions & 11 deletions src/Components/SideSheet/Comments/Components/CommentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {
useState,
} from "react"
import styled from "styled-components"
import { useCurrentContext } from "@equinor/fusion"
import { useCurrentContext } from "@equinor/fusion-framework-react-app/context"
import { GetConversationService } from "../../../../api/ConversationService"
import { Message } from "../../../../Models/Message"
import InputController from "./InputController"
Expand All @@ -13,6 +13,7 @@ import ClusteredMessages from "./ClusteredMessages"
import TagDropDown from "./TagDropDown"
import { processMessageInput } from "../../../../utils/helpers"
import { GetProjectService } from "../../../../api/ProjectService"
import { GetMessageService } from "../../../../api/MessageService"

const Controls = styled.div`
position: sticky;
Expand Down Expand Up @@ -60,17 +61,17 @@ const CommentView: React.FC<CommentViewProps> = ({
setActiveConversation,
} = useContext(ViewContext)

const fusionContextId = useCurrentContext()
const currentContext = useCurrentContext()

const getConversationForProperty = (property: string) => (
conversations.find((conversation) => conversation.property?.toUpperCase() === property.toUpperCase())
)

useEffect(() => {
(async () => {
if (fusionContextId) {
if (currentContext && currentContext.currentContext?.id) {
try {
const userTagsResult = await (await GetProjectService()).getUsers(fusionContextId.id, "", 1000, 0)
const userTagsResult = await (await GetProjectService()).getUsers(currentContext.currentContext.id, "", 1000, 0)
setUserTags(userTagsResult.data)
} catch (error) {
console.error("Error getting users for project: ", error)
Expand All @@ -85,8 +86,7 @@ const CommentView: React.FC<CommentViewProps> = ({
const currentConversationId = getConversationForProperty(currentProperty)?.id

if (currentConversationId) {
const currentConversation = await (await GetConversationService()).getMessagesForConversation(
activeTagData?.review?.id ?? "",
const currentConversation = await (await GetMessageService()).getMessagesForConversation(
currentConversationId,
)
setActiveConversation(currentConversation)
Expand Down Expand Up @@ -122,9 +122,14 @@ const CommentView: React.FC<CommentViewProps> = ({
conversationLevel: "Tag",
conversationStatus: "Open",
}
if (!currentContext.currentContext?.externalId || !activeTagData?.tagNo) { return }
try {
const service = await GetConversationService()
const savedConversation = await service.createConversation(activeTagData?.review?.id ?? "", createCommentDto)
const savedConversation = await service.createConversation(
currentContext.currentContext?.externalId,
activeTagData?.tagNo,
createCommentDto,
)
setActiveConversation(savedConversation)
const newConversations = [...conversations, savedConversation]
setConversations(newConversations)
Expand All @@ -135,13 +140,12 @@ const CommentView: React.FC<CommentViewProps> = ({
}

const addMessage = async () => {
const message = { ...newMessage }
const { processedString, mentions } = processMessageInput(newMessage?.text ?? "")
const message: Components.Schemas.MessageDto = { ...newMessage, text: processedString }
console.log("mentions: ", mentions) // to be used for tagging users in the future
message.text = processedString
try {
const service = await GetConversationService()
const savedMessage = await service.addMessage(activeTagData?.review?.id ?? "", activeConversation?.id ?? "", message)
const service = await GetMessageService()
const savedMessage = await service.addMessage(activeConversation?.id ?? "", message)

const updatedMessages = [...activeConversation?.messages ?? [], savedMessage]

Expand Down
21 changes: 11 additions & 10 deletions src/Components/SideSheet/Comments/Components/RenderComment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
delete_to_trash, edit,
} from "@equinor/eds-icons"
import { Message } from "../../../../Models/Message"
import { GetConversationService } from "../../../../api/ConversationService"
import { Conversation } from "../../../../Models/Conversation"
import { ViewContext } from "../../../../Context/ViewContext"
import { unescapeHtmlEntities } from "../../../../utils/helpers"
import { GetMessageService } from "../../../../api/MessageService"

const Container = styled.div`
max-width: 500px;
Expand Down Expand Up @@ -55,18 +55,19 @@ interface RenderCommentProps {
const updateComment = async (
reviewId: string,
activeConversationId: string,
comment: Message,
message: Message,
newCommentText: string,
activeConversation: Conversation,
setActiveConversation: Dispatch<SetStateAction<Conversation | undefined>>,
) => {
if (newCommentText && comment.id) {
if (newCommentText && message.id) {
try {
const newComment = { ...comment }
newComment.text = newCommentText
const commentService = await GetConversationService()
const updatedComment = await commentService.updateMessage(reviewId, activeConversationId, comment.id, newComment)
const updatedMessages = activeConversation.messages?.map((m) => (m.id !== comment.id ? m : updatedComment))
const newMessage: Components.Schemas.MessageDto = {
text: newCommentText,
}
const commentService = await GetMessageService()
const updatedComment = await commentService.updateMessage(activeConversationId, message.id, newMessage)
const updatedMessages = activeConversation.messages?.map((m) => (m.id !== message.id ? m : updatedComment))
const updatedConversation = { ...activeConversation }
updatedConversation.messages = updatedMessages
setActiveConversation(updatedConversation)
Expand All @@ -85,8 +86,8 @@ const deleteComment = async (
) => {
if (message.id && activeConversation && activeConversation.messages) {
try {
const service = await GetConversationService()
const response = await service.deleteMessage(reviewId, activeConversationId, message.id)
const service = await GetMessageService()
const response = await service.deleteMessage(activeConversationId, message.id)
if (response === 204) {
const deletedMessage = { ...activeConversation.messages?.find((m) => m.id === message.id) }
deletedMessage.softDeleted = true
Expand Down
8 changes: 6 additions & 2 deletions src/Views/JIP33InstrumentTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from "react"
import { useParams } from "react-router-dom"
import { comment_chat, open_side_sheet } from "@equinor/eds-icons"
import { useCurrentContext } from "@equinor/fusion-framework-react-app/context"
import { generateGeneralRowData } from "../Components/JIP33Table/RowData/Instrument/GeneralRowData"
import { generateInstallationConditionsRowData } from "../Components/JIP33Table/RowData/Instrument/InstallationConditionsRowData"
import { generateOperatingConditionsRowData } from "../Components/JIP33Table/RowData/Instrument/OperatingConditionsRowData"
Expand Down Expand Up @@ -88,6 +89,8 @@ function JIP33InstrumentTabView({ }) {
const [activeTab, setActiveTab] = useState(0)
const [sheetWidth, setSheetWidth] = useState(0)

const currentContext = useCurrentContext()

const {
activeTagData, setActiveTagData, activeSheetTab, setActiveSheetTab, setConversations,
} = useContext(ViewContext)
Expand All @@ -105,9 +108,10 @@ function JIP33InstrumentTabView({ }) {
}, [setOpen])

const getConversationsForTagReview = async (id: string) => {
const newConversations: Conversation[] = await (
if (!currentContext.currentContext?.externalId || !activeTagData?.tagNo) { return }
const newConversations = await (
await GetConversationService()
).getConversationsForTagReview(id)
).getConversationsForTag(currentContext.currentContext.externalId, activeTagData.tagNo, true)
setConversations(newConversations)
}

Expand Down
47 changes: 11 additions & 36 deletions src/api/ConversationService.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
import { Message } from "../Models/Message"
import { BaseService } from "./BaseService"
import { config, GetToken, LoginAccessTokenKey } from "./config"

class ConversationService extends BaseService {
async getMessage(id: string) {
const result: any = await this.get(`project/${id}`)
return result.value
}

async getMessagesForConversation(reviewId: string, conversationId: string) {
const result: any = await this.get(`${reviewId}/conversations/${conversationId}`)
async getConversationsForTag(projectId: string, tagNo: string, includeLatestMessage: boolean = false) {
const result: Components.Schemas.GetConversationDto[] = await this.get(
`${projectId}/tags/${tagNo}/conversations?includeLatestMessage=${includeLatestMessage}`,
)
return result
}

async getConversationsForTagReview(reviewId: string) {
const result: any = await this.get(`${reviewId}/conversations`)
return result
}

async addMessage(reviewId: string, conversationId: string, message: Message) {
const result: any = await this.post(`${reviewId}/conversations/${conversationId}/messages`, {
body: message,
})
return result
}

async createConversation(reviewId: string, message: Components.Schemas.ConversationDto) {
const result: any = await this.post(`${reviewId}/conversations`, {
body: message,
})
return result
}

async deleteMessage(reviewId: string, conversationId: string, commentId: string) {
const result: any = await this.delete(`${reviewId}/conversations/${conversationId}/messages/${commentId}`)
return result.status
}

async updateMessage(reviewId: string, conversationId: string, commentId: string, message: Message) {
const result: any = await this.put(`${reviewId}/conversations/${conversationId}/messages/${commentId}`, {
body: message,
})
async createConversation(projectId: string, tagNo: string, message: Components.Schemas.ConversationDto) {
const result: Components.Schemas.ConversationDto = await this.post(
`${projectId}/tags/${tagNo}/conversations`,
{
body: message,
},
)
return result
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/api/MessageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BaseService } from "./BaseService"
import { config, GetToken, LoginAccessTokenKey } from "./config"

class MessageService extends BaseService {
async getMessage(id: string) {
const result: any = await this.get(`project/${id}`)
return result.value
}

async getMessagesForConversation(conversationId: string) {
const result: any = await this.get(`${conversationId}`)
return result
}

async addMessage(conversationId: string, message: Components.Schemas.MessageDto) {
const result: any = await this.post(`${conversationId}/messages`, {
body: message,
})
return result
}

async deleteMessage(conversationId: string, messageId: string) {
const result: any = await this.delete(`${conversationId}/messages/${messageId}`)
return result.status
}

async updateMessage(conversationId: string, messageId: string, message: Components.Schemas.MessageDto) {
const result: any = await this.put(`${conversationId}/messages/${messageId}`, {
body: message,
})
return result
}
}

export async function GetMessageService() {
return new MessageService({
...config.MessageService,
accessToken: await GetToken(LoginAccessTokenKey)!,
})
}
6 changes: 5 additions & 1 deletion src/api/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const configuration = {
ConversationService: {
BASE_URL: "",
},
MessageService: {
BASE_URL: "",
},
TagDataReviewService: {
BASE_URL: "",
},
Expand All @@ -32,7 +35,8 @@ export const buildConfig = (baseUrl: string) => {
configuration.ProjectService.BASE_URL = `${baseUrl}/projects`
configuration.TagDataService.BASE_URL = `${baseUrl}/tagdata`
configuration.ContractService.BASE_URL = `${baseUrl}/contracts`
configuration.ConversationService.BASE_URL = `${baseUrl}/tag/reviews`
configuration.ConversationService.BASE_URL = `${baseUrl}/projects`
configuration.MessageService.BASE_URL = `${baseUrl}/conversations`
configuration.TagDataReviewService.BASE_URL = `${baseUrl}/tagdatareviews`
configuration.RevisionReviewService.BASE_URL = `${baseUrl}/revisionreviews`
configuration.UserTagService.BASE_URL = `${baseUrl}/usertags`
Expand Down
Loading

0 comments on commit dd082df

Please sign in to comment.