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

Change plan widget #1649

Merged
merged 25 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a7c7f2e
Working on fetching plans
RyRy79261 Oct 21, 2021
4cb1bd9
Working on toggle switch
RyRy79261 Oct 22, 2021
150aa31
Toggle done
RyRy79261 Oct 25, 2021
3ead3f1
View done
RyRy79261 Oct 25, 2021
1225876
Close modal on change
RyRy79261 Oct 25, 2021
36a5071
Fix Webpack Build with node 17 (#1651)
FSM1 Oct 25, 2021
203e670
lingui extract
FSM1 Oct 25, 2021
98009c1
Feedback
RyRy79261 Oct 26, 2021
4656daf
Merge branch 'feat/change-plan-1455' of github.com:ChainSafe/files-ui…
RyRy79261 Oct 26, 2021
ee61d97
Update packages/common-components/src/ToggleSwitch/ToggleSwitch.tsx
RyRy79261 Oct 27, 2021
9b16b9a
Update packages/files-ui/src/Components/Modules/Settings/Products/Cha…
RyRy79261 Oct 27, 2021
5b30d81
Update packages/files-ui/src/Components/Modules/Settings/Products/Cha…
RyRy79261 Oct 27, 2021
8a30cc9
resolved feedback
RyRy79261 Oct 27, 2021
35b43da
Changed display logic
RyRy79261 Oct 27, 2021
09a6d17
Apply suggestions from code review
RyRy79261 Oct 27, 2021
5217224
Fixed change subscription
RyRy79261 Oct 27, 2021
f5d08ad
Merge branch 'feat/change-plan-1455' of github.com:ChainSafe/files-ui…
RyRy79261 Oct 27, 2021
167da20
Update packages/files-ui/src/Components/Modules/Settings/Products/Cha…
RyRy79261 Oct 28, 2021
d18182a
Update packages/files-ui/src/Components/Modules/Settings/Products/Cha…
RyRy79261 Oct 28, 2021
3620ee3
nits to make things more readable
Tbaut Oct 29, 2021
a7b0b08
Removed cast
RyRy79261 Nov 3, 2021
0bcb0b9
fix overrides
FSM1 Nov 3, 2021
c306ab0
add knobs to story
FSM1 Nov 3, 2021
f47d0da
Added formik option
RyRy79261 Nov 4, 2021
7811b27
merge upstream
Tbaut Nov 4, 2021
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
7 changes: 7 additions & 0 deletions packages/common-components/src/Icons/icons/External.icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from "react"
import createSvgIcon from "../createSvgIcon"
import { ReactComponent as ExternalSvg } from "../svgs/external.svg"

export { ExternalSvg }

export default createSvgIcon(<ExternalSvg />)
1 change: 1 addition & 0 deletions packages/common-components/src/Icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { default as EthereumLogoIcon, EthereumLogoSvg } from "./icons/EthereumLo
export { default as ExclamationCircleIcon, ExclamationCircleSvg } from "./icons/ExclamationCircle.icon"
export { default as ExclamationCircleInverseIcon, ExclamationCircleInverseSvg } from "./icons/ExclamationCircleInverse.icon"
export { default as ExportIcon, ExportSvg } from "./icons/Export.icon"
export { default as ExternalIcon, ExternalSvg } from "./icons/External.icon"
export { default as EyeClosedIcon, EyeClosedSvg } from "./icons/EyeClosed.icon"
export { default as EyeIcon, EyeSvg } from "./icons/Eye.icon"
export { default as EyeOpenIcon, EyeOpenSvg } from "./icons/EyeOpen.icon"
Expand Down
5 changes: 5 additions & 0 deletions packages/common-components/src/Icons/svgs/external.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions packages/common-components/src/ToggleSwitch/FormikToggleSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useField } from "formik"
import React from "react"
import { IToggleSwitch, ToggleSwitch } from "."


interface IFormikToggleSwitch extends IToggleSwitch {
name: string
}

const FormikToggleSwitch = ({ injectedClasses, disabled, name, left, right, size }: IFormikToggleSwitch) => {
const [field, meta, helpers] = useField(name)
const handleChange = (value: any) => {
helpers.setValue(value)
}

return <ToggleSwitch
{...field}
left={left}
right={right}
size={size}
disabled={disabled}
onChange={handleChange}
injectedClasses={injectedClasses}
error={meta.error}
/>
}

export default FormikToggleSwitch
174 changes: 174 additions & 0 deletions packages/common-components/src/ToggleSwitch/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { createStyles, ITheme, makeStyles } from "@chainsafe/common-theme"
import clsx from "clsx"
import React, { ReactNode, useCallback, useMemo, useState } from "react"
import { Typography } from "../Typography"

