Skip to content

Commit bd9b414

Browse files
Fix date handling and saving
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent 42140fe commit bd9b414

File tree

6 files changed

+72
-41
lines changed

6 files changed

+72
-41
lines changed

apps/files_sharing/src/components/SharingEntry.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
:hide-label="true"
100100
:class="{ error: errors.expireDate}"
101101
:disabled="saving"
102-
:value="share.expireDate"
102+
:value="new Date(share.expireDate)"
103103
type="date"
104104
:min="dateTomorrow"
105105
:max="dateMaxEnforced"
@@ -149,7 +149,7 @@ import NcActionInput from '@nextcloud/vue/dist/Components/NcActionInput'
149149
import NcActionTextEditable from '@nextcloud/vue/dist/Components/NcActionTextEditable'
150150
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
151151
152-
import SharesMixin from '../mixins/SharesMixin'
152+
import SharesMixin from '../mixins/SharesMixin.js'
153153
154154
export default {
155155
name: 'SharingEntry',
@@ -374,12 +374,10 @@ export default {
374374
return this.config.isDefaultInternalExpireDateEnforced || !!this.share.expireDate
375375
},
376376
set(enabled) {
377-
let defaultExpirationDate = this.config.defaultInternalExpirationDate
378-
if (!defaultExpirationDate) {
379-
defaultExpirationDate = new Date()
380-
}
377+
const defaultExpirationDate = this.config.defaultInternalExpirationDate
378+
|| new Date(new Date().setDate(new Date().getDate() + 1))
381379
this.share.expireDate = enabled
382-
? defaultExpirationDate
380+
? this.formatDateToString(defaultExpirationDate)
383381
: ''
384382
console.debug('Expiration date status', enabled, this.share.expireDate)
385383
},

apps/files_sharing/src/components/SharingEntryLink.vue

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@
9797
{{ t('files_sharing', 'Expiration date (enforced)') }}
9898
</NcActionText>
9999
<NcActionInput v-if="pendingExpirationDate"
100-
v-model="share.expireDate"
101100
class="share-link-expire-date"
102101
:disabled="saving"
103102
:is-native-picker="true"
104103
:hide-label="true"
104+
:value="new Date(share.expireDate)"
105105
type="date"
106106
:min="dateTomorrow"
107-
:max="dateMaxEnforced">
107+
:max="dateMaxEnforced"
108+
@input="onExpirationChange">
108109
<!-- let's not submit when picked, the user
109110
might want to still edit or copy the password -->
110111
{{ t('files_sharing', 'Enter a date') }}
@@ -218,7 +219,7 @@
218219
class="share-link-expire-date"
219220
:class="{ error: errors.expireDate}"
220221
:disabled="saving"
221-
:value="share.expireDate"
222+
:value="new Date(share.expireDate)"
222223
type="date"
223224
:min="dateTomorrow"
224225
:max="dateMaxEnforced"
@@ -298,6 +299,7 @@
298299

299300
<script>
300301
import { generateUrl } from '@nextcloud/router'
302+
import { showError } from '@nextcloud/dialogs'
301303
import { Type as ShareTypes } from '@nextcloud/sharing'
302304
import Vue from 'vue'
303305
@@ -312,11 +314,11 @@ import NcActions from '@nextcloud/vue/dist/Components/NcActions'
312314
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar'
313315
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
314316
315-
import ExternalShareAction from './ExternalShareAction'
316-
import SharePermissionsEditor from './SharePermissionsEditor'
317-
import GeneratePassword from '../utils/GeneratePassword'
318-
import Share from '../models/Share'
319-
import SharesMixin from '../mixins/SharesMixin'
317+
import ExternalShareAction from './ExternalShareAction.vue'
318+
import SharePermissionsEditor from './SharePermissionsEditor.vue'
319+
import GeneratePassword from '../utils/GeneratePassword.js'
320+
import Share from '../models/Share.js'
321+
import SharesMixin from '../mixins/SharesMixin.js'
320322
321323
export default {
322324
name: 'SharingEntryLink',
@@ -422,20 +424,18 @@ export default {
422424
|| !!this.share.expireDate
423425
},
424426
set(enabled) {
425-
let defaultExpirationDate = this.config.defaultExpirationDate
426-
if (!defaultExpirationDate) {
427-
defaultExpirationDate = new Date()
428-
}
429-
this.share.state.expiration = enabled
430-
? defaultExpirationDate
427+
const defaultExpirationDate = this.config.defaultExpirationDate
428+
|| new Date(new Date().setDate(new Date().getDate() + 1))
429+
this.share.expireDate = enabled
430+
? this.formatDateToString(defaultExpirationDate)
431431
: ''
432432
console.debug('Expiration date status', enabled, this.share.expireDate)
433433
},
434434
},
435435
436436
dateMaxEnforced() {
437437
if (this.config.isDefaultExpireDateEnforced) {
438-
return new Date(new Date().setDate(new Date().getDate() + 1 + this.config.defaultExpireDate))
438+
return new Date(new Date().setDate(new Date().getDate() + this.config.defaultExpireDate))
439439
}
440440
return null
441441
},
@@ -620,7 +620,7 @@ export default {
620620
if (this.config.isDefaultExpireDateEnforced) {
621621
// default is empty string if not set
622622
// expiration is the share object key, not expireDate
623-
shareDefaults.expiration = this.config.defaultExpirationDate
623+
shareDefaults.expiration = this.formatDateToString(this.config.defaultExpirationDate)
624624
}
625625
if (this.config.enableLinkPasswordByDefault) {
626626
shareDefaults.password = await GeneratePassword()
@@ -687,7 +687,7 @@ export default {
687687
this.errors = {}
688688
689689
const path = (this.fileInfo.path + '/' + this.fileInfo.name).replace('//', '/')
690-
const newShare = await this.createShare({
690+
const options = {
691691
path,
692692
shareType: ShareTypes.SHARE_TYPE_LINK,
693693
password: share.password,
@@ -698,10 +698,12 @@ export default {
698698
// Todo: We also need to fix the createShare method in
699699
// lib/Controller/ShareAPIController.php to allow file drop
700700
// (currently not supported on create, only update)
701-
})
701+
}
702702
703-
this.open = false
703+
console.debug('Creating link share with options', options)
704+
const newShare = await this.createShare(options)
704705
706+
this.open = false
705707
console.debug('Link share created', newShare)
706708
707709
// if share already exists, copy link directly on next tick
@@ -728,8 +730,14 @@ export default {
728730
component.copyLink()
729731
}
730732
731-
} catch ({ response }) {
732-
const message = response.data.ocs.meta.message
733+
} catch (data) {
734+
const message = data?.response?.data?.ocs?.meta?.message
735+
if (!message) {
736+
showError(t('sharing', 'Error while creating the share'))
737+
console.error(data)
738+
return
739+
}
740+
733741
if (message.match(/password/i)) {
734742
this.onSyncError('password', message)
735743
} else if (message.match(/date/i)) {

apps/files_sharing/src/mixins/SharesMixin.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
*
2626
*/
2727

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

32-
import Share from '../models/Share'
33-
import SharesRequests from './ShareRequests'
34-
import ShareTypes from './ShareTypes'
35-
import Config from '../services/ConfigService'
36-
import { getCurrentUser } from '@nextcloud/auth'
33+
import Share from '../models/Share.js'
34+
import SharesRequests from './ShareRequests.js'
35+
import ShareTypes from './ShareTypes.js'
36+
import Config from '../services/ConfigService.js'
3737

3838
export default {
3939
mixins: [SharesRequests, ShareTypes],
@@ -150,13 +150,36 @@ export default {
150150
return true
151151
},
152152

153+
/**
154+
* @param {string} date a date with YYYY-MM-DD format
155+
* @return {Date} date
156+
*/
157+
parseDateString(date) {
158+
if (!date) {
159+
return
160+
}
161+
const regex = /([0-9]{4}-[0-9]{2}-[0-9]{2})/i
162+
return new Date(date.match(regex)?.pop())
163+
},
164+
165+
/**
166+
* @param {Date} date
167+
* @return {string} date a date with YYYY-MM-DD format
168+
*/
169+
formatDateToString(date) {
170+
// Force utc time. Drop time information to be timezone-less
171+
const utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
172+
// Format to YYYY-MM-DD
173+
return utcDate.toISOString().split('T')[0]
174+
},
175+
153176
/**
154177
* Save given value to expireDate and trigger queueUpdate
155178
*
156179
* @param {Date} date
157180
*/
158181
onExpirationChange(date) {
159-
this.share.expireDate = date
182+
this.share.expireDate = this.formatDateToString(date)
160183
this.queueUpdate('expireDate')
161184
},
162185

@@ -258,9 +281,11 @@ export default {
258281
this.saving = false
259282
}
260283
})
261-
} else {
262-
console.error('Cannot update share.', this.share, 'No valid id')
284+
return
263285
}
286+
287+
// This share does not exists on the server yet
288+
console.debug('Updated local share', this.share)
264289
},
265290

266291
/**

apps/files_sharing/src/models/Share.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export default class Share {
250250
/**
251251
* Get the expiration date
252252
*
253-
* @return {Date|null}
253+
* @return {string} date with YYYY-MM-DD format
254254
* @readonly
255255
* @memberof Share
256256
*/
@@ -261,7 +261,7 @@ export default class Share {
261261
/**
262262
* Set the expiration date
263263
*
264-
* @param {Date|null} date the share expiration date
264+
* @param {string} date the share expiration date with YYYY-MM-DD format
265265
* @memberof Share
266266
*/
267267
set expireDate(date) {

dist/files_sharing-files_sharing_tab.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_sharing-files_sharing_tab.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)