deep-jade
deep-jade5mo ago

Changing attributes

Can you show me how to add new attributes when creating a new company?
17 Replies
kapa.ai
kapa.ai5mo ago
Hi @Yasin1012. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To add new attributes when creating a new company using refine, you can modify the CreateCompany component by adding new Form.Item elements for each new attribute you want to include. Below is an example of how the CreateCompany component is structured, and you can add new fields similarly. Here's the existing structure of the CreateCompany component:
import { useApiUrl } from "@refinedev/core";
import { Modal, Form, Input, Grid, ModalProps, FormProps, Upload } from "antd";

import {
useStrapiUpload,
getValueProps,
mediaUploadMapper,
} from "@refinedev/strapi-v4";

import { TOKEN_KEY } from "../../constants";

type CreateCompanyProps = {
modalProps: ModalProps;
formProps: FormProps;
};

export const CreateCompany: React.FC<CreateCompanyProps> = ({
modalProps,
formProps,
}) => {
const breakpoint = Grid.useBreakpoint();
const { ...uploadProps } = useStrapiUpload({
maxCount: 1,
});
const API_URL = useApiUrl();

return (
<Modal
{...modalProps}
title="Create Company"
width={breakpoint.sm ? "600px" : "80%"}
>
<Form
{...formProps}
layout="vertical"
onFinish={(values) => {
console.log(values);
return formProps.onFinish?.({
...mediaUploadMapper(values),
});
}}
>
<Form.Item
label="Company Name"
name="name"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item label="Company Address" name="address">
<Input />
</Form.Item>
<Form.Item label="Company Country" name="country">
<Input />
</Form.Item>
<Form.Item label="Company City" name="city">
<Input />
</Form.Item>
<Form.Item
label="Email"
name="email"
rules={[
{
required: true,
type: "email",
},
]}
>
<Input />
</Form.Item>
<Form.Item label="Website" name="website">
<Input />
</Form.Item>
<Form.Item label="Company Logo">
<Form.Item
name={"logo"}
valuePropName="fileList"
getValueProps={(data) => getValueProps(data, API_URL)}
noStyle
rules={[
{
required: true,
},
]}
>
<Upload.Dragger
name="files"
action={`${API_URL}/upload`}
headers={{
Authorization: `Bearer ${localStorage.getItem(TOKEN_KEY)}`,
}}
listType="picture"
multiple
{...uploadProps}
>
<p className="ant-upload-text">Drag & drop a file in this area</p>
</Upload.Dragger>
</Form.Item>
</Form.Item>
</Form>
</Modal>
);
};
import { useApiUrl } from "@refinedev/core";
import { Modal, Form, Input, Grid, ModalProps, FormProps, Upload } from "antd";

import {
useStrapiUpload,
getValueProps,
mediaUploadMapper,
} from "@refinedev/strapi-v4";

import { TOKEN_KEY } from "../../constants";

type CreateCompanyProps = {
modalProps: ModalProps;
formProps: FormProps;
};

