Skip to content

Commit

Permalink
Fix date handling and saving
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Nov 16, 2022
1 parent 62919ac commit e03248e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion apps/files_sharing/src/components/SharingEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
:hide-label="true"
:class="{ error: errors.expireDate}"
:disabled="saving"
:value="share.expireDate"
:value="new Date(share.expireDate)"
type="date"
:min="dateTomorrow"
:max="dateMaxEnforced"
Expand Down
27 changes: 18 additions & 9 deletions apps/files_sharing/src/components/SharingEntryLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
class="share-link-expire-date"
:class="{ error: errors.expireDate}"
:disabled="saving"
:value="share.expireDate"
:value="new Date(share.expireDate)"
type="date"
:min="dateTomorrow"
:max="dateMaxEnforced"
Expand Down Expand Up @@ -317,6 +317,7 @@ import SharePermissionsEditor from './SharePermissionsEditor'
import GeneratePassword from '../utils/GeneratePassword'
import Share from '../models/Share'
import SharesMixin from '../mixins/SharesMixin'
import { showError } from '@nextcloud/dialogs'
export default {
name: 'SharingEntryLink',
Expand Down Expand Up @@ -427,15 +428,15 @@ export default {
defaultExpirationDate = new Date()
}
this.share.state.expiration = enabled
? defaultExpirationDate
? this.formatDateToString(defaultExpirationDate)
: ''
console.debug('Expiration date status', enabled, this.share.expireDate)
},
},
dateMaxEnforced() {
if (this.config.isDefaultExpireDateEnforced) {
return new Date(new Date().setDate(new Date().getDate() + 1 + this.config.defaultExpireDate))
return new Date(new Date().setDate(new Date().getDate() + this.config.defaultExpireDate))
}
return null
},
Expand Down Expand Up @@ -620,7 +621,7 @@ export default {
if (this.config.isDefaultExpireDateEnforced) {
// default is empty string if not set
// expiration is the share object key, not expireDate
shareDefaults.expiration = this.config.defaultExpirationDate
shareDefaults.expiration = this.formatDateToString(this.config.defaultExpirationDate)
}
if (this.config.enableLinkPasswordByDefault) {
shareDefaults.password = await GeneratePassword()
Expand Down Expand Up @@ -687,7 +688,7 @@ export default {
this.errors = {}
const path = (this.fileInfo.path + '/' + this.fileInfo.name).replace('//', '/')
const newShare = await this.createShare({
const options = {
path,
shareType: ShareTypes.SHARE_TYPE_LINK,
password: share.password,
Expand All @@ -698,10 +699,12 @@ export default {
// Todo: We also need to fix the createShare method in
// lib/Controller/ShareAPIController.php to allow file drop
// (currently not supported on create, only update)
})
}
this.open = false
console.debug('Creating link share with options', options)
const newShare = await this.createShare(options)
this.open = false
console.debug('Link share created', newShare)
// if share already exists, copy link directly on next tick
Expand All @@ -728,8 +731,14 @@ export default {
component.copyLink()
}
} catch ({ response }) {
const message = response.data.ocs.meta.message
} catch (data) {
const message = data?.response?.data?.ocs?.meta?.message
if (!message) {
showError(t('sharing', 'Error while creating the share'))
console.error(data)
return
}
if (message.match(/password/i)) {
this.onSyncError('password', message)
} else if (message.match(/date/i)) {
Expand Down
35 changes: 29 additions & 6 deletions apps/files_sharing/src/mixins/SharesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
*
*/

import { getCurrentUser } from '@nextcloud/auth'
// eslint-disable-next-line import/no-unresolved, node/no-missing-import
import PQueue from 'p-queue'
import debounce from 'debounce'

import Share from '../models/Share'
import SharesRequests from './ShareRequests'
import ShareTypes from './ShareTypes'
import Config from '../services/ConfigService'
import { getCurrentUser } from '@nextcloud/auth'
import Share from '../models/Share.js'
import SharesRequests from './ShareRequests.js'
import ShareTypes from './ShareTypes.js'
import Config from '../services/ConfigService.js'

export default {
mixins: [SharesRequests, ShareTypes],
Expand Down Expand Up @@ -150,13 +150,36 @@ export default {
return true
},

/**
* @param {string} date a date with YYYY-MM-DD format
* @return {Date} date
*/
parseDateString(date) {
if (!date) {
return
}
const regex = /([0-9]{4}-[0-9]{2}-[0-9]{2})/i
return new Date(date.match(regex)?.pop())
},

/**
* @param {Date} date
* @return {string} date a date with YYYY-MM-DD format
*/
formatDateToString(date) {
// Force utc time. Drop time information to be timezone-less
const utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
// Format to YYYY-MM-DD
return utcDate.toISOString().split('T')[0]
},

/**
* Save given value to expireDate and trigger queueUpdate
*
* @param {Date} date
*/
onExpirationChange(date) {
this.share.expireDate = date
this.share.expireDate = this.formatDateToString(date)
this.queueUpdate('expireDate')
},

Expand Down
4 changes: 2 additions & 2 deletions apps/files_sharing/src/models/Share.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default class Share {
/**
* Get the expiration date
*
* @return {Date|null}
* @return {string} date with YYYY-MM-DD format
* @readonly
* @memberof Share
*/
Expand All @@ -261,7 +261,7 @@ export default class Share {
/**
* Set the expiration date
*
* @param {Date|null} date the share expiration date
* @param {string} date the share expiration date with YYYY-MM-DD format
* @memberof Share
*/
set expireDate(date) {
Expand Down

0 comments on commit e03248e

Please sign in to comment.