Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: changed toast to have centralized state management and sing… #1112

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { useState } from 'react';
import { ToastProps } from '@components/toast/Toast';
import AlertCircleIcon from '../../icons/AlertCircleIcon';
import Button from '../../button/Button';
import { CloudAccount } from '../hooks/useCloudAccounts/useCloudAccount';
import settingsService from '../../../services/settingsService';
import { ToastProps } from '../../toast/hooks/useToast';

interface CloudAccountDeleteContentsProps {
cloudAccount: CloudAccount;
onCancel: () => void;
handleAfterDelete: (account: CloudAccount) => void;
setToast: (toast: ToastProps) => void;
showToast: (toast: ToastProps) => void;
}

function CloudAccountDeleteContents({
cloudAccount,
onCancel,
handleAfterDelete,
setToast
showToast
}: CloudAccountDeleteContentsProps) {
const [loading, setLoading] = useState(false);

Expand All @@ -28,14 +28,14 @@ function CloudAccountDeleteContents({
settingsService.deleteCloudAccount(cloudAccount.id).then(res => {
setLoading(false);
if (res === Error) {
setToast({
showToast({
hasError: true,
title: 'Cloud account was not deleted',
message:
'There was an error deleting this cloud account. Please try again.'
});
} else {
setToast({
showToast({
hasError: false,
title: 'Cloud account deleted',
message: `The cloud account was successfully deleted!`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MongoDbAtlasAccountDetails from '@components/account-details/MongoDBAtlas
import OciAccountDetails from '@components/account-details/OciAccountDetails';
import ScalewayAccountDetails from '@components/account-details/ScalewayAccountDetails';
import { getPayloadFromForm } from '@utils/cloudAccountHelpers';
import { ToastProps } from '@components/toast/Toast';
import providers, {
allProviders,
Provider
Expand All @@ -24,7 +25,6 @@ import {
CloudAccount,
CloudAccountsPage
} from '../hooks/useCloudAccounts/useCloudAccount';
import { ToastProps } from '../../toast/hooks/useToast';
import settingsService from '../../../services/settingsService';

interface CloudAccountsSidePanelProps {
Expand All @@ -36,7 +36,7 @@ interface CloudAccountsSidePanelProps {
handleAfterDelete: (account: CloudAccount) => void;
page: CloudAccountsPage;
goTo: (page: CloudAccountsPage) => void;
setToast: (toast: ToastProps) => void;
showToast: (toast: ToastProps) => void;
}

function AccountDetails({
Expand Down Expand Up @@ -81,7 +81,7 @@ function CloudAccountsSidePanel({
handleAfterDelete,
page,
goTo,
setToast
showToast
}: CloudAccountsSidePanelProps) {
const [isDeleteOpen, setIsDeleteOpen] = useState(false);
const [loading, setLoading] = useState(false);
Expand All @@ -101,15 +101,15 @@ function CloudAccountsSidePanel({
settingsService.editCloudAccount(id, payloadJson).then(res => {
if (res === Error || res.error) {
setLoading(false);
setToast({
showToast({
hasError: true,
title: 'Cloud account not edited',
message:
'There was an error editing this cloud account. Refer to the logs and try again.'
});
} else {
setLoading(false);
setToast({
showToast({
hasError: false,
title: 'Cloud account edited',
message: `The cloud account was successfully edited!`
Expand Down Expand Up @@ -182,7 +182,7 @@ function CloudAccountsSidePanel({
setIsDeleteOpen(false);
closeModal();
}}
setToast={setToast}
showToast={showToast}
/>
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function InventoryFilterDropdown({
toggle,
closeDropdownAfterAdd
}: InventoryFilterDropdownProps) {
const { setSkippedSearch, router, setToast } = useInventory();
const { setSkippedSearch, router, showToast } = useInventory();

const {
// toggle,
Expand Down Expand Up @@ -93,7 +93,7 @@ export default function InventoryFilterDropdown({
handleValueCheck={handleValueCheck}
handleValueInput={handleValueInput}
cleanValues={cleanValues}
setToast={setToast}
showToast={showToast}
costBetween={costBetween}
handleCostBetween={handleCostBetween}
/>
Expand Down
8 changes: 4 additions & 4 deletions dashboard/components/export-csv/ExportCSV.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ToastProps } from '@components/toast/Toast';
import { useRouter } from 'next/router';
import settingsService from '../../services/settingsService';
import { ToastProps } from '../toast/hooks/useToast';
import ExportCSVButton from './ExportCSVButton';

type ExportCSVProps = {
setToast: (toast: ToastProps | undefined) => void;
showToast: (toast: ToastProps) => void;
};

function ExportCSV({ setToast }: ExportCSVProps) {
function ExportCSV({ showToast }: ExportCSVProps) {
const router = useRouter();

function exportCSV(id?: string) {
settingsService.exportCSV(id);
setToast({
showToast({
hasError: false,
title: 'CSV exported',
message: 'The download of the CSV file should begin shortly.'
Expand Down
17 changes: 8 additions & 9 deletions dashboard/components/feedback-widget/FeedbackWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Modal from '@components/modal/Modal';
import Input from '@components/input/Input';
import settingsService from '@services/settingsService';
import Button from '@components/button/Button';
import useToast from '@components/toast/hooks/useToast';
import Toast from '@components/toast/Toast';
import { useToast } from '@components/toast/ToastProvider';

// We define the placeholder here for convenience
// It's difficult to read when passed inline
Expand Down Expand Up @@ -54,7 +54,7 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
const [isTakingScreenCapture, setIsTakingScreenCapture] = useState(false);
const [fileAttachement, setFileAttachement] = useState<File | null>(null);
const [isSendingFeedback, setIsSendingFeedback] = useState(false);
const { toast, setToast, dismissToast } = useToast();
const { toast, showToast, dismissToast } = useToast();

async function takeScreenshot() {
if (
Expand Down Expand Up @@ -85,15 +85,15 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
setFileAttachement(screenShotFile);
}

setToast({
showToast({
hasError: false,
title: 'Screen capture',
message:
'A screenshot of your current page on Komiser has been captured and attached to your feedback.'
});
})
.catch(err => {
setToast({
showToast({
hasError: true,
title: 'Screen capture failed',
message:
Expand Down Expand Up @@ -126,7 +126,7 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
settingsService
.sendFeedback(formData)
.then(result => {
setToast({
showToast({
hasError: false,
title: 'Feedback sent',
message:
Expand All @@ -136,7 +136,7 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
clearFeedbackForm();
})
.catch(error => {
setToast({
showToast({
hasError: true,
title: 'Feedback',
message: 'An Error happened. Maybe try again please!'
Expand Down Expand Up @@ -267,14 +267,14 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
hoverTitle="drop here"
maxSize={MAX_FILE_SIZE_MB}
onTypeError={(err: string) =>
setToast({
showToast({
hasError: true,
title: 'File upload failed',
message: err
})
}
onSizeError={(err: string) =>
setToast({
showToast({
hasError: true,
title: 'File upload failed',
message: err
Expand Down Expand Up @@ -459,7 +459,6 @@ const useFeedbackWidget = (defaultState: boolean = false) => {
</form>
</div>
</Modal>
{toast && <Toast {...toast} dismissToast={dismissToast} />}
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function InventoryFilterDropdown({
toggle,
closeDropdownAfterAdd
}: InventoryFilterDropdownProps) {
const { setSkippedSearch, router, setToast } = useInventory();
const { setSkippedSearch, router, showToast } = useInventory();

const {
// toggle,
Expand Down Expand Up @@ -91,7 +91,7 @@ export default function InventoryFilterDropdown({
handleValueCheck={handleValueCheck}
handleValueInput={handleValueInput}
cleanValues={cleanValues}
setToast={setToast}
showToast={showToast}
costBetween={costBetween}
handleCostBetween={handleCostBetween}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { ToastProps } from '@components/toast/Toast';
import ExportCSV from '../../export-csv/ExportCSV';
import CloseIcon from '../../icons/CloseIcon';
import SearchIcon from '../../icons/SearchIcon';
import { ToastProps } from '../../toast/hooks/useToast';

type InventorySearchBarProps = {
query: string;
setQuery: (query: string) => void;
error: boolean;
setToast: (toast: ToastProps | undefined) => void;
showToast: (toast: ToastProps) => void;
};

function InventorySearchBar({
query,
setQuery,
error,
setToast
showToast
}: InventorySearchBarProps) {
return (
<>
Expand Down Expand Up @@ -45,7 +45,7 @@ function InventorySearchBar({
maxLength={64}
/>
<div className="absolute right-4 top-[14px]">
<ExportCSV setToast={setToast} />
<ExportCSV showToast={showToast} />
</div>
</div>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ToastProps } from '@components/toast/Toast';
import { NextRouter } from 'next/router';
import { ChangeEvent } from 'react';
import formatNumber from '../../../utils/formatNumber';
import providers from '../../../utils/providerHelper';
import Checkbox from '../../checkbox/Checkbox';
import SkeletonInventory from '../../skeleton/SkeletonInventory';
import { ToastProps } from '../../toast/hooks/useToast';
import {
InventoryItem,
InventoryStats
Expand Down Expand Up @@ -32,7 +32,7 @@ type InventoryTableProps = {
searchedLoading: boolean;
hideResourceFromCustomView: () => void;
hideResourcesLoading: boolean;
setToast: (toast: ToastProps | undefined) => void;
showToast: (toast: ToastProps) => void;
};

function InventoryTable({
Expand All @@ -52,7 +52,7 @@ function InventoryTable({
searchedLoading,
hideResourceFromCustomView,
hideResourcesLoading,
setToast
showToast
}: InventoryTableProps) {
return (
<>
Expand All @@ -63,7 +63,7 @@ function InventoryTable({
query={query}
setQuery={setQuery}
error={error}
setToast={setToast}
showToast={showToast}
/>
<div className="rounded-lg rounded-t-none pb-6">
<table className="w-full table-auto bg-white text-left text-sm text-gray-900">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ToastProps } from '@components/toast/Toast';
import { ChangeEvent, useEffect, useState } from 'react';
import settingsService from '../../../../services/settingsService';
import Checkbox from '../../../checkbox/Checkbox';
import Input from '../../../input/Input';
import { ToastProps } from '../../../toast/hooks/useToast';
import { InventoryFilterData } from '../../hooks/useInventory/types/useInventoryTypes';
import { CostBetween } from './hooks/useFilterWizard';

Expand All @@ -14,7 +14,7 @@ type InventoryFilterValueProps = {
) => void;
handleValueInput: (newValue: { values: string }) => void;
cleanValues: () => void;
setToast: (toast: ToastProps | undefined) => void;
showToast: (toast: ToastProps) => void;
costBetween: CostBetween;
handleCostBetween: (newValue: Partial<CostBetween>) => void;
};
Expand All @@ -26,7 +26,7 @@ function InventoryFilterValue({
handleValueCheck,
handleValueInput,
cleanValues,
setToast,
showToast,
costBetween,
handleCostBetween
}: InventoryFilterValueProps) {
Expand All @@ -45,7 +45,7 @@ function InventoryFilterValue({
if (data.field === 'provider') {
settingsService.getProviders().then(res => {
if (res === Error) {
setToast({
showToast({
hasError: true,
title: `There was an error when fetching the cloud providers`,
message: `Please refresh the page and try again.`
Expand All @@ -59,7 +59,7 @@ function InventoryFilterValue({
if (data.field === 'account') {
settingsService.getAccounts().then(res => {
if (res === Error) {
setToast({
showToast({
hasError: true,
title: `There was an error when fetching the cloud accounts`,
message: `Please refresh the page and try again.`
Expand All @@ -73,7 +73,7 @@ function InventoryFilterValue({
if (data.field === 'region') {
settingsService.getRegions().then(res => {
if (res === Error) {
setToast({
showToast({
hasError: true,
title: `There was an error when fetching the cloud regions`,
message: `Please refresh the page and try again.`
Expand All @@ -87,7 +87,7 @@ function InventoryFilterValue({
if (data.field === 'service') {
settingsService.getServices().then(res => {
if (res === Error) {
setToast({
showToast({
hasError: true,
title: `There was an error when fetching the cloud services`,
message: `Please refresh the page and try again.`
Expand Down
Loading