Untitled

mail@pastecode.io avatar
unknown
javascript
a month ago
1.4 kB
5
Indexable
Never
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();
    }
  };
Leave a Comment