interface IStyleProps {
size: number
}

const SIZES = {
small: 14,
medium: 16,
large: 20
}

const PADDING = 2

const useStyles = makeStyles(
({ animation, breakpoints, constants, overrides, palette }: ITheme) =>
createStyles({
root : {
padding: `${constants.generalUnit}px 0`,
display: "flex",
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
cursor: "pointer",
"&.disabled": {
cursor: "initial"
},
[breakpoints.down("md")]: {
marginBottom: 0
},
...overrides?.Tabs?.root
},
label: {
marginRight: constants.generalUnit,
...overrides?.ToggleSwitch?.label
},
background: ({ size }: IStyleProps) => ({
boxSizing: "border-box",
display: "block",
position: "relative",
backgroundColor: palette.additional.gray[6],
borderRadius: size,
padding: PADDING,
height: size,
width: `calc(${size * 2}px - ${PADDING * 2}px)`,
transitionDuration: `${animation.transform}ms`,
...overrides?.ToggleSwitch?.background
}),
dot: ({ size }: IStyleProps) => ({
display: "block",
position: "absolute",
borderRadius: "100%",
height: `calc(${size}px - ${PADDING * 2}px)`,
width: `calc(${size}px - ${PADDING * 2}px)`,
backgroundColor: palette.additional.gray[1],
transitionDuration: `${animation.transform}ms`,
top: "50%",
transform: "translateY(-50%)",
"&.left": {
left: PADDING
},
"&.right": {
left: "50%"
},
"&.error": {
backgroundColor: palette.error.main
},
...overrides?.ToggleSwitch?.dot
})
})
)

interface ToggleSwitchInjectedClasses {
root?: string
label?: string
background?: string
dot?: string
}

// Could allow for a slider to be made from this
interface IToggleOption {
value: any
label?: string | ReactNode | number
}

interface IToggleSwitch {
left: IToggleOption
right: IToggleOption
onChange(value: any): void
injectedClasses?: ToggleSwitchInjectedClasses
size?: "large" | "medium" | "small" | number
error?: string
disabled?: boolean
value?: any
// name?: string
}

const ToggleSwitch = ({ injectedClasses, disabled, left, right, onChange, value, size, error }: IToggleSwitch) => {
const resolvedSize = useMemo(() => {
switch (size) {
case "large":
return SIZES.large
case "medium":
return SIZES.medium
case "small":
return SIZES.small
case undefined:
return SIZES.medium
default:
return size
}
}, [size])
const classes = useStyles({ size: resolvedSize })
const [side, setSide] = useState<"left" | "right">(value && right.value === value ? "right" : "left")

const onToggle = useCallback(() => {
if (disabled) return
if (side === "left") {
setSide("right")
onChange(right.value)
} else {
setSide("left")
onChange(left.value)
}
}, [left, right, side, onChange, disabled])

return <section
className={clsx(classes.root, injectedClasses?.root, {
"disabled": disabled
})}
onClick={onToggle}
>
{
side === "left" && left.label && (
<Typography
component="p"
variant="body1"
className={clsx(classes.label, injectedClasses?.label, {
"error": !!error
})}
>
{ left.label }
</Typography>
)
}
{
side === "right" && right.label && (
<Typography
component="p"
variant="body1"
className={clsx(classes.label, injectedClasses?.label, {
"error": !!error
})}
>
{ right.label }
</Typography>
)
}
<div className={clsx(classes.background, size, injectedClasses?.background, {
"error": !!error
})}>
<div className={clsx(classes.dot, side, injectedClasses?.dot, {
"error": !!error
})}>
</div>
</div>
</section>
}

export default ToggleSwitch
export { IToggleSwitch, IToggleOption, ToggleSwitchInjectedClasses }
6 changes: 6 additions & 0 deletions packages/common-components/src/ToggleSwitch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
default as ToggleSwitch,
IToggleOption,
IToggleSwitch,
ToggleSwitchInjectedClasses
} from "./ToggleSwitch"
1 change: 1 addition & 0 deletions packages/common-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export * from "./Tabs"
export * from "./TagsInput"
export * from "./Toasts"
export * from "./ToggleHiddenText"
export * from "./ToggleSwitch"
export * from "./TextInput"
export * from "./TreeView"
export * from "./Typography"
Expand Down
41 changes: 41 additions & 0 deletions packages/common-components/src/stories/ToggleSwitch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { action } from "@storybook/addon-actions"
import { boolean, select, withKnobs } from "@storybook/addon-knobs"
import React, { useState } from "react"
import { ToggleSwitch } from ".."
import { SizeOption } from "./types"

