Skip to content

Commit

Permalink
feat(sharing): Change ShareRestrictionModal validity
Browse files Browse the repository at this point in the history
Changed validation conditions when creating a share link.
  • Loading branch information
Merkur39 committed Jan 21, 2025
1 parent fe25984 commit f68c3a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
import { BoxDate } from './BoxDate'
import { BoxEditingRights } from './BoxEditingRights'
import { BoxPassword } from './BoxPassword'
import { checkIsPermissionHasPassword } from '../../helpers/permissions'
import { useSharingContext } from '../../hooks/useSharingContext'

const PASSWORD_MIN_LENGTH = 4

Expand All @@ -32,26 +34,40 @@ export const ShareRestrictionContentModal = ({
setEditingRights
}) => {
const { t } = useI18n()
const { getDocumentPermissions } = useSharingContext()
const permissions = getDocumentPermissions(file._id)
const hasPassword = checkIsPermissionHasPassword(permissions)

const handlePassword = value => {
setPassword(value)
setIsValidPassword(
value.trim().length === 0 || value.trim().length >= PASSWORD_MIN_LENGTH
)
setIsValidPassword(value.trim().length >= PASSWORD_MIN_LENGTH)
}

const handlePasswordToggle = val => {
setPasswordToggle(val)
handlePassword('')
if (val && !password) {
setIsValidPassword(hasPassword)
} else {
setIsValidPassword(true)
}
}

const handleDate = date => {
setSelectedDate(date)
setIsValidDate(Boolean(date))
}

const handleDateToggle = val => {
setDateToggle(val)
if (!val) {
setSelectedDate(null)
setIsValidDate(true)
} else {
const defaultValue = val ? addDays(new Date(), 30) : null
setSelectedDate(defaultValue)
setIsValidDate(true)
}
const defaultValue = val ? addDays(new Date(), 30) : null
setSelectedDate(defaultValue, true)
setDateToggle(val)
}

return (
Expand All @@ -66,7 +82,7 @@ export const ShareRestrictionContentModal = ({
setEditingRights={setEditingRights}
/>
<BoxDate
onChange={setSelectedDate}
onChange={handleDate}
date={selectedDate}
onToggle={handleDateToggle}
toggle={dateToggle}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ export const ShareRestrictionModal = ({ file, onClose }) => {
})
: null

const handleDateChange = (date, isValid) => {
setSelectedDate(date)
setIsValidDate(isValid)
}

const handleClick = async () => {
setLoading(true)
// If the file is not shared, we create a new sharing link
Expand Down Expand Up @@ -146,7 +141,7 @@ export const ShareRestrictionModal = ({ file, onClose }) => {
// Date
dateToggle={dateToggle}
setDateToggle={setDateToggle}
setSelectedDate={handleDateChange}
setSelectedDate={setSelectedDate}
selectedDate={selectedDate}
setIsValidDate={setIsValidDate}
// Password
Expand Down

0 comments on commit f68c3a4

Please sign in to comment.