Skip to content

Commit

Permalink
HK-52 fix syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
xWyvernPx committed Oct 11, 2023
1 parent f5eb117 commit 6f4c909
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 568 deletions.
96 changes: 45 additions & 51 deletions src/base/hooks/useAwsS3.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
import { PutObjectCommand, PutObjectRequest, S3Client, S3ClientConfig } from '@aws-sdk/client-s3'
import React, { useMemo } from 'react'
import {
PutObjectCommand,
PutObjectRequest,
S3Client,
S3ClientConfig,
} from "@aws-sdk/client-s3";
import { useMemo } from "react";
import { v4 as uuidv4 } from "uuid";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const config:S3ClientConfig = {
credentials : {
accessKeyId : "AKIAWPKX6GSPLAZFCWG5",
secretAccessKey: "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"
},
region:"ap-southeast-1"
}
const config: S3ClientConfig = {
credentials: {
accessKeyId: "AKIAWPKX6GSPLAZFCWG5",
secretAccessKey: "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",
},
region: "ap-southeast-1",
};
type UploadParams = {
object: Blob | File
path : string
}

object: Blob | File;
path: string;
};

const useAwsS3 = () => {
/**
* ISSUE with s3 client : build version @aws-sdk/client-s3@3.427.0
* https://stackoverflow.com/questions/77229817/failed-to-fetch-aws
* https://github.com/aws/aws-sdk-js-v3/issues/5334
* downgrade to @aws-sdk/client-s3@3.317.0 for working
* */
const s3Client = useMemo(()=> new S3Client(config) , []);
const putObject = async({
object,
path
} : UploadParams)=> {
try {

const fileName = object instanceof File ? object.name : uuidv4();
const input: PutObjectRequest = {
Bucket: "momkitchen",
Key: path+fileName,
Body: object,

}
const command = new PutObjectCommand(input);
await s3Client.send(command);
// const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
// return url
const objectPath = `https://momkitchen.s3.amazonaws.com/${input.Key}`;
return objectPath;


} catch (error) {
console.error("Error s3 upload => ", error);
return null;
}

/**
* ISSUE with s3 client : build version @aws-sdk/client-s3@3.427.0
* https://stackoverflow.com/questions/77229817/failed-to-fetch-aws
* https://github.com/aws/aws-sdk-js-v3/issues/5334
* downgrade to @aws-sdk/client-s3@3.317.0 for working
* */
const s3Client = useMemo(() => new S3Client(config), []);
const putObject = async ({ object, path }: UploadParams) => {
try {
const fileName = object instanceof File ? object.name : uuidv4();
const input: PutObjectRequest = {
Bucket: "momkitchen",
Key: path + fileName,
Body: object,
};
const command = new PutObjectCommand(input);
await s3Client.send(command);
// const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
// return url
const objectPath = `https://momkitchen.s3.amazonaws.com/${input.Key}`;
return objectPath;
} catch (error) {
console.error("Error s3 upload => ", error);
return null;
}
return { putObject };
}

};
return { putObject };
};

export default useAwsS3
export default useAwsS3;
4 changes: 2 additions & 2 deletions src/data/@mk/mock/Customer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function generateRandomCustomer(): CustomerAdmin {
orders: [],
order_quantity: faker.datatype.number({ min: 1, max: 10 }), // Adjust the range as needed
spentMoney: faker.datatype.float({ min: 50, max: 1000, precision: 0.01 }), // Adjust the range as needed
selection: true,
// selection: true,
};
}