export default {
title: "ToggleSwitch",
component: ToggleSwitch,
excludeStories: /.*Data$/,
decorators: [withKnobs]
}

const sizeOptions: SizeOption[] = ["large", "medium", "small"]

const actionsData = {
onChange: action("onChange")
}

export const ToggleSwitchDemo = (): React.ReactNode => {
const [state, setState] = useState(false)
return (
<ToggleSwitch
left={{
value: false,
label: "off"
}}
right={{
value: true,
label: "on"
}}
value={state}
onChange={(value) => {
setState(value)
actionsData.onChange(value)
}}
disabled={boolean("Disabled", false)}
size={select("Size", sizeOptions, "medium")}
/>
)
}
6 changes: 6 additions & 0 deletions packages/common-theme/src/Overrides/ToggleSwitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IToggleSwitchOverride {
root?: Record<string, any>
label?: Record<string, any>
background?: Record<string, any>
dot?: Record<string, any>
}
2 changes: 2 additions & 0 deletions packages/common-theme/src/Overrides/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { IToastsOverride } from "./Toasts"
import { ITypographyOverride } from "./Typography"
import { ITagsInputOverride } from "./TagsInput"
import { IToggleHiddenText } from "./ToggleHiddenText"
import { IToggleSwitchOverride } from "./ToggleSwitch"

export interface IComponentOverrides {
Avatar?: IAvatarOverride
Expand Down Expand Up @@ -52,6 +53,7 @@ export interface IComponentOverrides {
TextInput?: ITextInputOverride
Toasts?: IToastsOverride
ToggleHiddenText?: IToggleHiddenText
ToggleSwitch?: IToggleSwitchOverride
Typography?: ITypographyOverride
TagsInput?: ITagsInputOverride
}
8 changes: 0 additions & 8 deletions packages/files-ui/src/Components/FilesRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import BinPage from "./Pages/BinPage"
import { useThresholdKey } from "../Contexts/ThresholdKeyContext"
import ShareFilesPage from "./Pages/SharedFilesPage"
import SharedFoldersOverview from "./Modules/FileBrowsers/SharedFoldersOverview"
import PlansPage from "./Pages/PlansPage"
import BillingHistory from "./Pages/BillingHistory"
import { NonceResponsePermission } from "@chainsafe/files-api-client"
import LinkSharingLanding from "./Pages/LinkSharingLanding"
Expand Down Expand Up @@ -116,13 +115,6 @@ const FilesRoutes = () => {
component={SettingsPage}
redirectPath={ROUTE_LINKS.Landing}
/>
<ConditionalRoute
exact
path={ROUTE_LINKS.Plans}
isAuthorized={isAuthorized}
component={PlansPage}
redirectPath={ROUTE_LINKS.Landing}
/>
<ConditionalRoute
path={ROUTE_LINKS.Landing}
isAuthorized={!isAuthorized}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useEffect, useState } from "react"
import { makeStyles, createStyles } from "@chainsafe/common-theme"
import { CSFTheme } from "../../../../Themes/types"
import clsx from "clsx"
import { Modal } from "@chainsafe/common-components"
import SelectPlan from "./ChangeProductViews/SelectPlan"
import { useBilling } from "../../../../Contexts/BillingContext"

const useStyles = makeStyles(({ constants }: CSFTheme) =>
createStyles({
root: {
},
inner: {
borderRadius: `${constants.generalUnit / 2}px`
},
slide: {
borderRadius: constants.generalUnit / 2,
padding: `0 ${constants.generalUnit * 3}px`
}
})
)

interface IChangeProductModal {
className?: string
close: () => void
}

const ChangeProductModal = ({ className, close }: IChangeProductModal) => {
const classes = useStyles()
const { changeSubscription } = useBilling()
const [slide, setSlide] = useState<"select" | "confirm">("select")

useEffect(() => {
if (slide !== "select") {
setSlide("select")
}
}, [slide])

return (
<Modal
closePosition="none"
active={true}
maxWidth="md"
className={classes.root}
injectedClass={{
inner: classes.inner
}}
testId="change-product"
>
{
slide === "select" && <SelectPlan
className={clsx(classes.slide, className)}
close={close}
next={(newpriceId: string) => {
// setSlide("confirm")
changeSubscription(newpriceId)
.then(() => close())
.catch(console.error)
}}
/>
}
</Modal>
)
}

export default ChangeProductModal
Loading