Skip to content

Commit

Permalink
Merge pull request #771 from MahtabBukhari/Update-UI-Components-to-Ha…
Browse files Browse the repository at this point in the history
…ndle-Ticket-Versioning

Update UI Components to Handle Ticket Versioning
  • Loading branch information
humansinstitute authored Dec 15, 2024
2 parents 96c366e + 84acf9e commit 918a893
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/components/common/TicketEditor/TicketEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import React, { useState } from 'react';
import { observer } from 'mobx-react-lite';
import { useStores } from 'store';
import { EuiGlobalToastList, EuiFlexGroup, EuiFlexItem, EuiPanel, EuiIcon } from '@elastic/eui';
import {
EuiGlobalToastList,
EuiFlexGroup,
EuiFlexItem,
EuiPanel,
EuiIcon,
EuiBadge
} from '@elastic/eui';
import { phaseTicketStore } from '../../../store/phase';
import { ActionButton, TicketButtonGroup } from '../../../people/widgetViews/workspace/style';
import {
Expand Down Expand Up @@ -161,6 +168,9 @@ const TicketEditor = observer(
}
placeholder="Enter ticket name..."
/>
<EuiBadge color="success" style={{ marginBottom: '12px' }}>
Version {ticketData.version}
</EuiBadge>
</TicketHeaderInputWrap>
<TicketTextArea
value={description}
Expand Down
13 changes: 8 additions & 5 deletions src/people/widgetViews/PhasePlannerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ const PhasePlannerView: React.FC = observer(() => {
try {
const ticket = await main.getTicketDetails(ticketUuid);

// TODO: Backend needs to send this as uuid and not UUID
phaseTicketStore.updateTicket(ticket.uuid || ticket.UUID, ticket);
const groupId = ticket.ticket_group || ticket.uuid;

const latestTicket = phaseTicketStore.getLatestVersionFromGroup(groupId);

phaseTicketStore.updateTicket(latestTicket?.uuid as string, latestTicket as Ticket);
} catch (error) {
console.error('Error on refreshing ticket:', error);
}
Expand Down Expand Up @@ -158,11 +161,11 @@ const PhasePlannerView: React.FC = observer(() => {
if (!feature || !phase || !Array.isArray(phaseTickets)) {
history.push('/');
} else {
// Clear existing tickets for this phase before adding new ones
phaseTicketStore.clearPhaseTickets(phase_uuid);

// Add fetched tickets
for (const ticket of phaseTickets) {
const latestVersionTickets = phaseTicketStore.organizeTicketsByGroup(phaseTickets);

for (const ticket of latestVersionTickets) {
if (ticket.UUID) {
ticket.uuid = ticket.UUID;
}
Expand Down
1 change: 1 addition & 0 deletions src/store/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export type Author = 'HUMAN' | 'AGENT';

export interface Ticket {
uuid: string;
UUID?: string;
ticketUUID?: string;
feature_uuid: string;
phase_uuid: string;
Expand Down
5 changes: 4 additions & 1 deletion src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3603,7 +3603,10 @@ export class MainStore {
}
}

async getTicketDataByPhase(feature_uuid: string, phase_uuid: string): Promise<Phase | undefined> {
async getTicketDataByPhase(
feature_uuid: string,
phase_uuid: string
): Promise<Ticket[] | undefined> {
try {
if (!uiStore.meInfo) return undefined;
const info = uiStore.meInfo;
Expand Down
33 changes: 33 additions & 0 deletions src/store/phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface TicketStore {
getTicket: (uuid: string) => Ticket | undefined;
getPhaseTickets: (phase_uuid: string) => Ticket[];
clearPhaseTickets: (phase_uuid: string) => void;
organizeTicketsByGroup: (tickets: Ticket[]) => Ticket[];
getLatestVersionFromGroup: (groupId: string) => Ticket | undefined;
}

export class PhaseTicketStore implements TicketStore {
Expand Down Expand Up @@ -65,6 +67,37 @@ export class PhaseTicketStore implements TicketStore {
});
this.phaseTickets[phase_uuid] = [];
}

organizeTicketsByGroup(tickets: Ticket[]): Ticket[] {
const groupedTickets = tickets.reduce(
(groups: Record<string, Ticket[]>, ticket: Ticket) => {
const group = ticket.ticket_group || ticket.uuid;
if (!groups[group]) {
groups[group] = [];
}
groups[group].push(ticket);
return groups;
},
{} as Record<string, Ticket[]>
);

return Object.values(groupedTickets).map((groupTickets: Ticket[]) =>
groupTickets.reduce((latest: Ticket, current: Ticket) =>
current.version > latest.version ? current : latest
)
);
}

getLatestVersionFromGroup(groupId: string): Ticket | undefined {
const ticketsInGroup = Array.from(this.tickets.values()).filter(
(ticket: Ticket) => ticket.ticket_group === groupId
);
if (ticketsInGroup.length === 0) return undefined;

return ticketsInGroup.reduce((latest: Ticket, current: Ticket) =>
current.version > latest.version ? current : latest
);
}
}

export const phaseTicketStore = new PhaseTicketStore();

0 comments on commit 918a893

Please sign in to comment.