refact from scratch form

This commit is contained in:
wryk 2020-03-04 01:10:36 +01:00
parent 1f7050bccf
commit 02166f9b86
4 changed files with 71 additions and 112 deletions

55
package-lock.json generated
View File

@ -3120,11 +3120,6 @@
"integrity": "sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==", "integrity": "sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==",
"dev": true "dev": true
}, },
"fn-name": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/fn-name/-/fn-name-2.0.1.tgz",
"integrity": "sha1-UhTXU3pNBqSjAcDMJi/rhBiAAuc="
},
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@ -4680,11 +4675,6 @@
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
}, },
"lodash-es": {
"version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.15.tgz",
"integrity": "sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ=="
},
"lodash.clone": { "lodash.clone": {
"version": "4.5.0", "version": "4.5.0",
"resolved": "https://registry.npmjs.org/lodash.clone/-/lodash.clone-4.5.0.tgz", "resolved": "https://registry.npmjs.org/lodash.clone/-/lodash.clone-4.5.0.tgz",
@ -6247,11 +6237,6 @@
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",
"dev": true "dev": true
}, },
"property-expr": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/property-expr/-/property-expr-1.5.1.tgz",
"integrity": "sha512-CGuc0VUTGthpJXL36ydB6jnbyOf/rAHFvmVrJlH+Rg0DqqLFQGAP6hIaxD/G0OAmBJPhXDHuEJigrp0e0wFV6g=="
},
"psl": { "psl": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.7.0.tgz", "resolved": "https://registry.npmjs.org/psl/-/psl-1.7.0.tgz",
@ -7242,22 +7227,6 @@
"integrity": "sha512-jRk7jdYULb9V4Z+0BKlfofombmdIIQph4leojrOSHzvZBRmCredz7fZsJBiUDLO6h83XYekuLbwfy5zx1i95GQ==", "integrity": "sha512-jRk7jdYULb9V4Z+0BKlfofombmdIIQph4leojrOSHzvZBRmCredz7fZsJBiUDLO6h83XYekuLbwfy5zx1i95GQ==",
"dev": true "dev": true
}, },
"svelte-forms-lib": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/svelte-forms-lib/-/svelte-forms-lib-1.0.4.tgz",
"integrity": "sha512-6GuufV5AX/B8vQjE5uJS0rZSiAvjs+G8Apz8WYUaTeVLOirx0InfK5i7jgoA+J1uLFBg7+P5c4Icam8bjuKvkA==",
"requires": {
"property-expr": "1.5.1",
"svelte": "3.16.5"
},
"dependencies": {
"svelte": {
"version": "3.16.5",
"resolved": "https://registry.npmjs.org/svelte/-/svelte-3.16.5.tgz",
"integrity": "sha512-iYOhJc7iczaonycuKjPgmy1lthhBoDOQo5UVhgKJRd3XYRdnYPPb0LvvJ0t7jqu5+15S0msMm06WSOKUvhEZsw=="
}
}
},
"svelte-pipeable-store": { "svelte-pipeable-store": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/svelte-pipeable-store/-/svelte-pipeable-store-1.0.3.tgz", "resolved": "https://registry.npmjs.org/svelte-pipeable-store/-/svelte-pipeable-store-1.0.3.tgz",
@ -7303,11 +7272,6 @@
"integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==", "integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==",
"dev": true "dev": true
}, },
"synchronous-promise": {
"version": "2.0.10",
"resolved": "https://registry.npmjs.org/synchronous-promise/-/synchronous-promise-2.0.10.tgz",
"integrity": "sha512-6PC+JRGmNjiG3kJ56ZMNWDPL8hjyghF5cMXIFOKg+NiwwEZZIvxTWd0pinWKyD227odg9ygF8xVhhz7gb8Uq7A=="
},
"teeny-request": { "teeny-request": {
"version": "6.0.1", "version": "6.0.1",
"resolved": "https://registry.npmjs.org/teeny-request/-/teeny-request-6.0.1.tgz", "resolved": "https://registry.npmjs.org/teeny-request/-/teeny-request-6.0.1.tgz",
@ -7440,11 +7404,6 @@
"integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==", "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==",
"dev": true "dev": true
}, },
"toposort": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/toposort/-/toposort-2.0.2.tgz",
"integrity": "sha1-riF2gXXRVZ1IvvNUILL0li8JwzA="
},
"tough-cookie": { "tough-cookie": {
"version": "2.5.0", "version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
@ -8001,20 +7960,6 @@
"dev": true "dev": true
} }
} }
},
"yup": {
"version": "0.28.1",
"resolved": "https://registry.npmjs.org/yup/-/yup-0.28.1.tgz",
"integrity": "sha512-xSHMZA7UyecSG/CCTDCtnYZMjBrYDR/C7hu0fMsZ6UcS/ngko4qCVFbw+CAmNtHlbItKkvQ3YXITODeTj/dUkw==",
"requires": {
"@babel/runtime": "^7.0.0",
"fn-name": "~2.0.1",
"lodash": "^4.17.11",
"lodash-es": "^4.17.11",
"property-expr": "^1.5.0",
"synchronous-promise": "^2.0.6",
"toposort": "^2.0.2"
}
} }
} }
} }

