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

Fix todo tags && fix handling error while copying #100

Merged
merged 4 commits into from
Mar 22, 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
1 change: 0 additions & 1 deletion src/features/settings/settingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export const useSettingsManager = () => {
signOut(auth);
} catch (error) {
toast.error('Error deleting account');
console.error(error);
}
setIsRemoving(false);
};
Expand Down
4 changes: 1 addition & 3 deletions src/features/surveys/components/SurveyRow/SurveyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function SurveyRow({
endDate,
id,
}: SurveyRowProps) {
const [, copy] = useCopyToClipboard();
const { copy } = useCopyToClipboard();
const navigate = useRouter();
const [isOpen, setIsOpen] = useState(false);
const [isRemoving, setIsRemoving] = useState(false);
Expand All @@ -43,7 +43,6 @@ export default function SurveyRow({
window.location.hostname === 'localhost' ? 'http://' : 'https://';
const link = `${domain}${window.location.host}/survey/${id}`;
copy(link);
toast.success('Link copied to clipboard');
};

const handleOnMoreButton = () => {
Expand All @@ -58,7 +57,6 @@ export default function SurveyRow({
toast.success('Survey deleted');
} catch (error) {
toast.error('Error deleting survey');
console.error(error);
}
setIsRemoving(false);
};
Expand Down
11 changes: 7 additions & 4 deletions src/features/surveys/managers/createSurveyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const useCreateSurveyManager = () => {
const [isCreating, setIsCreating] = useState(false);

const router = useRouter();
const [, copy] = useCopyToClipboard();
const { copy } = useCopyToClipboard();

// Move to useState initial value when bug in emoji library will be solved:
// https://github.com/ealush/emoji-picker-react/issues/329
Expand Down Expand Up @@ -61,10 +61,13 @@ export const useCreateSurveyManager = () => {
const domain =
window.location.hostname === 'localhost' ? 'http://' : 'https://';
const link = `${domain}${window.location.host}/survey/${newSurvey.id}`;
copy(link);
const copiedCorrectly = await copy(link, true);
await router.push(`/survey/answer/${newSurvey.id}`);

toast.success('Survey created and link copied to clipboard');
toast.success(
`Survey created succesfully ${
copiedCorrectly ? 'and link copied to clipboard' : ''
}`
);
} catch (error) {
toast.error('Survey creation failed');
}
Expand Down
3 changes: 1 addition & 2 deletions src/features/surveys/managers/surveyResultsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useSurveyResultsManager = () => {
const [startDate, setStartDate] = useState('-');
const [endDate, setEndDate] = useState('-');
const [answersData, setAnswersData] = useState<AnswerData[]>([]);
const [, copy] = useCopyToClipboard();
const { copy } = useCopyToClipboard();

const getSurveyData = useCallback(
async (displayMessages = false) => {
Expand Down Expand Up @@ -121,7 +121,6 @@ export const useSurveyResultsManager = () => {
window.location.hostname === 'localhost' ? 'http://' : 'https://';
const link = `${domain}${window.location.host}/survey/${id}`;
copy(link);
toast.success('Link copied to clipboard');
};

return {
Expand Down
14 changes: 6 additions & 8 deletions src/pages/surveys/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ function SurveyListPage() {

<div className="flex flex-col items-center justify-center">
<div>
{
// TODO: add user friendly error message
error && <strong>Error: {JSON.stringify(error)}</strong>
}
{
// TODO: add fancy loading
loading && <Loader isLoading={true} />
}
{error && (
<strong>
There is a problem - your surveys cannot be viewed.
</strong>
)}
{loading && <Loader isLoading={true} />}
</div>
{surveysCollection &&
(surveysCollection.docs?.length > 0 ? (
Expand Down
19 changes: 10 additions & 9 deletions src/shared/hooks/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { useState } from 'react';
import toast from 'react-hot-toast';

type CopiedValue = string | null;
type CopyFn = (text: string) => Promise<boolean>;
function useCopyToClipboard() {
const [copiedText, setCopiedText] = useState<string | null>(null);

function useCopyToClipboard(): [CopiedValue, CopyFn] {
const [copiedText, setCopiedText] = useState<CopiedValue>(null);

const copy: CopyFn = async (text) => {
const copy = async (text: string, silient = false) => {
if (!navigator?.clipboard) {
console.warn('Clipboard not supported');
toast.error('Clipboard not supported');
return false;
}

try {
await navigator.clipboard.writeText(text);
setCopiedText(text);
if (!silient) {
toast.success('Copied to clipboard');
}
return true;
} catch (error) {
console.warn('Copy failed', error);
toast.error('Copy failed');
setCopiedText(null);
return false;
}
};

return [copiedText, copy];
return { copiedText, copy };
}

export default useCopyToClipboard;