Skip to content

Commit cc9df5a

Browse files
committed
feat: Composable utility for using @omnicajs/symfony-fouter
1 parent b29c9a8 commit cc9df5a

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import type {
2424
ContextStoreDefinition,
2525
} from '@retailcrm/embed-ui-v1-contexts/remote'
2626

27+
import type { Router } from '@omnicajs/symfony-router'
28+
2729
import type { Schema as CustomerCardSchema } from '@retailcrm/embed-ui-v1-contexts/types/customer/card'
2830
import type { Schema as CustomerCardPhoneSchema } from '@retailcrm/embed-ui-v1-contexts/types/customer/card-phone'
2931
import type { Schema as OrderCardSchema } from '@retailcrm/embed-ui-v1-contexts/types/order/card'
@@ -85,4 +87,6 @@ export declare const useCustomerCardContext: ContextStoreDefinition<'customer/ca
8587
export declare const useCustomerCardPhoneContext: ContextStoreDefinition<'customer/card:phone', CustomerCardPhoneSchema>
8688
export declare const useOrderCardContext: ContextStoreDefinition<'order/card', OrderCardSchema>
8789
export declare const useCurrentUserContext: ContextStoreDefinition<'user/current', CurrentUserSchema>
88-
export declare const useSettingsContext: ContextStoreDefinition<'settings', SettingsSchema>
90+
export declare const useSettingsContext: ContextStoreDefinition<'settings', SettingsSchema>
91+
92+
export declare const useRouter: () => ComputedRef<Pick<Router, 'generate'>>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"vue": "^3.5"
3636
},
3737
"dependencies": {
38+
"@omnicajs/symfony-router": "^1.0.0",
3839
"@omnicajs/vue-remote": "^0.2.5",
3940
"@remote-ui/rpc": "^1.4.5",
4041
"@retailcrm/embed-ui-v1-contexts": "^0.5.14",

src/composables.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Callable } from '~types/host/callable'
22

3+
import type { RouteParams } from '@omnicajs/symfony-router'
4+
35
import {
46
Context,
57
ContextAccessor,
@@ -17,14 +19,23 @@ import type { CustomContextStore } from '@retailcrm/embed-ui-v1-contexts/remote/
1719

1820
import type { Endpoint, RemoteCallable } from '@remote-ui/rpc'
1921

22+
import { Router } from '@omnicajs/symfony-router'
23+
2024
import type {
2125
ComputedRef,
2226
WritableComputedRef,
2327
} from 'vue'
2428

25-
import { computed } from 'vue'
29+
import {
30+
computed,
31+
shallowRef,
32+
watch,
33+
} from 'vue'
34+
2635
import { defineStore } from 'pinia'
2736

37+
import { useContext as useSettings } from '@retailcrm/embed-ui-v1-contexts/remote/settings'
38+
2839
type Computed<S extends ContextSchema, F extends keyof S> = IsReadonly<S[F]> extends true
2940
? ComputedRef<TypeOf<S[F]>>
3041
: WritableComputedRef<TypeOf<S[F]>>
@@ -98,3 +109,27 @@ export const useHost = (): RemoteCallable<Callable> => {
98109
},
99110
}
100111
}
112+
113+
export const useRouter = (): ComputedRef<Pick<Router, 'generate'>> => {
114+
const stringify = JSON.stringify
115+
const clone = <T>(pojo: T) => JSON.parse(stringify(pojo)) as T
116+
117+
const settings = useSettings()
118+
const routing = shallowRef(clone(settings['system.routing']))
119+
120+
watch(() => settings['system.routing'], (changed) => {
121+
if (stringify(changed) !== stringify(routing.value)) {
122+
routing.value = clone(changed)
123+
}
124+
})
125+
126+
return computed(() => {
127+
Router.setData(clone(routing.value))
128+
129+
return {
130+
generate: (name: string, params: RouteParams = {}, absolute = false) => {
131+
return Router.getInstance().generate(name, params, absolute)
132+
},
133+
}
134+
})
135+
}

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ __metadata:
10261026
dependencies:
10271027
"@eslint/eslintrc": "npm:^3.0.2"
10281028
"@eslint/js": "npm:^9.13.0"
1029+
"@omnicajs/symfony-router": "npm:^1.0.0"
10291030
"@omnicajs/vue-remote": "npm:^0.2.5"
10301031
"@remote-ui/rpc": "npm:^1.4.5"
10311032
"@retailcrm/embed-ui-v1-contexts": "npm:^0.5.14"

0 commit comments

Comments
 (0)