1
1
// No need to import React with modern JSX transform
2
2
import { Button } from "../components/ui/button" ;
3
3
import { AccountCard } from "../components/account-card" ;
4
- import { client } from "../lib/amplify-client" ;
4
+ import { client } from "../lib/amplify-ssr- client" ;
5
5
import { useNavigate } from "react-router" ;
6
6
import { parse } from "csv-parse/browser/esm" ;
7
7
import { CSVImportDialog } from "../components/csv-import-dialog" ;
8
8
import type { Route } from "./+types/accounts" ;
9
+ import { runWithAmplifyServerContext } from "~/lib/amplifyServerUtils" ;
9
10
10
11
export function meta ( ) {
11
12
return [
@@ -14,7 +15,7 @@ export function meta() {
14
15
] ;
15
16
}
16
17
17
- export async function clientAction ( { request } : { request : Request } ) {
18
+ export async function action ( { request } : { request : Request } ) {
18
19
const formData = await request . formData ( ) ;
19
20
const csvFile = formData . get ( "csvFile" ) as File ;
20
21
@@ -66,27 +67,33 @@ export async function clientAction({ request }: { request: Request }) {
66
67
continue ;
67
68
}
68
69
69
- try {
70
- const { errors } = await client . models . Account . create ( {
71
- name : record . name ,
72
- email : record . email ,
73
- photo : record . photo || undefined ,
74
- organizationLine : record . organizationLine ,
75
- residence : record . residence ,
76
- } ) ;
77
-
78
- if ( errors ) {
79
- results . errors . push (
80
- `行 ${ i + 1 } : ${ errors . map ( ( err ) => err . message ) . join ( ", " ) } ` ,
81
- ) ;
82
- } else {
83
- results . success ++ ;
84
- }
85
- } catch ( error ) {
86
- results . errors . push (
87
- `行 ${ i + 1 } : ${ error instanceof Error ? error . message : "不明なエラー" } ` ,
88
- ) ;
89
- }
70
+ const responseHeaders = new Headers ( ) ;
71
+ await runWithAmplifyServerContext ( {
72
+ serverContext : { request, responseHeaders } ,
73
+ operation : async ( contextSpec ) => {
74
+ try {
75
+ const { errors } = await client . models . Account . create ( contextSpec , {
76
+ name : record . name ,
77
+ email : record . email ,
78
+ photo : record . photo || undefined ,
79
+ organizationLine : record . organizationLine ,
80
+ residence : record . residence ,
81
+ } ) ;
82
+
83
+ if ( errors ) {
84
+ results . errors . push (
85
+ `行 ${ i + 1 } : ${ errors . map ( ( err ) => err . message ) . join ( ", " ) } ` ,
86
+ ) ;
87
+ } else {
88
+ results . success ++ ;
89
+ }
90
+ } catch ( error ) {
91
+ results . errors . push (
92
+ `行 ${ i + 1 } : ${ error instanceof Error ? error . message : "不明なエラー" } ` ,
93
+ ) ;
94
+ }
95
+ } ,
96
+ } ) ;
90
97
}
91
98
92
99
return {
@@ -99,17 +106,23 @@ export async function clientAction({ request }: { request: Request }) {
99
106
}
100
107
}
101
108
102
- export async function clientLoader ( ) {
103
- try {
104
- const { data } = await client . models . Account . list ( ) ;
105
- return { accounts : data } ;
106
- } catch ( err ) {
107
- console . error ( "Error fetching accounts:" , err ) ;
108
- return {
109
- accounts : [ ] ,
110
- error : err instanceof Error ? err . message : "Unknown error occurred" ,
111
- } ;
112
- }
109
+ export async function loader ( { request } : Route . LoaderArgs ) {
110
+ const responseHeaders = new Headers ( ) ;
111
+ return runWithAmplifyServerContext ( {
112
+ serverContext : { request, responseHeaders } ,
113
+ operation : async ( contextSpec ) => {
114
+ try {
115
+ const { data } = await client . models . Account . list ( contextSpec ) ;
116
+ return { accounts : data } ;
117
+ } catch ( err ) {
118
+ console . error ( "Error fetching accounts:" , err ) ;
119
+ return {
120
+ accounts : [ ] ,
121
+ error : err instanceof Error ? err . message : "Unknown error occurred" ,
122
+ } ;
123
+ }
124
+ } ,
125
+ } ) ;
113
126
}
114
127
115
128
export default function Accounts ( {
0 commit comments