@picked/svelte 0.0.1-dev.1756875690387 → 0.0.1-dev.1762750479117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,12 @@ interface GetFormOptions {
5
5
  onSuccess?: () => void | Promise<void>;
6
6
  onError?: (errors: ValidationError[]) => void | Promise<void>;
7
7
  }
8
- export declare function getForm(options: GetFormOptions): {
8
+ type SubmitEventWithCurrentTarget = SubmitEvent & {
9
+ currentTarget: EventTarget & HTMLFormElement;
10
+ };
11
+ export declare function getForm({ id: formId, title, onError, onSuccess }: GetFormOptions): {
9
12
  form: {
10
- onsubmit: (event: SubmitEvent & {
11
- currentTarget: EventTarget & HTMLFormElement;
12
- }) => Promise<void>;
13
+ onsubmit: import('effector').EventCallable<SubmitEventWithCurrentTarget>;
13
14
  };
14
15
  };
15
16
  export {};
@@ -1 +1 @@
1
- import{getPicked as v,getExperience as y}from"../hooks/client.js";import"svelte";import"effector";import"svelte/store";import"@picked/client";function B(a){const s=v();y();const{id:n,title:o}=a;async function d(c){var f,m;c.preventDefault(),s.eventBus({type:"form_submit",form:{id:n,title:o}});const p=new FormData(c.currentTarget),i={data:{},push(t){const r=t.questionId,e=this.data[r];e?Array.isArray(e)&&Array.isArray(t.value)?Array.isArray(e)&&Array.isArray(t.value)&&(typeof e[0]=="string"?this.data[r]=[...e,...t.value].map(String):typeof e[0]=="number"?this.data[r]=[...e,...t.value].map(Number):typeof e[0]=="boolean"?this.data[r]=[...e,...t.value].map(u=>!!u):this.data[r]=t.value):this.data[r]=t.value:this.data[r]=t.value}};for(const[t,r]of p.entries()){const e=s.fields.getById(t).getState();if(!e)continue;const u=e.id;switch(e.type){case"text":i.push({questionId:u,value:e.multiple?[String(r)]:String(r)});break;case"number":i.push({questionId:u,value:e.multiple?[Number(r)]:Number(r)});break;case"boolean":i.push({questionId:u,value:e.multiple?[r==="true"]:r==="true"});break}}const{errors:l}=await s.responses.commit(i.data);if(l.length>0){s.eventBus({type:"form_submit_error",form:{id:n,title:o},exception:"Form validation error"}),l.forEach(t=>{s.eventBus({type:"field_validation_error",exception:t})}),await((f=a.onError)==null?void 0:f.call(a,l));return}s.eventBus({type:"form_submit_success",form:{id:n,title:o}}),await((m=a.onSuccess)==null?void 0:m.call(a))}return{form:{onsubmit:d}}}export{B as getForm};
1
+ import{createEvent as k,createEffect as l,attach as b,sample as x,split as B}from"effector";import{getPicked as F,getExperience as D}from"../hooks/client.js";import"svelte";import"@picked/client";import"svelte/store";function I({id:f,title:m,onError:d,onSuccess:v}){const i=F(),o=D(),{currentStepStore:p}=o,h=k(),y=l(e=>i.responses.commit(e)),g=b({source:{step:p},effect:l(({step:e})=>{i.eventBus({type:"form_submit_success",form:{id:f,title:m},experience:o,step:e??void 0}),v==null||v()})}),_=b({source:{step:p},mapParams:(e,a)=>({step:a.step,errors:e.errors}),effect:l(({step:e,errors:a})=>{i.eventBus({type:"form_submit_error",form:{id:f,title:m},exception:{reason:a},experience:o,step:e??void 0}),a.forEach(n=>{i.eventBus({type:"field_validation_error",exception:{reason:[n]},experience:o,step:e??void 0})}),d==null||d(a)})});return x({clock:h,source:{step:p},fn:({step:e})=>({formId:f,title:m,step:e}),target:l(({formId:e,title:a,step:n})=>{i.eventBus({type:"form_submit",form:{id:e,title:a},experience:o,step:n??void 0})})}),x({clock:h,source:{fields:i.fields.listStore,step:p},fn:({fields:e},a)=>{a.preventDefault();const n=new FormData(a.currentTarget),u={data:{},push(s){const r=s.questionId,t=this.data[r];t?Array.isArray(t)&&Array.isArray(s.value)?Array.isArray(t)&&Array.isArray(s.value)&&(typeof t[0]=="string"?this.data[r]=[...t,...s.value].map(String):typeof t[0]=="number"?this.data[r]=[...t,...s.value].map(Number):typeof t[0]=="boolean"?this.data[r]=[...t,...s.value].map(c=>!!c):this.data[r]=s.value):this.data[r]=s.value:this.data[r]=s.value}};for(const[s,r]of n.entries()){const t=e.find(A=>A.id===s);if(!t)continue;const c=t.id;switch(t.type){case"text":u.push({questionId:c,value:t.multiple?[String(r)]:String(r)});break;case"number":u.push({questionId:c,value:t.multiple?[Number(r)]:Number(r)});break;case"boolean":u.push({questionId:c,value:t.multiple?[r==="true"]:r==="true"});break}}return u.data},target:y}),B({source:y.doneData,match:{hasErrors:({errors:e})=>e.length>0},cases:{hasErrors:_,__:g}}),{form:{onsubmit:h}}}export{I as getForm};
@@ -1,16 +1,13 @@
1
1
  import { Store } from 'effector';
2
- import { Readable } from 'svelte/store';
3
- import { Field, Response } from '@picked/client';
2
+ import { ExperienceStep, Field, Response } from '@picked/client';
3
+ type FieldWithValue = Field & {
4
+ value: Response | undefined;
5
+ };
4
6
  export interface StepContext {
5
- step: Store<{
6
- id: string;
7
- label: string;
8
- }>;
9
- fields: Store<{
10
- field: Field | undefined;
11
- value: Response | undefined;
12
- }[]>;
13
- isActiveStep?: Readable<boolean>;
7
+ step: Store<ExperienceStep>;
8
+ fields: Store<FieldWithValue[]>;
9
+ isActiveStep: Store<boolean>;
14
10
  }
15
11
  export declare function getStep(): StepContext;
16
12
  export declare function setStepContext(stepId: string): StepContext;
13
+ export {};
@@ -1 +1 @@
1
- import{getContext as g,setContext as C}from"svelte";import{combine as E}from"effector";import{derived as b}from"svelte/store";import{STEP_CONTEXT_KEY as f}from"../context.js";import{getStepRouter as h}from"../router/context.js";import{getExperience as w,getPicked as T}from"./client.js";function K(){return g(f)}function N(r){const l=w(),n=T(),{activeStep:s}=h(),m=s?b(s,e=>(e==null?void 0:e.id)===r):void 0,p=E({tree:l.treeStore,fields:n.fields.listStore,responses:n.responses.listStore},({tree:e,fields:c,responses:u})=>{const t=e.steps.find(o=>o.id===r);if(!t)throw new Error(`Step with id "${r}" not found in the experience tree.`);const x=t.fieldIds.map(o=>{var d;const a=c.find(i=>i.id===o),S=(d=u.find(i=>i.fieldId===o))==null?void 0:d.value;return{field:a,value:S}});return{step:{id:t.id,label:t.label},fields:x}});return C(f,{step:p.map(e=>e.step),fields:p.map(e=>e.fields),isActiveStep:m})}export{K as getStep,N as setStepContext};
1
+ import{getContext as v,setContext as E}from"svelte";import{createStore as h,combine as a,attach as w,createEffect as C,sample as l}from"effector";import{getStepFields as A}from"@picked/client";import{STEP_CONTEXT_KEY as d}from"../context.js";import{getStepRouter as F}from"../router/context.js";import{getExperience as T,getPicked as P}from"./client.js";function N(){return v(d)}function O(o){const u=T(),i=P(),{activeStep:m}=F(),n=m.map(e=>(e==null?void 0:e.id)===o),s=u.treeStore.map(e=>{const t=e.steps.find(r=>r.id===o);if(!t)throw new Error(`Step with id "${o}" not found in the experience tree.`);return t}),c=h([]),S=a({fields:c,responses:i.responses.listStore},({fields:e,responses:t})=>e.map(r=>{var f;const g=(f=t.find(x=>x.fieldId===r.id))==null?void 0:f.value;return{...r,value:g}})),p=w({source:a({step:s,fields:i.fields.listStore}),effect:C(async({step:{fieldIds:e},fields:t})=>await A(i,t.filter(r=>e.includes(r.id))))});return l({source:{isActiveStep:n},filter:({isActiveStep:e})=>e,target:p}),l({source:p.doneData,target:c}),E(d,{step:s,fields:S,isActiveStep:n})}export{N as getStep,O as setStepContext};
@@ -1 +1 @@
1
- import{createExperience as e}from"@picked/client/plugins/create-experience";import{adaptSvelteComponent as o}from"../../component.js";import t from"./DevToolsExperience.svelte";const p=e({id:"svelte-devtools",name:"Picked Devtools",decide:{},targets:[{selector:"main",placement:"append"}],type:"funnel",component:o(t)});p.addStep({label:"Tools"});export{p as devtoolsExperience};
1
+ import{createExperience as e}from"@picked/client/plugins/create-experience";import{adaptSvelteComponent as o}from"../../component.js";import t from"./DevToolsExperience.svelte";const p=e({id:"svelte-devtools",name:"Picked Devtools",decide:{},targets:[{selector:"main",placement:"append"}],component:o(t)});p.addStep({label:"Tools"});export{p as devtoolsExperience};
@@ -48,7 +48,7 @@
48
48
  if (node.type === 'origin') return 'Start'
49
49
  if (node.type === 'step' && node.stepId)
50
50
  return stepsMap.get(node.stepId)?.label ?? 'Unknown Step'
51
- if (node.type === 'conditional' && node.conditionalId) return `Condition'}`
51
+ if (node.type === 'conditional' && node.conditionalId) return `Condition`
52
52
  return 'Unknown Node'
53
53
  }
