Skip to content

Commit

Permalink
Add areas to deck cards
Browse files Browse the repository at this point in the history
  • Loading branch information
mrica-equinor committed Nov 1, 2023
1 parent 345ca7d commit 37f815a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 15 deletions.
29 changes: 29 additions & 0 deletions backend/api/Controllers/AreaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,35 @@ public async Task<ActionResult<AreaResponse>> GetAreaById([FromRoute] string id)
}
}

/// <summary>
/// Lookup area by specified id.
/// </summary>
[HttpGet]
[Authorize(Roles = Role.Any)]
[Route("deck/{deckId}")]
[ProducesResponseType(typeof(AreaResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<IList<AreaResponse>>> GetAreaByDeckId([FromRoute] string deckId)
{
try
{
var areas = await _areaService.ReadByDeckId(deckId);
if (!areas.Any())
return NotFound($"Could not find area for deck with id {deckId}");

var response = areas.Select(area => new AreaResponse(area!));
return Ok(response);
}
catch (Exception e)
{
_logger.LogError(e, "Error during GET of areas from database");
throw;
}
}

/// <summary>
/// Lookup all the mission definitions related to an area
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions backend/api/Services/AreaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface IAreaService

public abstract Task<Area?> ReadById(string id);

public abstract Task<IEnumerable<Area?>> ReadByDeckId(string deckId);

public abstract Task<IEnumerable<Area>> ReadByInstallation(string installationCode);

public abstract Task<Area?> ReadByInstallationAndName(string installationCode, string areaName);
Expand Down Expand Up @@ -73,6 +75,17 @@ private IQueryable<Area> GetAreas()
.FirstOrDefaultAsync(a => a.Id.Equals(id));
}

public async Task<IEnumerable<Area?>> ReadByDeckId(string deckId)
{
if (deckId == null)
return new List<Area>();

return await _context.Areas.Where(a =>
a.Deck != null && a.Deck.Id.Equals(deckId)
).Include(a => a.SafePositions).Include(a => a.Installation)
.Include(a => a.Plant).Include(a => a.Deck).ToListAsync();
}

public async Task<Area?> ReadByInstallationAndName(Installation? installation, string areaName)
{
if (installation == null)
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/api/ApiCaller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ export class BackendAPICaller {
return result.content
}

static async getAreadByDeckID(deckId: string): Promise<Area[]> {
const path: string = 'areas/deck/' + deckId
const result = await this.GET<Area[]>(path).catch((e) => {
console.error(`Failed to GET /${path}: ` + e)
throw e
})
return result.content
}

static async getDecks(): Promise<Deck[]> {
const path: string = 'decks'
const result = await this.GET<Deck[]>(path).catch((e) => {
Expand Down
58 changes: 47 additions & 11 deletions frontend/src/components/Pages/InspectionPage/InspectionSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ const StyledDeckCards = styled.div`
`

const StyledDeckText = styled.div`
display: flex;
flex-direction: column;
gap: 6px;
display: grid;
grid-template-rows: 25px 35px;
align-self: stretch;
`
const StyledTopDeckText = styled.div`
Expand Down Expand Up @@ -71,7 +70,7 @@ const DeckCard = styled.div`
const StyledCircle = styled.div`
width: 13px;
height: 13px;
border-radius: 100px;
border-radius: 50px;
`

const StyledMissionComponents = styled.div`
Expand Down Expand Up @@ -124,6 +123,12 @@ export interface DeckMissionCount {
}
}

export interface DeckAreas {
[deckId: string]: {
areaString: string
}
}

export interface ScheduledMissionType {
[missionId: string]: boolean
}
Expand Down Expand Up @@ -168,6 +173,7 @@ export function InspectionSection({
const { installationCode } = useInstallationContext()
const [deckMissions, setDeckMissions] = useState<DeckMissionType>({})
const [selectedDeck, setSelectedDeck] = useState<Deck>()
const [areas, setAreas] = useState<DeckAreas>({})
const [selectedMissions, setSelectedMissions] = useState<CondensedMissionDefinition[]>()
const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false)
const [unscheduledMissions, setUnscheduledMissions] = useState<CondensedMissionDefinition[]>([])
Expand All @@ -187,7 +193,7 @@ export function InspectionSection({
useEffect(() => {
if (selectedMissions) {
let unscheduledMissions: CondensedMissionDefinition[] = []
selectedMissions!.forEach((mission) => {
selectedMissions.forEach((mission) => {
if (Object.keys(scheduledMissions).includes(mission.id) && scheduledMissions[mission.id])
setIsAlreadyScheduled(true)
else unscheduledMissions = unscheduledMissions.concat([mission])
Expand Down Expand Up @@ -253,6 +259,37 @@ export function InspectionSection({
return getDeadlineInspection(nextInspection.deadline)
}

useEffect(() => {
const newAreas: DeckAreas = {}

Object.keys(deckMissions).map((deckId) => {
let string: string = ''
let areaNames: String[] = []
BackendAPICaller.getAreadByDeckID(deckMissions[deckId].deck.id).then((areas) => {
areaNames = []
string = ''
if (areas != null) {
areas.forEach((area) => {
if (!areaNames.includes(area.areaName))
areaNames = areaNames.concat(area.areaName.toLocaleUpperCase())
})
areaNames = areaNames.sort()
console.log(areaNames)
areaNames.forEach((areaName) => {
console.log(areaName)
if (areaNames[areaNames.length - 1] == areaName) string = string + ' ' + areaName
else string = string + ' ' + areaName + ' |'
})

newAreas[deckMissions[deckId].deck.id] = {
areaString: string,
}
setAreas(newAreas)
}
})
})
}, [deckMissions])

return (
<>
<StyledDeckOverview>
Expand Down Expand Up @@ -291,6 +328,9 @@ export function InspectionSection({
</StyledContent>
))}
</StyledTopDeckText>
{Object.keys(areas).includes(deckId) && (
<Typography variant={'body_short'}>{areas[deckId].areaString}</Typography>
)}
{deckMissions[deckId].inspections && (
<CardMissionInformation deckId={deckId} deckMissions={deckMissions} />
)}
Expand All @@ -305,14 +345,10 @@ export function InspectionSection({
}
>
<Button
disabled={deckMissions[deckId].inspections.length > 0 ? false : true}
disabled={deckMissions[deckId].inspections.length === 0}
variant="outlined"
onClick={() => handleScheduleAll(deckMissions[deckId].inspections)}
style={
deckMissions[deckId].inspections.length > 0
? { borderColor: '#3D3D3D' }
: {}
}
color="secondary"
>
<Icon
name={Icons.LibraryAdd}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const StyledContent = styled.div`
const StyledCircle = styled.div`
width: 13px;
height: 13px;
border-radius: 100px;
border-radius: 50px;
`

interface IProps {
Expand Down Expand Up @@ -205,7 +205,7 @@ const InspectionRow = ({
}}
>
<StyledIcon
color={`${tokens.colors.interactive.focus.hex}`}
color={`${tokens.colors.interactive.focus.rgba}`}
name={Icons.AddOutlined}
size={24}
title={TranslateTextWithContext('Add to queue')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export const ScheduleMissionDialog = (props: IProps): JSX.Element => {
props.closeDialog()
}}
variant="outlined"
color="primary"
>
{' '}
{TranslateText('Cancel')}{' '}
Expand Down Expand Up @@ -211,7 +210,6 @@ export const AlreadyScheduledMissionDialog = (props: IScheduledProps): JSX.Eleme
props.closeDialog()
}}
variant="outlined"
color="primary"
>
{' '}
{TranslateText('Cancel')}{' '}
Expand Down

0 comments on commit 37f815a

Please sign in to comment.