-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5258675
commit 5bf820c
Showing
15 changed files
with
541 additions
and
211 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
3 changes: 3 additions & 0 deletions
3
packages/web/src/components/data-entry/AccessAndSaleModalLegacy.module.css
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,3 @@ | ||
.availabilityButton { | ||
margin-top: 18px; | ||
} |
249 changes: 249 additions & 0 deletions
249
packages/web/src/components/data-entry/AccessAndSaleModalLegacy.tsx
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,249 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { | ||
Track, | ||
TrackAvailabilityType, | ||
isPremiumContentCollectibleGated, | ||
isPremiumContentFollowGated, | ||
isPremiumContentTipGated, | ||
isPremiumContentUSDCPurchaseGated | ||
} from '@audius/common' | ||
import { | ||
Button, | ||
ButtonSize, | ||
ButtonType, | ||
IconCart, | ||
IconCollectible, | ||
IconHidden, | ||
IconSpecialAccess, | ||
IconVisibilityPublic | ||
} from '@audius/stems' | ||
import { set, isEmpty, get } from 'lodash' | ||
import { z } from 'zod' | ||
import { toFormikValidationSchema } from 'zod-formik-adapter' | ||
|
||
import { TrackMetadataState } from 'components/track-availability-modal/types' | ||
import { defaultFieldVisibility } from 'pages/track-page/utils' | ||
import { | ||
AVAILABILITY_TYPE, | ||
AccessAndSaleFormValues, | ||
AccessAndSaleMenuFields, | ||
FIELD_VISIBILITY, | ||
IS_PREMIUM, | ||
IS_UNLISTED, | ||
PREMIUM_CONDITIONS, | ||
PREVIEW, | ||
PRICE, | ||
PRICE_HUMANIZED, | ||
SPECIAL_ACCESS_TYPE | ||
} from 'pages/upload-page/fields/AccessAndSaleField' | ||
import { SpecialAccessType } from 'pages/upload-page/fields/availability/SpecialAccessFields' | ||
|
||
import styles from './AccessAndSaleModalLegacy.module.css' | ||
import { ContextualMenu } from './ContextualMenu' | ||
|
||
const messages = { | ||
title: 'Access & Sale', | ||
description: | ||
"Customize your music's availability for different audiences, and create personalized gated experiences for your fans.", | ||
public: 'Public (Default)', | ||
premium: 'Premium', | ||
specialAccess: 'Special Access', | ||
collectibleGated: 'Collectible Gated', | ||
hidden: 'Hidden', | ||
errors: { | ||
price: { | ||
tooLow: 'Price must be at least $0.99', | ||
tooHigh: 'Price must be less than $9.99' | ||
}, | ||
preview: { | ||
tooEarly: 'Preview must start during the track', | ||
tooLate: | ||
'Preview must start at lest 15 seconds before the end of the track' | ||
} | ||
} | ||
} | ||
|
||
const AccessAndSaleFormSchema = (trackLength: number) => | ||
z.object({ | ||
[PREMIUM_CONDITIONS]: z.nullable( | ||
z.object({ | ||
// TODO: there are other types | ||
usdc_purchase: z.object({ | ||
price: z | ||
.number() | ||
.lte(999, messages.errors.price.tooHigh) | ||
.gte(99, messages.errors.price.tooLow) | ||
}) | ||
}) | ||
), | ||
[PREVIEW]: z.optional( | ||
z | ||
.number() | ||
.gte(0, messages.errors.preview.tooEarly) | ||
.lte(trackLength - 15, messages.errors.preview.tooLate) | ||
) | ||
}) | ||
|
||
type AccessAndSaleModalLegacyProps = { | ||
isRemix: boolean | ||
isUpload: boolean | ||
initialForm: Track | ||
metadataState: TrackMetadataState | ||
trackLength: number | ||
didUpdateState: (newState: TrackMetadataState) => void | ||
} | ||
|
||
export const AccessAndSaleModalLegacy = ( | ||
props: AccessAndSaleModalLegacyProps | ||
) => { | ||
const { | ||
isUpload, | ||
isRemix, | ||
initialForm, | ||
metadataState, | ||
trackLength, | ||
didUpdateState | ||
} = props | ||
const { | ||
premium_conditions: premiumConditions, | ||
unlisted: isUnlisted, | ||
is_premium: isPremium, | ||
preview_start_seconds: preview, | ||
...fieldVisibility | ||
} = metadataState | ||
|
||
const initialValues: AccessAndSaleFormValues = useMemo(() => { | ||
const isUsdcGated = isPremiumContentUSDCPurchaseGated(premiumConditions) | ||
const isTipGated = isPremiumContentTipGated(premiumConditions) | ||
const isFollowGated = isPremiumContentFollowGated(premiumConditions) | ||
const isCollectibleGated = | ||
isPremiumContentCollectibleGated(premiumConditions) | ||
const initialValues = {} | ||
set(initialValues, IS_UNLISTED, isUnlisted) | ||
set(initialValues, IS_PREMIUM, isPremium) | ||
set(initialValues, PREMIUM_CONDITIONS, premiumConditions) | ||
|
||
let availabilityType = TrackAvailabilityType.PUBLIC | ||
if (isUsdcGated) { | ||
availabilityType = TrackAvailabilityType.USDC_PURCHASE | ||
set( | ||
initialValues, | ||
PRICE_HUMANIZED, | ||
(Number(premiumConditions.usdc_purchase.price || 0) / 100).toFixed(2) | ||
) | ||
} | ||
if (isFollowGated || isTipGated) { | ||
availabilityType = TrackAvailabilityType.SPECIAL_ACCESS | ||
} | ||
if (isCollectibleGated) { | ||
availabilityType = TrackAvailabilityType.COLLECTIBLE_GATED | ||
} | ||
if (isUnlisted) { | ||
availabilityType = TrackAvailabilityType.HIDDEN | ||
} | ||
set(initialValues, AVAILABILITY_TYPE, availabilityType) | ||
set(initialValues, FIELD_VISIBILITY, fieldVisibility) | ||
set(initialValues, PREVIEW, preview) | ||
set( | ||
initialValues, | ||
SPECIAL_ACCESS_TYPE, | ||
isTipGated ? SpecialAccessType.TIP : SpecialAccessType.FOLLOW | ||
) | ||
return initialValues as AccessAndSaleFormValues | ||
}, [fieldVisibility, isPremium, isUnlisted, premiumConditions, preview]) | ||
|
||
const onSubmit = (values: AccessAndSaleFormValues) => { | ||
let newState = { | ||
...metadataState, | ||
is_premium: !isEmpty(values[PREMIUM_CONDITIONS]), | ||
premium_conditions: values[PREMIUM_CONDITIONS], | ||
unlisted: values.is_unlisted, | ||
preview_start_seconds: values[PREVIEW] ?? 0 | ||
} | ||
|
||
if ( | ||
get(values, AVAILABILITY_TYPE) === TrackAvailabilityType.USDC_PURCHASE | ||
) { | ||
newState.is_premium = true | ||
const price = Math.round(get(values, PRICE)) | ||
newState.premium_conditions = { | ||
// @ts-ignore splits get added in saga | ||
usdc_purchase: { | ||
price | ||
} | ||
} | ||
} | ||
|
||
if (get(values, AVAILABILITY_TYPE) === TrackAvailabilityType.HIDDEN) { | ||
newState = { | ||
...newState, | ||
...(get(values, FIELD_VISIBILITY) ?? undefined), | ||
unlisted: true | ||
} | ||
} else { | ||
newState = { | ||
...newState, | ||
...defaultFieldVisibility, | ||
unlisted: false | ||
} | ||
} | ||
|
||
didUpdateState(newState) | ||
} | ||
|
||
let availabilityButtonTitle = messages.public | ||
let AvailabilityIcon = IconVisibilityPublic | ||
if (isUnlisted) { | ||
availabilityButtonTitle = messages.hidden | ||
AvailabilityIcon = IconHidden | ||
} else if (isPremium) { | ||
if (isPremiumContentUSDCPurchaseGated(premiumConditions)) { | ||
availabilityButtonTitle = messages.premium | ||
AvailabilityIcon = IconCart | ||
} else if (isPremiumContentCollectibleGated(premiumConditions)) { | ||
availabilityButtonTitle = messages.collectibleGated | ||
AvailabilityIcon = IconCollectible | ||
} else { | ||
availabilityButtonTitle = messages.specialAccess | ||
AvailabilityIcon = IconSpecialAccess | ||
} | ||
} | ||
|
||
return ( | ||
<ContextualMenu | ||
label={messages.title} | ||
description={messages.description} | ||
icon={<IconHidden />} | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
validationSchema={toFormikValidationSchema( | ||
// @ts-ignore | ||
AccessAndSaleFormSchema(trackLength) | ||
)} | ||
menuFields={ | ||
<AccessAndSaleMenuFields | ||
isRemix={isRemix} | ||
isUpload={isUpload} | ||
isInitiallyUnlisted={initialForm[IS_UNLISTED]} | ||
initialPremiumConditions={ | ||
initialForm[PREMIUM_CONDITIONS] ?? undefined | ||
} | ||
premiumConditions={metadataState.premium_conditions} | ||
/> | ||
} | ||
renderValue={() => null} | ||
previewOverride={(toggleMenu) => ( | ||
<Button | ||
className={styles.availabilityButton} | ||
type={ButtonType.COMMON_ALT} | ||
name='availabilityModal' | ||
text={availabilityButtonTitle} | ||
size={ButtonSize.SMALL} | ||
onClick={toggleMenu} | ||
leftIcon={<AvailabilityIcon />} | ||
/> | ||
)} | ||
/> | ||
) | ||
} |
Oops, something went wrong.