export const CreateCompany: React.FC<CreateCompanyProps> = ({
modalProps,
formProps,
}) => {
const breakpoint = Grid.useBreakpoint();
const { ...uploadProps } = useStrapiUpload({
maxCount: 1,
});
const API_URL = useApiUrl();

return (
<Modal
{...modalProps}
title="Create Company"
width={breakpoint.sm ? "600px" : "80%"}
>
<Form
{...formProps}
layout="vertical"
onFinish={(values) => {
console.log(values);
return formProps.onFinish?.({
...mediaUploadMapper(values),
});
}}
>
<Form.Item
label="Company Name"
name="name"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
<Form.Item label="Company Address" name="address">
<Input />
</Form.Item>
<Form.Item label="Company Country" name="country">
<Input />
</Form.Item>
<Form.Item label="Company City" name="city">
<Input />
</Form.Item>
<Form.Item
label="Email"
name="email"
rules={[
{
required: true,
type: "email",
},
]}
>
<Input />
</Form.Item>
<Form.Item label="Website" name="website">
<Input />
</Form.Item>
<Form.Item label="Company Logo">
<Form.Item
name={"logo"}
valuePropName="fileList"
getValueProps={(data) => getValueProps(data, API_URL)}
noStyle
rules={[
{
required: true,
},
]}
>
<Upload.Dragger
name="files"
action={`${API_URL}/upload`}
headers={{
Authorization: `Bearer ${localStorage.getItem(TOKEN_KEY)}`,
}}
listType="picture"
multiple
{...uploadProps}
>
<p className="ant-upload-text">Drag & drop a file in this area</p>
</Upload.Dragger>
</Form.Item>
</Form.Item>
</Form>
</Modal>
);
};
To add a new attribute, such as "Phone Number ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@866038216879374378> kapa.ai is still learning and improving, please let us know how it did by reacting below
deep-jade
deep-jadeOP5mo ago
@kapa.ai I want to create completly new attributes. Where and how do I do this
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To create completely new attributes for a company in your refine application, you need to follow these steps: 1. Update the Strapi Collection: First, you need to add the new attributes to your Strapi collection. This involves updating the company collection in Strapi to include the new fields you want. For example, if you want to add a "Phone Number" attribute, you would add a new field of type Text or Number in the Strapi admin panel. 2. Modify the Frontend Form: After updating the Strapi collection, you need to modify the CreateCompany component in your refine application to include form fields for the new attributes. You can add new Form.Item components for each new attribute. Here's an example of how you might add a "Phone Number" field to the CreateCompany component:
<Form.Item label="Phone Number" name="phoneNumber">
<Input />
</Form.Item>
<Form.Item label="Phone Number" name="phoneNumber">
<Input />
</Form.Item>
3. Ensure Data Mapping: Make sure that the new attributes are correctly mapped in the form submission logic. This involves ensuring that the form values are correctly sent to the backend when the form is submitted. You might need to update the onFinish function to handle the new attributes. 4. Test the Changes: Finally, test the changes to ensure that the new attributes are being saved correctly in the database and are displayed properly in the application. For more detailed guidance on working with Strapi collections, you can refer to the Strapi documentation (https://docs.strapi.io/developer-docs/latest/getting-started/quick-start.html). If you need further assistance, consider joining the Refine Discord community (https://discord.gg/refine) or checking out the GitHub Discussions (https://github.com/refinedev/refine/discussions) for more support. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@866038216879374378> kapa.ai is still learning and improving, please let us know how it did by reacting below
deep-jade
deep-jadeOP5mo ago
@kapa.ai How i do update the strapi Collection
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To update a Strapi collection to add new attributes, follow these steps: 1. Access the Strapi Admin Panel: - Navigate to your Strapi admin panel, typically found at <http://localhost:1337/admin> if you're running it locally. 2. Navigate to Content-Type Builder: - In the Strapi admin dashboard, go to the "Content-Types Builder" section. This is where you can manage your data models. 3. Select the Collection to Update: - Find and select the collection you want to update, such as the company collection. 4. Add New Fields: - Use the interface to add new fields to your collection. You can choose from various field types such as Text, Number, Email, Media, etc. - For example, to add a "Phone Number" field, you might choose a Text or Number field type and configure it as needed. 5. Save the Changes: - After adding the new fields, make sure to save your changes. Strapi will automatically update the database schema to reflect these changes. 6. Test the Changes: - Once the changes are saved, test them by creating or updating entries in the collection to ensure the new fields are working as expected. For more detailed instructions, you can refer to the Strapi Quick Start Guide (https://docs.strapi.io/dev-docs/quick-start#step-1-create-collection-types-with-the-content-type-builder) which provides comprehensive steps on using the Content-Type Builder. If you encounter any issues or need further assistance, consider checking out the Strapi documentation (https://docs.strapi.io/developer-docs/latest/getting-started/introduction.html) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@866038216879374378> kapa.ai is still learning and improving, please let us know how it did by reacting below
deep-jade
deep-jadeOP5mo ago
@kapa.ai i created my strapi acc but how do link it with the project
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To link your Strapi instance with your refine project, you need to set up the data provider in your refine application to communicate with your Strapi backend. Here’s how you can do it: 1. Install the Strapi Data Provider: - Ensure that you have the @refinedev/strapi-v4 data provider installed in your refine project. This package allows your refine app to interact with the Strapi API. 2. Configure the Data Provider: - In your App.tsx or main application file, import and configure the Strapi data provider. You need to provide the API URL of your Strapi instance.
import { Refine } from "@refinedev/core";
import { DataProvider } from "@refinedev/strapi-v4";
import axios from "axios";

const API_URL = "<http://localhost:1337";> // Replace with your Strapi instance URL

const axiosInstance = axios.create();

function App() {
return (
<Refine
dataProvider={DataProvider(`${API_URL}/api`, axiosInstance)}
// other props
/>
);
}

export default App;

import { Refine } from "@refinedev/core";
import { DataProvider } from "@refinedev/strapi-v4";
import axios from "axios";

const API_URL = "<http://localhost:1337";> // Replace with your Strapi instance URL

const axiosInstance = axios.create();

function App() {
return (
<Refine
dataProvider={DataProvider(`${API_URL}/api`, axiosInstance)}
// other props
/>
);
}

export default App;

3. Set Up Authentication (Optional): - If your Strapi instance requires authentication, ensure that your authProvider is set up correctly. You can use the AuthHelper from @refinedev/strapi-v4 to handle authentication.
import { AuthProvider } from "@refinedev/core";
import { AuthHelper } from "@refinedev/strapi-v4";

const strapiAuthHelper = AuthHelper(API_URL + "/api");
export const authProvider: AuthProvider = {
login: async ({ email, password }) => {
const { data, status } = await strapiAuthHelper.login(email, password);
if (status === 200) {
localStorage.setItem("TOKEN_KEY", data.jwt);
axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${data.jwt}`;
return { success: true, redirectTo: "/" };
}
return { success: false, error: new Error("Invalid username or password") };
},
// other methods
};

import { AuthProvider } from "@refinedev/core";
import { AuthHelper } from "@refinedev/strapi-v4";

const strapiAuthHelper = AuthHelper(API_URL + "/api");
export const authProvider: AuthProvider = {
login: async ({ email, password }) => {
const { data, status } = await strapiAuthHelper.login(email, password);
if (status === 200) {
localStorage.setItem("TOKEN_KEY", data.jwt);
axiosInstance.defaults.headers.common["Authorization"] = `Bearer ${data.jwt}`;
return { success: true, redirectTo: "/" };
}
return { success: false, error: new Error("Invalid username or password") };
},
// other methods
};

4. Test the Connection: - Run your refine application and ensure that it can successfully fetch data from your Strapi instance. You can test this by navigating to a page that lists resources from your Strapi backend. By following these steps, you should be able to link your Strapi instance with your refine project. For more detailed guidance, you can refer to the refine documentation (https://refine.dev/docs/examples/data-provider/strapi/) and the Strapi documentation (https://docs.strapi.io/developer-docs/latest/getting-started/introduction.html). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@866038216879374378> kapa.ai is still learning and improving, please let us know how it did by reacting below
dependent-tan
dependent-tan5mo ago
@kapa.ai @kapa.ai can you help fix these errors with Form.Item etc
src/pages/blog-posts/create.tsx:35:12 - error TS2769: No overload matches this call.
Overload 1 of 2, '(props: FormItemProps): FormItem', gave the following error.
Type '{ children: Element; name: string; valuePropName: string; getValueFromEvent: (event: UploadChangeParam<UploadFile<any>>) => UploadFile<any>[]; noStyle: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Overload 2 of 2, '(props: FormItemProps, context: any): FormItem', gave the following error.
Type '{ children: Element; name: string; valuePropName: string; getValueFromEvent: (event: UploadChangeParam<UploadFile<any>>) => UploadFile<any>[]; noStyle: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.

35 <Form.Item
~~~~~~~~~


src/pages/blog-posts/create.tsx:41:21 - error TS2339: Property 'Dragger' does not exist on type 'typeof Upload'.

41 <Upload.Dragger
~~~~~~~

src/pages/blog-posts/create.tsx:49:22 - error TS2339: Property 'Dragger' does not exist on type 'typeof Upload'.

49 </Upload.Dragger>
~~~~~~~
src/pages/blog-posts/create.tsx:35:12 - error TS2769: No overload matches this call.
Overload 1 of 2, '(props: FormItemProps): FormItem', gave the following error.
Type '{ children: Element; name: string; valuePropName: string; getValueFromEvent: (event: UploadChangeParam<UploadFile<any>>) => UploadFile<any>[]; noStyle: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Overload 2 of 2, '(props: FormItemProps, context: any): FormItem', gave the following error.
Type '{ children: Element; name: string; valuePropName: string; getValueFromEvent: (event: UploadChangeParam<UploadFile<any>>) => UploadFile<any>[]; noStyle: true; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.
Property 'children' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormItem> & Readonly<FormItemProps>'.

35 <Form.Item
~~~~~~~~~


src/pages/blog-posts/create.tsx:41:21 - error TS2339: Property 'Dragger' does not exist on type 'typeof Upload'.

41 <Upload.Dragger
~~~~~~~

src/pages/blog-posts/create.tsx:49:22 - error TS2339: Property 'Dragger' does not exist on type 'typeof Upload'.

49 </Upload.Dragger>
~~~~~~~
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The errors you're encountering seem to be related to the usage of Form.Item and Upload.Dragger in your code. Let's address each error: 1. Error with Form.Item: - The error message indicates that the children property is not recognized. This might be due to incorrect usage or missing properties in the Form.Item component. - Ensure that you are using the correct properties for Form.Item. The children should be passed directly as nested elements rather than as a property. Here's an example of how to correctly use Form.Item:
<Form.Item
name="image"
valuePropName="fileList"
getValueFromEvent={getValueFromEvent}
noStyle
>
<Upload.Dragger
name="file"
action={`${apiUrl}/media/upload`}
listType="picture"
maxCount={5}
multiple
>
<p className="ant-upload-text">Drag & drop a file in this area</p>
</Upload.Dragger>
</Form.Item>

<Form.Item
name="image"
valuePropName="fileList"
getValueFromEvent={getValueFromEvent}
noStyle
>
<Upload.Dragger
name="file"
action={`${apiUrl}/media/upload`}
listType="picture"
maxCount={5}
multiple
>
<p className="ant-upload-text">Drag & drop a file in this area</p>
</Upload.Dragger>
</Form.Item>

2. Error with Upload.Dragger: - The error indicates that Dragger is not recognized as a property of Upload. This might be due to an incorrect import or version mismatch. - Ensure that you have imported Upload correctly from antd and that your antd package is up to date. Make sure your import statement looks like this:
import { Upload } from "antd";

import { Upload } from "antd";

If Dragger is still not recognized, check your antd version and update it if necessary, as Dragger is a part of the Upload component in recent versions of antd. If these suggestions do not resolve the issue, consider checking the Ant Design documentation (https://ant.design/components/upload/) for the latest usage patterns and ensure your project dependencies are up to date. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1233073022878089296> kapa.ai is still learning and improving, please let us know how it did by reacting below
dependent-tan
dependent-tan5mo ago
@kapa.ai The Same Origin Policy disallows reading the remote resource at http://ec2 idk why
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The error you're encountering is related to the Same Origin Policy, which is a security feature implemented by web browsers to prevent scripts from making requests to a different domain than the one that served the web page. This is commonly encountered when trying to access resources hosted on a different domain or IP address, such as an AWS EC2 instance. To resolve this issue, you need to enable Cross-Origin Resource Sharing (CORS) on the server that hosts the resource you are trying to access. Here are some steps you can take: 1. Configure CORS on the Server: - If you have control over the server, configure it to include the appropriate CORS headers in its responses. This typically involves setting the Access-Control-Allow-Origin header to allow requests from your domain. 2. Using a Proxy: - If you cannot modify the server's CORS settings, consider using a proxy server that adds the necessary CORS headers to the response. 3. Check Strapi CORS Settings: - If you are using Strapi, you can configure CORS in the config/middleware.js file. Ensure that the strapi::cors middleware is configured to allow requests from your frontend's origin. Example configuration in Strapi:
module.exports = {
settings: {
cors: {
enabled: true,
origin: ['<http://your-frontend-domain.com'],> // Replace with your frontend domain
},
},
};

module.exports = {
settings: {
cors: {
enabled: true,
origin: ['<http://your-frontend-domain.com'],> // Replace with your frontend domain
},
},
};

4. Testing and Debugging: - After making changes, test your application to ensure that the CORS issue is resolved. You can use browser developer tools to inspect network requests and verify that the correct headers are being sent. For more detailed guidance, you can refer to the Strapi documentation on CORS (https://docs.strapi.io/developer-docs/latest/plugins/users-permissions.html#configurations) or consult the documentation for the server technology you are using. If you need further assistance, consider reaching out to the Refine community on Discord (https://discord.gg/refine) for support. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1233073022878089296> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?