-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from perimetre/dev
Promoting Dev
- Loading branch information
Showing
13 changed files
with
252 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { useCallback, useMemo, useRef, useState } from 'react'; | ||
import { FilePlus, X } from 'react-feather'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { CreateListMutation, useCreateListMutation } from '../../../apollo/generated/graphql'; | ||
import env from '../../../env'; | ||
import { CatchGraphqlError, getLocaleIdFromGraphqlError } from '../../../lib/apollo/exceptions'; | ||
import Button from '../../UI/Button'; | ||
import ReactPortal from '../../UI/ReactPortal'; | ||
import Spinner from '../../UI/Spinner'; | ||
import Toastr, { ToastrRef } from '../../UI/Toastr'; | ||
|
||
type CreateListButtonProps = { | ||
projectId: number; | ||
}; | ||
|
||
const CreateListButton: React.FC<CreateListButtonProps> = ({ projectId }) => { | ||
// *********** | ||
// ** Grapqhl declarations | ||
// *********** | ||
|
||
// ** Mutations | ||
const [createList, { loading: createListLoading }] = useCreateListMutation(); | ||
|
||
// *********** | ||
// ** Business logic | ||
// *********** | ||
|
||
const { NEXT_PUBLIC_MARATHON_API, NEXT_PUBLIC_MARATHON_API_LIST } = useMemo(env, []); | ||
|
||
const [createListError, setCreateListError] = useState<string | undefined>(); | ||
const [lastCreatedList, setLastCreatedList] = useState<CreateListMutation | undefined>(undefined); | ||
const toastRef = useRef<ToastrRef>(null); | ||
|
||
const listUrl = useMemo(() => { | ||
if (!NEXT_PUBLIC_MARATHON_API_LIST || !NEXT_PUBLIC_MARATHON_API) return; | ||
|
||
const url = new URL(NEXT_PUBLIC_MARATHON_API_LIST, NEXT_PUBLIC_MARATHON_API); | ||
|
||
if (lastCreatedList?.createList?.externalId) { | ||
url.searchParams.append('oid', lastCreatedList.createList.externalId); | ||
} | ||
|
||
return url.toString(); | ||
}, [NEXT_PUBLIC_MARATHON_API, NEXT_PUBLIC_MARATHON_API_LIST, lastCreatedList]); | ||
|
||
const handleCreateList = useCallback(async () => { | ||
setLastCreatedList(undefined); | ||
if (projectId) { | ||
try { | ||
const { data: result } = await createList({ | ||
variables: { | ||
projectId | ||
} | ||
}); | ||
|
||
setLastCreatedList(result || undefined); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (err: any) { | ||
const { graphQLErrors, networkError }: CatchGraphqlError = err; | ||
// If failed we'll get here | ||
// Get the locale id for the error | ||
const error = getLocaleIdFromGraphqlError(graphQLErrors, networkError); | ||
|
||
// Update the error state, which will display the error | ||
if (error) { | ||
toastRef.current?.open(3, () => { | ||
setCreateListError(undefined); | ||
}); | ||
setCreateListError(error); | ||
} | ||
} | ||
} | ||
}, [createList, projectId]); | ||
|
||
return ( | ||
<> | ||
<Button className="whitespace-pre group" onClick={handleCreateList} disabled={createListLoading}> | ||
{createListLoading ? ( | ||
<Spinner className="w-6 h-6" /> | ||
) : ( | ||
<> | ||
<span> | ||
<FormattedMessage id="cart.createList" /> | ||
</span> | ||
<FilePlus className="text-2xl" /> | ||
</> | ||
)} | ||
</Button> | ||
<ReactPortal selector="#cart-header"> | ||
{!!lastCreatedList && ( | ||
<div className="container p-4 mx-auto mt-12 bg-white border border-gray-200 border-solid rounded print:hidden"> | ||
<div className="flex items-center justify-between w-full"> | ||
<p> | ||
<FormattedMessage | ||
id="cart.listCreatedSuccessfully" | ||
values={{ | ||
name: lastCreatedList?.createList?.name, | ||
bold: (msg: string) => <span className="font-bold">{msg}</span>, | ||
url: (msg: string) => ( | ||
<a className="text-mui-primary hover:underline" href={listUrl} target="_blank" rel="noreferrer"> | ||
{msg} | ||
</a> | ||
) | ||
}} | ||
/> | ||
</p> | ||
<button className="p-4 mui-btn-icon text-mui-paragraph-900" onClick={() => setLastCreatedList(undefined)}> | ||
<X className="mui-animate-scaleHover-target" /> | ||
</button> | ||
</div> | ||
</div> | ||
)} | ||
</ReactPortal> | ||
<Toastr ref={toastRef} variant="error"> | ||
{createListError && <FormattedMessage id={createListError} />} | ||
</Toastr> | ||
</> | ||
); | ||
}; | ||
|
||
export default CreateListButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { forwardRef, useImperativeHandle, useState } from 'react'; | ||
import ReactPortal from '../ReactPortal'; | ||
import classnames from 'classnames'; | ||
|
||
const placementClassnameMap = { | ||
// top: 'top-0', | ||
'top-start': 'top-0 left-0', | ||
'top-end': 'top-0 right-0', | ||
// right: '', | ||
'right-start': 'top-0 right-0', | ||
'right-end': 'bottom-0 right-0', | ||
// bottom: '', | ||
'bottom-start': 'bottom-0 left-0', | ||
'bottom-end': 'bottom-0 right-0', | ||
// left: '', | ||
'left-start': 'top-0 left-0', | ||
'left-end': 'bottom-0 left-0' | ||
}; | ||
|
||
const variantClassnameMap = { | ||
default: 'bg-white border-gray-200', | ||
error: 'bg-mui-error font-bold text-white' | ||
}; | ||
|
||
export type ToastrRef = { | ||
open: (timer?: number, done?: () => void) => void; | ||
}; | ||
|
||
export type ToastrProps = { | ||
placement?: keyof typeof placementClassnameMap; | ||
variant?: keyof typeof variantClassnameMap; | ||
timer?: number; | ||
className?: string; | ||
children?: React.ReactNode | string; | ||
}; | ||
|
||
const Toastr = forwardRef<ToastrRef, ToastrProps>(function Toastr( | ||
{ placement = 'bottom-end', variant = 'default', timer = 3, className, children }, | ||
ref | ||
) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
open: (newTimer, done) => { | ||
setIsOpen(true); | ||
setTimeout(() => { | ||
setIsOpen(false); | ||
|
||
if (done) done(); | ||
}, (newTimer || timer) * 1000); | ||
} | ||
}), | ||
[timer] | ||
); | ||
|
||
return ( | ||
<ReactPortal selector="#toast-root"> | ||
{isOpen && ( | ||
<div | ||
className={classnames( | ||
placementClassnameMap[placement], | ||
variantClassnameMap[variant], | ||
'rounded-md absolute z-50 p-8 m-4 max-w-md shadow-md border border-solid mui-animate-fade-up duration-100', | ||
className | ||
)} | ||
> | ||
{children} | ||
</div> | ||
)} | ||
</ReactPortal> | ||
); | ||
}); | ||
|
||
export default Toastr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.