@@ -36,78 +36,42 @@ export function useWebsite(id: string) {
3636export function useCreateWebsite ( ) {
3737 const utils = trpc . useUtils ( ) ;
3838 return trpc . websites . create . useMutation ( {
39- onMutate : async ( newWebsite ) => {
39+ onSuccess : ( newWebsite , variables ) => {
4040 const queryKey = {
41- organizationId : newWebsite . organizationId ?? undefined ,
41+ organizationId : variables . organizationId ?? undefined ,
4242 } ;
43- await utils . websites . list . cancel ( queryKey ) ;
44- const previousWebsites = utils . websites . list . getData ( queryKey ) ;
45-
43+
4644 utils . websites . list . setData ( queryKey , ( old ) => {
47- const optimisticWebsite = {
48- ...newWebsite ,
49- id : crypto . randomUUID ( ) ,
50- createdAt : new Date ( ) . toISOString ( ) ,
51- } as Website ;
52- return old ? [ ...old , optimisticWebsite ] : [ optimisticWebsite ] ;
45+ if ( ! old ) return [ newWebsite ] ;
46+ const exists = old . some ( w => w . id === newWebsite . id ) ;
47+ return exists ? old : [ ...old , newWebsite ] ;
5348 } ) ;
54-
55- return { previousWebsites, queryKey } ;
56- } ,
57- onError : ( _ , __ , context ) => {
58- if ( context ?. previousWebsites ) {
59- utils . websites . list . setData ( context . queryKey , context . previousWebsites ) ;
60- }
6149 } ,
62- onSettled : ( _ , __ , ___ , context ) => {
63- utils . websites . list . invalidate ( context ?. queryKey ) ;
50+ onError : ( error ) => {
51+ console . error ( 'Failed to create website:' , error ) ;
6452 } ,
6553 } ) ;
6654}
6755
6856export function useUpdateWebsite ( ) {
6957 const utils = trpc . useUtils ( ) ;
7058 return trpc . websites . update . useMutation ( {
71- onMutate : async ( updatedWebsite ) => {
59+ onSuccess : ( updatedWebsite ) => {
7260 const getByIdKey = { id : updatedWebsite . id } ;
73- await utils . websites . getById . cancel ( getByIdKey ) ;
74- const previousWebsite = utils . websites . getById . getData ( getByIdKey ) ;
75-
7661 const listKey = {
77- organizationId : previousWebsite ? .organizationId ?? undefined ,
62+ organizationId : updatedWebsite . organizationId ?? undefined ,
7863 } ;
79- await utils . websites . list . cancel ( listKey ) ;
80- const previousWebsites = utils . websites . list . getData ( listKey ) ;
8164
8265 utils . websites . list . setData ( listKey , ( old ) =>
8366 old ?. map ( ( website ) =>
84- website . id === updatedWebsite . id
85- ? { ...website , ...updatedWebsite }
86- : website
87- )
88- ) ;
89- utils . websites . getById . setData ( getByIdKey , ( old ) =>
90- old ? { ...old , ...updatedWebsite } : undefined
67+ website . id === updatedWebsite . id ? updatedWebsite : website
68+ ) ?? [ ]
9169 ) ;
92-
93- return { previousWebsites, previousWebsite, listKey } ;
94- } ,
95- onError : ( _ , updatedWebsite , context ) => {
96- if ( context ?. previousWebsites && context . listKey ) {
97- utils . websites . list . setData ( context . listKey , context . previousWebsites ) ;
98- }
99- if ( context ?. previousWebsite ) {
100- utils . websites . getById . setData (
101- { id : updatedWebsite . id } ,
102- context . previousWebsite
103- ) ;
104- }
70+
71+ utils . websites . getById . setData ( getByIdKey , updatedWebsite ) ;
10572 } ,
106- onSettled : ( data , __ , ___ , context ) => {
107- utils . websites . list . invalidate ( context ?. listKey ) ;
108- if ( data ) {
109- utils . websites . getById . invalidate ( { id : data . id } ) ;
110- }
73+ onError : ( error ) => {
74+ console . error ( 'Failed to update website:' , error ) ;
11175 } ,
11276 } ) ;
11377}
@@ -117,34 +81,29 @@ export function useDeleteWebsite() {
11781 return trpc . websites . delete . useMutation ( {
11882 onMutate : async ( { id } ) => {
11983 const getByIdKey = { id } ;
120- await utils . websites . getById . cancel ( getByIdKey ) ;
12184 const previousWebsite = utils . websites . getById . getData ( getByIdKey ) ;
12285
12386 const listKey = {
12487 organizationId : previousWebsite ?. organizationId ?? undefined ,
12588 } ;
89+
12690 await utils . websites . list . cancel ( listKey ) ;
12791 const previousWebsites = utils . websites . list . getData ( listKey ) ;
12892
12993 utils . websites . list . setData (
13094 listKey ,
13195 ( old ) => old ?. filter ( ( w ) => w . id !== id ) ?? [ ]
13296 ) ;
133- utils . websites . getById . setData ( getByIdKey , undefined ) ;
13497
135- return { previousWebsites, previousWebsite , listKey } ;
98+ return { previousWebsites, listKey } ;
13699 } ,
137- onError : ( _ , { id } , context ) => {
100+ onError : ( _ , __ , context ) => {
138101 if ( context ?. previousWebsites && context . listKey ) {
139102 utils . websites . list . setData ( context . listKey , context . previousWebsites ) ;
140103 }
141- if ( context ?. previousWebsite ) {
142- utils . websites . getById . setData ( { id } , context . previousWebsite ) ;
143- }
144104 } ,
145- onSettled : ( _ , __ , { id } , context ) => {
146- utils . websites . list . invalidate ( context ?. listKey ) ;
147- utils . websites . getById . invalidate ( { id } ) ;
105+ onSuccess : ( _ , { id } ) => {
106+ utils . websites . getById . setData ( { id } , undefined ) ;
148107 } ,
149108 } ) ;
150109}
0 commit comments