Skip to content

Commit

Permalink
Merge pull request stakwork#705 from aliraza556/refactor-phase-store-…
Browse files Browse the repository at this point in the history
…implementation

Refactor Phase Store to Support Phase-Based Ticket Grouping
  • Loading branch information
humansinstitute authored Dec 4, 2024
2 parents ebba3a2 + 8e7ba6b commit 1df9bdf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/common/TicketEditor/TicketEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from 'react';
import { useStores } from 'store';
import { EuiGlobalToastList } from '@elastic/eui';
import { phasePlannerStore } from '../../../store/phase';
import { phaseTicketStore } from '../../../store/phase';
import { ActionButton, TicketButtonGroup } from '../../../people/widgetViews/workspace/style';
import {
TicketContainer,
Expand Down Expand Up @@ -87,7 +87,7 @@ const TicketEditor = ({ ticketData }: TicketEditorProps) => {
if (response === 406 || !response) {
throw new Error('Failed to update ticket');
}
phasePlannerStore.updateTicket(ticketData.uuid, updateTicketData);
phaseTicketStore.updateTicket(ticketData.uuid, updateTicketData);
addUpdateSuccessToast();
} catch (error) {
console.error('Error updating ticket:', error);
Expand Down
4 changes: 2 additions & 2 deletions src/people/widgetViews/PhasePlannerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from 'pages/tickets/style';
import { SOCKET_MSG } from 'config/socket';
import { createSocketInstance } from 'config/socket';
import { phasePlannerStore } from '../../store/phase';
import { phaseTicketStore } from '../../store/phase';
import {
FeatureHeadNameWrap,
FeatureHeadWrap,
Expand Down Expand Up @@ -149,7 +149,7 @@ const PhasePlannerView: React.FC = () => {

try {
await main.createUpdateTicket(initialTicketData);
phasePlannerStore.addTicket(initialTicketData as Ticket);
phaseTicketStore.addTicket(initialTicketData as Ticket);
setTicketData((prevTickets: TicketData[]) => [
...prevTickets,
{
Expand Down
30 changes: 26 additions & 4 deletions src/store/phase.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
import { makeAutoObservable } from 'mobx';
import { Ticket } from './interface';

export interface IPhasePlannerStore {
interface PhaseTickets {
[phase_uuid: string]: Ticket[];
}

export interface TicketStore {
tickets: Map<string, Ticket>;
phaseTickets: PhaseTickets;

addTicket: (ticket: Ticket) => void;
updateTicket: (uuid: string, ticket: Partial<Ticket>) => void;
getTicket: (uuid: string) => Ticket | undefined;
getPhaseTickets: (phase_uuid: string) => Ticket[];
}

export class PhasePlannerStore implements IPhasePlannerStore {
export class PhaseTicketStore implements TicketStore {
tickets: Map<string, Ticket> = new Map();
phaseTickets: PhaseTickets = {};

constructor() {
makeAutoObservable(this);
}

addTicket(ticket: Ticket) {
this.tickets.set(ticket.uuid, ticket);

const phaseTickets = this.phaseTickets[ticket.phase_uuid] || [];
this.phaseTickets[ticket.phase_uuid] = [...phaseTickets, ticket];
}

updateTicket(uuid: string, ticketUpdate: Partial<Ticket>) {
const existingTicket = this.tickets.get(uuid);
if (existingTicket) {
this.tickets.set(uuid, { ...existingTicket, ...ticketUpdate });
const updatedTicket = { ...existingTicket, ...ticketUpdate } as Ticket;

this.tickets.set(uuid, updatedTicket);

const phaseTickets = this.phaseTickets[existingTicket.phase_uuid] || [];
this.phaseTickets[existingTicket.phase_uuid] = phaseTickets.map((t: Ticket) =>
t.uuid === uuid ? updatedTicket : t
);
}
}

getTicket(uuid: string): Ticket | undefined {
return this.tickets.get(uuid);
}

getPhaseTickets(phase_uuid: string): Ticket[] {
return this.phaseTickets[phase_uuid] || [];
}
}

export const phasePlannerStore = new PhasePlannerStore();
export const phaseTicketStore = new PhaseTicketStore();

0 comments on commit 1df9bdf

Please sign in to comment.