View File

@ -32,11 +32,9 @@
"iter-tools": "^7.0.0-rc.0", "iter-tools": "^7.0.0-rc.0",
"regenerator-runtime": "^0.13.3", "regenerator-runtime": "^0.13.3",
"route-parser": "0.0.5", "route-parser": "0.0.5",
"svelte-forms-lib": "^1.0.4",
"svelte-pipeable-store": "^1.0.3", "svelte-pipeable-store": "^1.0.3",
"svelte-portal": "^0.1.0", "svelte-portal": "^0.1.0",
"svelte-routing": "^1.4.0", "svelte-routing": "^1.4.0"
"yup": "^0.28.1"
}, },
"staticFiles": { "staticFiles": {
"staticPath": "public", "staticPath": "public",

View File

@ -9,24 +9,35 @@
<div class="settings-side__subtitle">The mastodon instance from the toods are fetched</div> <div class="settings-side__subtitle">The mastodon instance from the toods are fetched</div>
</div> </div>
<div class="settings-main"> <div class="settings-main">
<input class="f-size-full error" type="text" id="domain" name="domain" on:change={handleChange} bind:value={$form.domain}> <input
<div class="notif notif--error">Le format n'est pas bon, déso</div> class="f-size-full"
</div> type="text" id="domain"
{#if $errors.domain} name="domain"
<span>{$errors.domain}</span> on:change={event => $domainValue = event.target.value}
value={$domainValue}
>
{#if !$domainState.valid}
<div class="notif notif--error">{$domainState.error}</div>
{/if} {/if}
</div> </div>
</div>
<div class="settings-line"> <div class="settings-line">
<div class="settings-side"> <div class="settings-side">
<div class="settings-side__title">Tags</div> <div class="settings-side__title">Tags</div>
<div class="settings-side__subtitle">What hashtags are fetched</div> <div class="settings-side__subtitle">What hashtags are fetched</div>
</div> </div>
<div class="settings-main"> <div class="settings-main">
<TagInput on:change={handleCustomChange('hashtags')} bind:value={$form.hashtags} /> <TagInput
on:change={event => $hashtagsValue = event.detail}
value={$hashtagsValue}
/>
{#if !$hashtagsState.valid}
<div class="notif notif--error">{$hashtagsState.error}</div>
{/if}
{#if $hashtagsValue.length > 5}
<div class="notif notif--warning">Too many hashtags can impair performances and network consumption.</div>
{/if}
</div> </div>
<!-- {#if $errors.hashtags}
<span>{$errors.hashtags}{console.log($errors.hashtags)}</span>
{/if} -->
</div> </div>
<!-- <div class="settings-line"> <!-- <div class="settings-line">
<div class="settings-side"> <div class="settings-side">
@ -46,15 +57,14 @@
</div> </div>
</div> </div>
</div> --> </div> -->
<button class="btn btn--primary w100" type="submit">Save the changes</button> <button class="btn btn--primary w100" type="submit" disabled={!$valid}>Save the changes</button>
</form> </form>
</ContextPage> </ContextPage>
</Portal> </Portal>
<script> <script>
import { getContext } from 'svelte' import { getContext } from 'svelte'
import { createForm } from 'svelte-forms-lib' import { writable, derived } from 'svelte/store'
import * as yup from 'yup'
import Portal from 'svelte-portal' import Portal from 'svelte-portal'
import ContextPage from '/components/ContextPage' import ContextPage from '/components/ContextPage'
import TagInput from '/components/TagInput' import TagInput from '/components/TagInput'
@ -62,44 +72,49 @@
const domain = getContext('domain') const domain = getContext('domain')
const hashtags = getContext('hashtags') const hashtags = getContext('hashtags')
const { const domainValue = writable($domain)
form, const domainState = derived([domainValue], async ([$domainValue], set) => {
errors, if ($domainValue === '') {
handleChange, set({ valid: false, error: 'A Mastodon instance\'s domain is required.' })
handleSubmit } else {
} = createForm({ try {
initialValues: { const response = await fetch(`https://${$domainValue}/api/v1/instance`)
domain: $domain, const instance = await response.json()
hashtags: $hashtags
}, if (response.ok) {
// validationSchema: yup.object().shape({ set({ valid: true, error: null })
// domain: yup.string() } else {
// .required() set({ valid: false, error: 'Domain is not a mastodon instance.' })
// .trim() }
// .test( } catch (error) {
// 'is-valid-domain', set({ valid: false, error: 'Network error when accessing domain. This might be an internet access problem or a typing mistake.' })
// '${path} is not a valid domain', }
// async domain => (await fetch(`https://${domain}/api/v1/instance`)).status === 200 }
// ), }, { valid: false, error: '' })
// // hashtags: yup.array()
// // .required() const hashtagsValue = writable($hashtags)
// // .of(yup.string() const hashtagsState = derived([hashtagsValue], ([$hashtagsValue]) => {
// // .ensure() if ($hashtagsValue.some(hashtag => !/[a-z0-9]+/i.test(hashtag))) {
// // .required() return { valid: false, error: 'Hashtags can only contains alphanumeric characters.' }
// // .trim() } else if ($hashtagsValue.length === 0) {
// // .matches('/[a-z0-9]+/i') return { valid: false, error: 'At least one hashtag is required.' }
// // ) } else {
// // .min(1) return { valid: true, error: null }
// // .min(5)
// }),
onSubmit: values => {
$domain = values.domain
$hashtags = values.hashtags
location.reload() // for now
} }
}) })
const handleCustomChange = name => event => handleChange({ target: { name, value: event.detail } }) const valid = derived([domainState, hashtagsState], states => states.every(state => state.valid))
const handleSubmit = event => {
event.preventDefault()
if ($valid) {
$domain = $domainValue
$hashtags = $hashtagsValue
location.reload() // for now
}
}
let open = false let open = false

View File

@ -14,19 +14,14 @@
$: tags = value $: tags = value
const update = () => {
value = tags
dispatch('change', tags)
}
const add = tag => { const add = tag => {
tags = [...tags, tag] tags = [...tags, tag]
update() dispatch('change', tags)
} }
const remove = index => { const remove = index => {
tags = [...tags.slice(0, index), ...tags.slice(index + 1)] tags = [...tags.slice(0, index), ...tags.slice(index + 1)]
update() dispatch('change', tags)
} }
const onKeyDown = event => { const onKeyDown = event => {
@ -34,6 +29,12 @@
switch (keyCode) { switch (keyCode) {
case 13: case 13:
if (value === '') {
break
}
case 32:
case 188:
event.preventDefault() event.preventDefault()
if (value !== '' && tags.indexOf(value) === -1) { if (value !== '' && tags.indexOf(value) === -1) {