Skip to content

Commit

Permalink
Merge pull request #1493 from Vizzuality/MRXN23-439-move-export-solut…
Browse files Browse the repository at this point in the history
…ions-btn

FE (fix): move export solutions [MRXN23-439]
  • Loading branch information
andresgnlez authored Sep 11, 2023
2 parents a9a6005 + 2cf41c9 commit 97406c5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 63 deletions.
56 changes: 50 additions & 6 deletions app/layout/project/sidebar/project/scenarios-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useScenarios,
useDuplicateScenario,
useCancelRunScenario,
useDownloadSolutionsSummary,
} from 'hooks/scenarios';
import { useToasts } from 'hooks/toast';

Expand All @@ -28,6 +29,7 @@ import { ScrollArea } from 'components/scroll-area';
import HelpBeacon from 'layout/help/beacon';
import NoResults from 'layout/project/sidebar/project/inventory-panel/components/no-results';
import ScenarioToolbar from 'layout/project/sidebar/project/scenarios-list/toolbar';
import Section from 'layout/section';
import { cn } from 'utils/cn';

import bgScenariosDashboard from 'images/new-layout/bg-scenarios-dashboard.png';
Expand All @@ -42,8 +44,9 @@ export const ScenariosList: React.FC = () => {
const queryClient = useQueryClient();
const { addToast } = useToasts();

const [modal, setModal] = useState(false);
const [modal, setModal] = useState<boolean>(false);
const [deleteScenario, setDelete] = useState(null);
const [solutionsReportLoader, setSolutionsReportLoader] = useState<boolean>(false);

const { search, filters, sort } = useSelector((state) => state['/projects/[id]']);

Expand Down Expand Up @@ -187,6 +190,33 @@ export const ScenariosList: React.FC = () => {
[addToast, cancelRunMutation]
);

const downloadSolutionsSummary = useDownloadSolutionsSummary();

const onDownloadSolutionsSummary = useCallback(() => {
setSolutionsReportLoader(true);

downloadSolutionsSummary.mutate(
{ id: pid },
{
onError: () => {
addToast(
'download-error',
<>
<h2 className="font-medium">Error!</h2>
<ul className="text-sm">Solutions report not downloaded</ul>
</>,
{
level: 'error',
}
);
},
onSettled: () => {
setSolutionsReportLoader(false);
},
}
);
}, [downloadSolutionsSummary, addToast, pid]);

return (
<AnimatePresence>
<div
Expand All @@ -204,7 +234,6 @@ export const ScenariosList: React.FC = () => {
className="relative flex flex-grow flex-col space-y-3 overflow-hidden"
>
{(hasScenarios || search || hasFilters) && <ScenarioToolbar />}

<div
className={cn({
'relative overflow-hidden': true,
Expand Down Expand Up @@ -260,7 +289,6 @@ export const ScenariosList: React.FC = () => {
</ScrollArea>
)}
</div>

{!hasScenarios && !search && !hasFilters && scenariosIsFetched && (
<div className="flex flex-col space-y-2">
<motion.div
Expand Down Expand Up @@ -323,18 +351,34 @@ export const ScenariosList: React.FC = () => {
)}

{(hasScenarios || search || hasFilters) && (
<div className="flex w-full flex-shrink-0 items-center justify-center rounded-[20px] bg-gray-700 p-6">
<Section className="flex w-full flex-col items-center justify-center space-y-5">
{hasScenarios && (
<Button
theme="primary-alt"
size="base"
className="flex w-full overflow-hidden uppercase"
disabled={solutionsReportLoader}
onClick={onDownloadSolutionsSummary}
>
<Loading
visible={solutionsReportLoader}
className="absolute bottom-0 left-0 right-0 top-0 z-40 flex h-full w-full items-center justify-center bg-gray-800 bg-opacity-90"
iconClassName="w-10 h-10 text-primary-500"
/>
{`Export scenario${scenariosData.length > 1 ? 's' : ''} data`}
</Button>
)}
<Button
theme="primary"
size="base"
className="!px-5"
className="px-5"
disabled={!editable}
onClick={() => setModal(true)}
>
<span className="mr-5">Create scenario</span>
<Icon icon={PLUS_SVG} className="h-4 w-4" />
</Button>
</div>
</Section>
)}
</div>

Expand Down
59 changes: 5 additions & 54 deletions app/layout/project/sidebar/scenario/solutions/overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,27 @@ import { useRouter } from 'next/router';
import { getScenarioEditSlice } from 'store/slices/scenarios/edit';

import { motion } from 'framer-motion';
import { HiOutlineTable } from 'react-icons/hi';

import { useProject } from 'hooks/projects';
import {
useScenario,
useDownloadScenarioReport,
useDownloadSolutionsSummary,
} from 'hooks/scenarios';
import { useScenario, useDownloadScenarioReport } from 'hooks/scenarios';
import { useSolution, useBestSolution } from 'hooks/solutions';
import { useToasts } from 'hooks/toast';

import Button from 'components/button';
import Icon from 'components/icon';
import Loading from 'components/loading';
import Modal from 'components/modal';
import Section from 'layout/section';
import SolutionSelected from 'layout/solutions/selected';
import { formatFileName } from 'utils/units';

import TABLE_SVG from 'svgs/ui/table.svg?sprite';

import SolutionsTableForm from './table/table-form/component';

export const SolutionsOverview = (): JSX.Element => {
const { query } = useRouter();
const { pid, sid } = query as { pid: string; sid: string };

const [PDFLoader, setPDFLoader] = useState<boolean>(false);
const [solutionsReportLoader, setSolutionsReportLoader] = useState<boolean>(false);
const [showTable, setShowTable] = useState<boolean>(false);
const { addToast } = useToasts();

Expand Down Expand Up @@ -71,8 +64,6 @@ export const SolutionsOverview = (): JSX.Element => {
runId: `${(selectedSolutionData || bestSolutionData)?.runId}`,
});

const downloadSolutionsSummary = useDownloadSolutionsSummary();

const SOLUTION_DATA = selectedSolutionData || bestSolutionData;

const isBestSolution =
Expand All @@ -83,31 +74,6 @@ export const SolutionsOverview = (): JSX.Element => {
(bestSolutionisFetching && !bestSolutionisFetched) ||
(selectedSolutionisFetching && !selectedSolutionisFetched);

const onDownloadSolutionsSummary = useCallback(() => {
setSolutionsReportLoader(true);

downloadSolutionsSummary.mutate(
{ id: pid },
{
onError: () => {
addToast(
'download-error',
<>
<h2 className="font-medium">Error!</h2>
<ul className="text-sm">Solutions report not downloaded</ul>
</>,
{
level: 'error',
}
);
},
onSettled: () => {
setSolutionsReportLoader(false);
},
}
);
}, [downloadSolutionsSummary, addToast, pid]);

const onDownloadReport = useCallback(() => {
setPDFLoader(true);
addToast(
Expand Down Expand Up @@ -211,22 +177,7 @@ export const SolutionsOverview = (): JSX.Element => {
<Button
theme="primary-alt"
size="base"
className="mb-4 flex h-12 overflow-hidden uppercase"
disabled={solutionsReportLoader}
onClick={onDownloadSolutionsSummary}
>
<Loading
visible={solutionsReportLoader}
className="absolute bottom-0 left-0 right-0 top-0 z-40 flex h-full w-full items-center justify-center bg-gray-800 bg-opacity-90"
iconClassName="w-10 h-10 text-primary-500"
/>
Export solutions summary
</Button>

<Button
theme="primary-alt"
size="base"
className="mb-4 flex h-12 overflow-hidden uppercase"
className="relative overflow-hidden uppercase"
disabled={PDFLoader}
onClick={onDownloadReport}
>
Expand All @@ -240,11 +191,11 @@ export const SolutionsOverview = (): JSX.Element => {
<Button
theme="primary"
size="base"
className="mb-4 flex h-12 uppercase"
className="relative uppercase"
onClick={() => setShowTable(true)}
>
View solutions table
<Icon icon={TABLE_SVG} className="absolute right-8 h-4 w-4" />
<HiOutlineTable className="absolute right-8 h-6 w-6" />
</Button>

<Modal
Expand Down
3 changes: 0 additions & 3 deletions app/svgs/ui/table.svg

This file was deleted.

0 comments on commit 97406c5

Please sign in to comment.