54
54
 
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
- import { writable } from 'svelte/store'
2
+ import { createEvent, createStore, sample } from 'effector'
3
+ import { supportsWindow } from '@picked/client'
3
4
  import { getExperience } from '../hooks'
4
5
  import { setStepRouter, type Step } from './context'
5
6
 
@@ -7,45 +8,58 @@
7
8
 
8
9
  const { currentStepStore } = experience
9
10
 
10
- const activeStep = writable<Step | null>(null)
11
+ const activeStepStore = createStore<Step | null>(null)
11
12
  let hasActiveStep = false
12
- const steps = writable<Step[]>([])
13
+ const stepsStore = createStore<Step[]>([])
14
+
15
+ const registerStepEvent = createEvent<Step>()
16
+ const unregisterStepEvent = createEvent<Step>()
17
+ const setActiveStepEvent = createEvent<Step | null>()
18
+
19
+ stepsStore.on(registerStepEvent, (state, step) => [...state, step])
20
+ stepsStore.on(unregisterStepEvent, (state, step) => state.filter((s) => s !== step))
21
+
22
+ sample({
23
+ source: setActiveStepEvent,
24
+ filter: (step) => step !== null,
25
+ target: activeStepStore,
26
+ })
13
27
 
14
28
  const registerStep = (step: Step) => {
15
29
  // todo: getIsSSR context
16
30
 
17
- if (typeof window === 'undefined') {
31
+ if (!supportsWindow()) {
18
32
  // In SSR we should set the activeStep immediately if it is a match.
19
33
  // If there are more Routes being registered after a match is found,
20
34
  // we just skip them.
21
35
  if (hasActiveStep) return
22
36
 
23
- const isMatchingStep = step.id === $currentStepStore?.id
37
+ const isMatchingStep = step.id === currentStepStore.getState()?.id
24
38
 
25
39
  if (isMatchingStep) {
26
- activeStep.set(step)
40
+ setActiveStepEvent(step)
27
41
  hasActiveStep = true
28
42
  }
29
43
  } else {
30
- steps.update((steps) => [...steps, step])
44
+ registerStepEvent(step)
31
45
  }
32
46
  }
33
47
 
34
48
  const unregisterStep = (step: Step) => {
35
- steps.update((st) => st.filter((s) => s !== step))
49
+ unregisterStepEvent(step)
36
50
  }
37
51
 
38
- $: {
39
- activeStep.set($steps.find((s) => s.id === $currentStepStore?.id) || null)
40
- }
52
+ sample({
53
+ source: { steps: stepsStore, currentStep: currentStepStore },
54
+ fn: ({ steps, currentStep }) => steps.find((s) => s.id === currentStep?.id) || null,
55
+ target: setActiveStepEvent,
56
+ })
41
57
 
42
58
  setStepRouter({
43
59
  registerStep,
44
60
  unregisterStep,
45
- activeStep,
61
+ activeStep: activeStepStore.map((val) => val),
46
62
  })
47
63
  </script>
48
64
 
49
- {#if $currentStepStore?.id}
50
- <slot />
51
- {/if}
65
+ <slot />
@@ -1,9 +1,9 @@
1
- import { Writable } from 'svelte/store';
1
+ import { Store } from 'effector';
2
2
  export interface Step {
3
3
  id: string;
4
4
  }
5
5
  interface ContextOptions {
6
- activeStep?: Writable<Step | null>;
6
+ activeStep: Store<Step | null>;
7
7
  unregisterStep: (step: Step) => void;
8
8
  registerStep: (step: Step) => void;
9
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@picked/svelte",
3
- "version": "0.0.1-dev.1756875690387",
3
+ "version": "0.0.1-dev.1762750479117",
4
4
  "dependencies": {
5
5
  "d3": "^7.9.0"
6
6
  },
@@ -18,51 +18,51 @@
18
18
  "svelte-check": "^4.2.2",
19
19
  "typedoc": "^0.28.5",
20
20
  "typedoc-plugin-markdown": "^4.6.3",
21
- "vite": "^6.3.5",
21
+ "vite": "^6.4.1",
22
22
  "vite-plugin-dts": "^4.5.4"
23
23
  },
24
24
  "exports": {
25
25
  ".": {
26
26
  "types": "./dist/index.d.ts",
27
27
  "import": "./dist/index.js",
28
- "default": "./dist/index.js",
29
- "svelte": "./dist/index.js"
28
+ "svelte": "./dist/index.js",
29
+ "default": "./dist/index.js"
30
30
  },
31
31
  "./server": {
32
32
  "types": "./dist/server/index.d.ts",
33
33
  "import": "./dist/server/index.js",
34
- "default": "./dist/server/index.js",
35
- "svelte": "./dist/server/index.js"
34
+ "svelte": "./dist/server/index.js",
35
+ "default": "./dist/server/index.js"
36
36
  },
37
37
  "./router": {
38
38
  "types": "./dist/router/index.d.ts",
39
39
  "import": "./dist/router/index.js",
40
- "default": "./dist/router/index.js",
41
- "svelte": "./dist/router/index.js"
40
+ "svelte": "./dist/router/index.js",
41
+ "default": "./dist/router/index.js"
42
42
  },
43
43
  "./form": {
44
44
  "types": "./dist/form/index.d.ts",
45
45
  "import": "./dist/form/index.js",
46
- "default": "./dist/form/index.js",
47
- "svelte": "./dist/form/index.js"
46
+ "svelte": "./dist/form/index.js",
47
+ "default": "./dist/form/index.js"
48
48
  },
49
49
  "./hooks": {
50
50
  "types": "./dist/hooks/index.d.ts",
51
51
  "import": "./dist/hooks/index.js",
52
- "default": "./dist/hooks/index.js",
53
- "svelte": "./dist/hooks/index.js"
52
+ "svelte": "./dist/hooks/index.js",
53
+ "default": "./dist/hooks/index.js"
54
54
  },
55
55
  "./plugins/framework": {
56
56
  "types": "./dist/plugins/framework/index.d.ts",
57
57
  "import": "./dist/plugins/framework/index.js",
58
- "default": "./dist/plugins/framework/index.js",
59
- "svelte": "./dist/plugins/framework/index.js"
58
+ "svelte": "./dist/plugins/framework/index.js",
59
+ "default": "./dist/plugins/framework/index.js"
60
60
  },
61
61
  "./plugins/devtools": {
62
62
  "types": "./dist/plugins/devtools/index.d.ts",
63
63
  "import": "./dist/plugins/devtools/index.js",
64
- "default": "./dist/plugins/devtools/index.js",
65
- "svelte": "./dist/plugins/devtools/index.js"
64
+ "svelte": "./dist/plugins/devtools/index.js",
65
+ "default": "./dist/plugins/devtools/index.js"
66
66
  }
67
67
  },
68
68
  "files": [
@@ -70,7 +70,7 @@
70
70
  ],
71
71
  "module": "./dist/index.js",
72
72
  "peerDependencies": {
73
- "@picked/client": "0.0.1-dev.1756875690387",
73
+ "@picked/client": "0.0.1-dev.1762750479117",
74
74
  "effector": "^23.3.0",
75
75
  "svelte": "^5.34.3"
76
76
  },