Expand Down Expand Up @@ -70,6 +70,6 @@ export const customer: CustomerAdmin[] = [
orders: [], // You can populate this array with Order objects
order_quantity: 5,
spentMoney: 500.0,
selection: true,
// selection: true,
},
];
79 changes: 33 additions & 46 deletions src/modules/customer/components/AddCustomerModal.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
import { mockRole } from "@/data/@mk/mock/Role";
import { User } from "@/types/@mk/entity/user";
import { CustomerStatus } from "@/types/@mk/enum/customerStatus";
import { CameraOutlined, DeleteFilled } from "@ant-design/icons";
import { DeleteFilled } from "@ant-design/icons";
import { yupResolver } from "@hookform/resolvers/yup";
import {
Button,
DialogActions,
DialogContent,
DialogTitle,
Divider,
FormControl,
FormControlLabel,
FormHelperText,
FormLabel,
Grid,
IconButton,
InputLabel,
ListItemText,
MenuItem,
OutlinedInput,
Select,
Switch,
TextField,
Tooltip,
Typography,
} from "@mui/material";
import { Box, Stack, useTheme } from "@mui/system";
import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import Avatar from "@ui/@extended/Avatar";
import { MuiTelInput, matchIsValidTel } from "mui-tel-input";
import { ChangeEvent, ReactNode, useEffect, useState } from "react";
import { Controller, FormProvider, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { Box, Stack } from "@mui/system";
import { useEffect } from "react";
import { FormProvider, useForm } from "react-hook-form";
import useCustomerForm, {
ManipulateCustomerForm,
} from "../hook/useCustomerForm";
import CustomerManipulateForm from "./form/CustomerManipulateForm";
import useCustomerForm, { ManipulateCustomerForm } from "../hook/useCustomerForm";
import { mockRole } from "@/data/@mk/mock/Role";

interface AddCustomerModalProps {
isOpen?: boolean;
Expand All @@ -42,24 +27,26 @@ interface AddCustomerModalProps {
customer: User;
}


const AddCustomerModal = ({
customer,
onCancel,
}: // isOpen,
AddCustomerModalProps) => {
const isCreating = !customer;
const defaultValue:ManipulateCustomerForm = !isCreating ? {
autoPassword: true,
birthday: customer?.birthday,
email: customer?.email,
phone: customer?.phone,
fullname: customer?.fullName,
status: customer?.customer.status,
role: customer?.roleId
}: {autoPassword: true}
const defaultValue: ManipulateCustomerForm = !isCreating
? {
autoPassword: true,
birthday: customer?.birthday,
email: customer?.email,
phone: customer?.phone,
fullname: customer?.fullName,
status: customer?.customer.status,
role: customer?.roleId,
}
: { autoPassword: true };
console.log(defaultValue);

const {CustomerSchema, createCustomerHandler} = useCustomerForm()
const { CustomerSchema, createCustomerHandler } = useCustomerForm();
const deleteHandler = () => {
// dispatch(deleteCustomer(customer?.id)); - delete
// dispatch(
Expand Down Expand Up @@ -126,32 +113,32 @@ AddCustomerModalProps) => {
const methods = useForm<ManipulateCustomerForm>({
mode: "all",
resolver: yupResolver(CustomerSchema),
defaultValues:{
autoPassword:true
}
defaultValues: {
autoPassword: true,
},
});

const roles = mockRole; // TODO: load from be
useEffect(()=>{
console.log("Error =>",methods.formState?.errors);

},[methods.formState.errors])
useEffect(() => {
console.log("Error =>", methods.formState?.errors);
}, [methods.formState.errors]);

return (
<>
<FormProvider {...methods}>
<FormProvider {...methods}>
<Box
component={"form"}
onSubmit={methods.handleSubmit( async (data) => {
onSubmit={methods.handleSubmit(async (data) => {
console.log("Add customer data => ", data);
const res = await createCustomerHandler(data);
console.log(res);
})}>
<DialogTitle>
{customer ? "Edit Customer" : "New Customer"}
</DialogTitle>
<Divider />
<DialogContent sx={{ p: 2.5 }}>
<CustomerManipulateForm roles={roles} isCreating={isCreating}/>
<CustomerManipulateForm roles={roles} isCreating={isCreating} />
</DialogContent>
<Divider />
<DialogActions sx={{ p: 2.5 }}>
Expand Down Expand Up @@ -192,7 +179,7 @@ AddCustomerModalProps) => {
</Grid>
</DialogActions>
</Box>
</FormProvider>
</FormProvider>
</>
);
};
Expand Down
Loading

0 comments on commit 6f4c909

Please sign in to comment.