Untitled

 avatar
unknown
javascript
a year ago
1.4 kB
7
Indexable
const saveData = () => {
    if (
      productData.productName === '' ||
      productData.imagePath === '' ||
      !productData.category ||
      productData.description === '' ||
      productData.price === ''
    ) {
      Alert.alert('Please fill all your product information!');
    } else if (
      productData.instagram === '' &&
      productData.facebook === '' &&
      productData.phoneNumber === ''
    ) {
      Alert.alert('Please fill at least one seller contact!');
    } else {
      const allData = realm.objects('Product');
      const lastId = allData.length === 0 ? 0 : allData[allData.length - 1].id;
      realm.write(() => {
        realm.create('Product', {
          id: lastId + 1,
          productName: productData.productName,
          imagePath: productData.imagePath,
          category: productData.category,
          description: productData.description,
          price: parseInt(productData.price, 10),
          instagram: productData.instagram,
          facebook: productData.facebook,
          phoneNumber: productData.phoneNumber,
        });
      });
      Alert.alert('Saved!');
      setProductData({
        productName: '',
        imagePath: '',
        category: null,
        description: '',
        price: null,
        instagram: '',
        facebook: '',
        phoneNumber: '',
      });
      // dropdownRef.current.reset();
    }
  };
Editor is loading...
Leave a Comment