11// No need to import React with modern JSX transform
22import { Button } from "../components/ui/button" ;
33import { AccountCard } from "../components/account-card" ;
4- import { client } from "../lib/amplify-client" ;
4+ import { client } from "../lib/amplify-ssr- client" ;
55import { useNavigate } from "react-router" ;
66import { parse } from "csv-parse/browser/esm" ;
77import { CSVImportDialog } from "../components/csv-import-dialog" ;
88import type { Route } from "./+types/accounts" ;
9+ import { runWithAmplifyServerContext } from "~/lib/amplifyServerUtils" ;
910
1011export function meta ( ) {
1112 return [
@@ -14,7 +15,7 @@ export function meta() {
1415 ] ;
1516}
1617
17- export async function clientAction ( { request } : { request : Request } ) {
18+ export async function action ( { request } : { request : Request } ) {
1819 const formData = await request . formData ( ) ;
1920 const csvFile = formData . get ( "csvFile" ) as File ;
2021
@@ -66,27 +67,33 @@ export async function clientAction({ request }: { request: Request }) {
6667 continue ;
6768 }
6869
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+ } ) ;
9097 }
9198
9299 return {
@@ -99,17 +106,23 @@ export async function clientAction({ request }: { request: Request }) {
99106 }
100107}
101108
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+ } ) ;
113126}
114127
115128export default function Accounts ( {
0 commit comments