Skip to content

Commit

Permalink
fix: handle active + source filter, reduce code
Browse files Browse the repository at this point in the history
  • Loading branch information
Saelmala committed Aug 23, 2024
1 parent 9ef0b99 commit 4b6d3ed
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 91 deletions.
21 changes: 0 additions & 21 deletions src/api/agileLive/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,6 @@ export async function getIngests(): Promise<ResourcesCompactIngestResponse[]> {
throw await response.json();
}

export async function getCompleteIngests(): Promise<ResourcesIngestResponse[]> {
const response = await fetch(
new URL(
AGILE_BASE_API_PATH + `/ingests?expand=true`,
process.env.AGILE_URL
),
{
headers: {
authorization: getAuthorizationHeader()
},
next: {
revalidate: 0
}
}
);
if (response.ok) {
return response.json();
}
throw await response.json();
}

export async function getIngest(
uuid: string
): Promise<ResourcesIngestResponse> {
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/manager/sources/resources/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server';
import { isAuthenticated } from '../../../../../api/manager/auth';
import { getCompleteIngests } from '../../../../../api/agileLive/ingest';
import { getIngests } from '../../../../../api/agileLive/ingest';


export async function GET(): Promise<NextResponse> {
Expand All @@ -11,7 +11,7 @@ export async function GET(): Promise<NextResponse> {
}

try {
return NextResponse.json(await getCompleteIngests());
return NextResponse.json(await getIngests());
} catch (e) {
return new NextResponse(e?.toString(), { status: 404 })
}
Expand Down
57 changes: 39 additions & 18 deletions src/components/filter/FilterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function FilterDropdown({
isLocationHidden,
showConfigSources,
selectedTags,
showConfigNdiType,
showConfigBmdType,
showConfigSrtType,
showNdiType,
showBmdType,
showSrtType,
setIsTypeHidden,
setIsLocationHidden,
setSelectedTags,
Expand All @@ -29,9 +29,9 @@ function FilterDropdown({
isLocationHidden: boolean;
showConfigSources: boolean;
selectedTags: Set<string>;
showConfigNdiType: boolean;
showConfigSrtType: boolean;
showConfigBmdType: boolean;
showNdiType: boolean;
showSrtType: boolean;
showBmdType: boolean;
setIsTypeHidden: React.Dispatch<React.SetStateAction<boolean>>;
setIsLocationHidden: React.Dispatch<React.SetStateAction<boolean>>;
setOnlyShowActiveSources: React.Dispatch<React.SetStateAction<boolean>>;
Expand Down Expand Up @@ -60,16 +60,34 @@ function FilterDropdown({
setOnlyShowConfigSources(!showConfigSources);
};

const showSelectedConfigNdiType = () => {
setOnlyShowNdiSources(!showConfigNdiType);
const showSelectedNdiType = () => {
if (!showNdiType) {
setOnlyShowNdiSources(true);
setOnlyShowBmdSources(false);
setOnlyShowSrtSources(false);
} else {
setOnlyShowNdiSources(false);
}
};

const showSelectedConfigBmdType = () => {
setOnlyShowBmdSources(!showConfigBmdType);
const showSelectedSrtType = () => {
if (!showSrtType) {
setOnlyShowSrtSources(true);
setOnlyShowNdiSources(false);
setOnlyShowBmdSources(false);
} else {
setOnlyShowSrtSources(false);
}
};

const showSelectedConfigSrtType = () => {
setOnlyShowSrtSources(!showConfigSrtType);
const showSelectedBmdType = () => {
if (!showBmdType) {
setOnlyShowBmdSources(true);
setOnlyShowNdiSources(false);
setOnlyShowSrtSources(false);
} else {
setOnlyShowBmdSources(false);
}
};

const deleteTag = (value: string) => {
Expand Down Expand Up @@ -122,6 +140,9 @@ function FilterDropdown({
const handleClear = () => {
setSelectedTags(new Set<string>());
setOnlyShowConfigSources(false);
setOnlyShowBmdSources(false);
setOnlyShowNdiSources(false);
setOnlyShowSrtSources(false);
};

const typesSearch = (event: ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -262,8 +283,8 @@ function FilterDropdown({
id="showNdiCheckbox"
type="checkbox"
className="flex ml-2 w-4 justify-center rounded-lg text-zinc-300"
checked={showConfigNdiType}
onChange={showSelectedConfigNdiType}
checked={showNdiType}
onChange={showSelectedNdiType}
/>
<label
className="ml-2 mt-1 text-left text-zinc-300"
Expand All @@ -277,8 +298,8 @@ function FilterDropdown({
id="showSelectedCheckbox"
type="checkbox"
className="flex ml-2 w-4 justify-center rounded-lg text-zinc-300"
checked={showConfigSrtType}
onChange={showSelectedConfigSrtType}
checked={showSrtType}
onChange={showSelectedSrtType}
/>
<label
className="ml-2 mt-1 text-left text-zinc-300"
Expand All @@ -292,8 +313,8 @@ function FilterDropdown({
id="showSelectedCheckbox"
type="checkbox"
className="flex ml-2 w-4 justify-center rounded-lg text-zinc-300"
checked={showConfigBmdType}
onChange={showSelectedConfigBmdType}
checked={showBmdType}
onChange={showSelectedBmdType}
/>
<label
className="ml-2 mt-1 text-left text-zinc-300"
Expand Down
57 changes: 29 additions & 28 deletions src/components/filter/FilterOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ function FilterOptions({ onFilteredSources }: FilterOptionsProps) {

handleSearch();
handleTags();
handleShowActiveSources();
filterOnSourceType();
filterSources();

onFilteredSources(tempSet);
tempSet.clear();
Expand Down Expand Up @@ -106,36 +105,38 @@ function FilterOptions({ onFilteredSources }: FilterOptionsProps) {
}
};

const handleShowActiveSources = () => {
if (onlyShowActiveSources) {
for (const source of tempSet.values()) {
if (source.status === 'gone') {
tempSet.delete(source._id.toString());
}
}
}
};

const filterOnSourceType = async () => {
const filterSources = async () => {
for (const source of tempSet.values()) {
const correspondingResource = resources.find(
(resource) => resource.name === source.ingest_source_name
);
if (correspondingResource) {
if (onlyShowNdiSources) {
if (correspondingResource.type.toUpperCase() !== 'NDI') {
tempSet.delete(source._id.toString());
}
let shouldDelete = false;
if (
onlyShowNdiSources &&
correspondingResource.type.toUpperCase() !== 'NDI'
) {
shouldDelete = true;
}
if (onlyShowBmdSources) {
if (correspondingResource.type.toUpperCase() !== 'BMD') {
tempSet.delete(source._id.toString());
}
if (
onlyShowBmdSources &&
correspondingResource.type.toUpperCase() !== 'BMD'
) {
shouldDelete = true;
}
if (
onlyShowSrtSources &&
correspondingResource.type.toUpperCase() !== 'SRT'
) {
shouldDelete = true;
}
if (onlyShowSrtSources) {
if (correspondingResource.type.toUpperCase() !== 'SRT') {
tempSet.delete(source._id.toString());
}

if (onlyShowActiveSources && source.status === 'gone') {
shouldDelete = true;
}

if (shouldDelete) {
tempSet.delete(source._id.toString());
}
}
}
Expand Down Expand Up @@ -171,9 +172,9 @@ function FilterOptions({ onFilteredSources }: FilterOptionsProps) {
setOnlyShowNdiSources={setOnlyShowNdiSources}
setOnlyShowBmdSources={setOnlyShowBmdSources}
setOnlyShowSrtSources={setOnlyShowSrtSources}
showConfigBmdType={onlyShowBmdSources}
showConfigNdiType={onlyShowNdiSources}
showConfigSrtType={onlyShowSrtSources}
showBmdType={onlyShowBmdSources}
showNdiType={onlyShowNdiSources}
showSrtType={onlyShowSrtSources}
/>
</div>
</ClickAwayListener>
Expand Down
33 changes: 11 additions & 22 deletions src/hooks/sources/useResources.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import {
ResourcesIngestResponse,
ResourcesSourceResponse
ResourcesSourceResponse,
ResourcesCompactIngestResponse
} from '../../../types/agile-live';
import { useState, useEffect } from 'react';

export function useResources() {
const [ingests, setIngests] = useState<ResourcesIngestResponse[]>([]);
const [ingests, setIngests] = useState<ResourcesCompactIngestResponse[]>([]);
const [resources, setResources] = useState<ResourcesSourceResponse[]>([]);

useEffect(() => {
let isMounted = true;

const fetchSources = async () => {
try {
const response = await fetch(`/api/manager/sources/resourceSource`, {
method: 'GET',
headers: [['x-api-key', `Bearer apisecretkey`]]
});

if (!response.ok) {
throw new Error('Error');
}

const getIngests = async () =>
await fetch(`/api/manager/sources/resources`, {
method: 'GET',
headers: [['x-api-key', `Bearer apisecretkey`]]
}).then(async (response) => {
const ing = await response.json();
if (isMounted) {
setIngests(ing);
}
} catch (e) {
console.log('ERROR');
}
};
fetchSources();

});
getIngests();
return () => {
isMounted = false;
};
Expand All @@ -42,11 +32,10 @@ export function useResources() {
for (let i = 0; i < ingests.length; i++) {
const id = ingests[i].uuid;
if (id) {
fetch(`/api/manager/resources/${id}`, {
fetch(`/api/manager/sources/resources/${id}`, {
method: 'GET',
headers: [['x-api-key', `Bearer apisecretkey`]]
}).then(async (response) => {
console.log('RESPONSE: ', response);
const sources = await response.json();
setResources(sources);
});
Expand Down

0 comments on commit 4b6d3ed

Please sign in to comment.