Skip to content

Commit

Permalink
Select stakeholder if a user is part of multiple stakeholders (#10369)
Browse files Browse the repository at this point in the history
* Select stakeholder if a user is part of multiple stakeholders

* Avoid refresh of page

* Review changes

---------

Co-authored-by: Daniel M James <[email protected]>
  • Loading branch information
danieljames-dj and Daniel M James authored Dec 6, 2024
1 parent 1746776 commit 940a1f7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 25 deletions.
10 changes: 8 additions & 2 deletions app/webpacker/components/Tickets/TicketWorkbench.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import React from 'react';
import { ticketTypes } from '../../lib/wca-data.js.erb';
import EditPersonTicketWorkbench from './TicketWorkbenches/EditPersonTicketWorkbench';

export default function TicketWorkbench({ ticketDetails, sync }) {
export default function TicketWorkbench({ ticketDetails, sync, currentStakeholder }) {
const { ticket } = ticketDetails;

switch (ticket.metadata_type) {
case ticketTypes.edit_person:
return <EditPersonTicketWorkbench ticketDetails={ticketDetails} sync={sync} />;
return (
<EditPersonTicketWorkbench
ticketDetails={ticketDetails}
sync={sync}
currentStakeholder={currentStakeholder}
/>
);
default: return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,19 @@ function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, s
);
}

export default function EditPersonTicketWorkbench({ ticketDetails, sync }) {
const { requester_stakeholders: requesterStakeholders, ticket } = ticketDetails;

if (ticket.metadata.status === ticketStatuses.edit_person.closed) {
export default function EditPersonTicketWorkbench({ ticketDetails, sync, currentStakeholder }) {
if (ticketDetails.ticket.metadata.status === ticketStatuses.edit_person.closed) {
return null;
}

return requesterStakeholders.map((requesterStakeholder) => {
if (requesterStakeholder.stakeholder?.metadata?.friendly_id === 'wrt') {
return (
<EditPersonTicketWorkbenchForWrt
ticketDetails={ticketDetails}
actingStakeholderId={requesterStakeholder.id}
sync={sync}
/>
);
}
return null;
});
if (currentStakeholder.stakeholder?.metadata?.friendly_id === 'wrt') {
return (
<EditPersonTicketWorkbenchForWrt
ticketDetails={ticketDetails}
actingStakeholderId={currentStakeholder.id}
sync={sync}
/>
);
}
return null;
}
97 changes: 90 additions & 7 deletions app/webpacker/components/Tickets/index.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,109 @@
import React from 'react';
import { Container } from 'semantic-ui-react';
import React, { useMemo, useState } from 'react';
import {
Button, Container, Dropdown, Message, Modal,
} from 'semantic-ui-react';
import useLoadedData from '../../lib/hooks/useLoadedData';
import { actionUrls } from '../../lib/requests/routes.js.erb';
import Loading from '../Requests/Loading';
import Errored from '../Requests/Errored';
import TicketHeader from './TicketHeader';
import TicketWorkbench from './TicketWorkbench';
import TicketLogs from './TicketLogs';
import useInputState from '../../lib/hooks/useInputState';

function SkateholderSelector({ stakeholderList, setUserSelectedStakeholder }) {
const [selectedOption, setSelectedOption] = useInputState(stakeholderList[0]);
return (
<>
<p>
You are part of more than one stakeholder, please select the stakeholder as which you want
to visit the ticket page.
</p>
<Dropdown
options={stakeholderList.map((requesterStakeholder) => ({
key: requesterStakeholder.id,
text: requesterStakeholder.stakeholder.name,
value: requesterStakeholder,
}))}
value={selectedOption}
onChange={setSelectedOption}
/>
<Button
disabled={!selectedOption}
onClick={() => setUserSelectedStakeholder(selectedOption)}
>
Select
</Button>
</>
);
}

function TicketContent({ ticketDetails, currentStakeholder, sync }) {
return (
<>
<TicketHeader ticketDetails={ticketDetails} />
<TicketWorkbench
ticketDetails={ticketDetails}
sync={sync}
currentStakeholder={currentStakeholder}
/>
<TicketLogs logs={ticketDetails.ticket.ticket_logs} />
</>
);
}

export default function Tickets({ id }) {
const {
data: ticketDetails, sync, loading, error,
} = useLoadedData(actionUrls.tickets.show(id));

const [userSelectedStakeholder, setUserSelectedStakeholder] = useInputState();
const currentStakeholder = useMemo(() => {
if (userSelectedStakeholder) {
return userSelectedStakeholder;
} if (ticketDetails?.requester_stakeholders.length === 1) {
return ticketDetails?.requester_stakeholders[0];
}
return null;
}, [ticketDetails?.requester_stakeholders, userSelectedStakeholder]);

if (loading) return <Loading />;
if (error) return <Errored />;

return (
<Container fluid>
<TicketHeader ticketDetails={ticketDetails} />
<TicketWorkbench ticketDetails={ticketDetails} sync={sync} />
<TicketLogs logs={ticketDetails.ticket.ticket_logs} />
</Container>
<>
<Container fluid>
{userSelectedStakeholder && (
<Message>
{`You are currently viewing the ticket as stakeholder "${userSelectedStakeholder.stakeholder.name}".`}
<Button
onClick={() => setUserSelectedStakeholder(false)}
>
Click here to change
</Button>
</Message>
)}
{currentStakeholder && (
<TicketContent
ticketDetails={ticketDetails}
currentStakeholder={currentStakeholder}
sync={sync}
/>
)}
</Container>
<Modal
open={!currentStakeholder}
closeOnEscape={false}
closeOnDimmerClick={false}
>
<Modal.Header>Select stakeholder</Modal.Header>
<Modal.Content>
<SkateholderSelector
stakeholderList={ticketDetails?.requester_stakeholders}
setUserSelectedStakeholder={setUserSelectedStakeholder}
/>
</Modal.Content>
</Modal>
</>
);
}

0 comments on commit 940a1f7

Please sign in to comment.