forked from tsai/budibase
8 changed files with 81 additions and 55 deletions
@ -1,20 +1,20 @@ |
|||||
import { writable } from 'svelte/store' |
import { writable } from "svelte/store" |
||||
import api from "builderStore/api" |
import api from "builderStore/api" |
||||
|
|
||||
export default function (url) { |
export default function (url) { |
||||
const store = writable({status: 'LOADING', data: {}, error: {}}) |
const store = writable({ status: "LOADING", data: {}, error: {} }) |
||||
|
|
||||
async function get() { |
async function get() { |
||||
store.update(u => ({...u, status: 'SUCCESS'})) |
store.update(u => ({ ...u, status: "SUCCESS" })) |
||||
try { |
try { |
||||
const response = await api.get(url) |
const response = await api.get(url) |
||||
store.set({data: await response.json(), status: 'SUCCESS'}) |
store.set({ data: await response.json(), status: "SUCCESS" }) |
||||
} catch(e) { |
} catch (e) { |
||||
store.set({data: {}, error: e, status: 'ERROR'}) |
store.set({ data: {}, error: e, status: "ERROR" }) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
get() |
get() |
||||
|
|
||||
return {subscribe: store.subscribe, refresh: get} |
return { subscribe: store.subscribe, refresh: get } |
||||
} |
} |
||||
|
|||||
@ -1,2 +1,9 @@ |
|||||
export { default as fetchData } from './fetchData' |
export { default as fetchData } from "./fetchData" |
||||
export { buildStyle, convertCamel, pipe, capitalise, get_name, get_capitalised_name } from './helpers' |
export { |
||||
|
buildStyle, |
||||
|
convertCamel, |
||||
|
pipe, |
||||
|
capitalise, |
||||
|
get_name, |
||||
|
get_capitalised_name, |
||||
|
} from "./helpers" |
||||
|
|||||
@ -1,2 +1,2 @@ |
|||||
export { emailValidator, requiredValidator } from './validators' |
export { emailValidator, requiredValidator } from "./validators" |
||||
export { createValidationStore } from './validation' |
export { createValidationStore } from "./validation" |
||||
|
|||||
@ -1,23 +1,23 @@ |
|||||
import { writable, derived } from 'svelte/store' |
import { writable, derived } from "svelte/store" |
||||
|
|
||||
export function createValidationStore(initialValue, ...validators) { |
export function createValidationStore(initialValue, ...validators) { |
||||
let touched = false |
let touched = false |
||||
|
|
||||
const value = writable(initialValue || '') |
const value = writable(initialValue || "") |
||||
const error = derived(value, $v => validate($v, validators)) |
const error = derived(value, $v => validate($v, validators)) |
||||
const touchedStore = derived(value, () => { |
const touchedStore = derived(value, () => { |
||||
if (!touched) { |
if (!touched) { |
||||
touched = true |
touched = true |
||||
return false |
return false |
||||
} |
} |
||||
return touched |
return touched |
||||
}) |
}) |
||||
|
|
||||
return [value, error, touchedStore] |
return [value, error, touchedStore] |
||||
} |
} |
||||
|
|
||||
function validate(value, validators) { |
function validate(value, validators) { |
||||
const failing = validators.find(v => v(value) !== true) |
const failing = validators.find(v => v(value) !== true) |
||||
|
|
||||
return failing && failing(value) |
return failing && failing(value) |
||||
} |
} |
||||
|
|||||
@ -1,7 +1,16 @@ |
|||||
export function emailValidator (value) { |
export function emailValidator(value) { |
||||
return (value && !!value.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) || 'Please enter a valid email' |
return ( |
||||
} |
(value && |
||||
|
!!value.match( |
||||
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ |
||||
|
)) || |
||||
|
"Please enter a valid email" |
||||
|
) |
||||
|
} |
||||
|
|
||||
export function requiredValidator (value) { |
export function requiredValidator(value) { |
||||
return (value !== undefined && value !== null && value !== '') || 'This field is required' |
return ( |
||||
} |
(value !== undefined && value !== null && value !== "") || |
||||
|
"This field is required" |
||||
|
) |
||||
|
} |
||||
|
|||||
Loading…
Reference in new issue