@reduxjs/toolkit#createAsyncThunk JavaScript Examples

The following examples show how to use @reduxjs/toolkit#createAsyncThunk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createQueue.js    From simplQ-frontend with GNU General Public License v3.0 6 votes vote down vote up
useCreateQueue = () => {
  const makeAuthedRequest = useMakeAuthedRequest();
  const history = useHistory();

  const createQueue = createAsyncThunk(typePrefix, async ({ queueName }) => {
    const authedRequest = makeAuthedRequest(RequestFactory.createQueue(queueName));
    const response = await authedRequest;
    if (response) {
      history.push(`/queue/${response.queueId}`);
    }
    return response;
  });

  return createQueue;
}
Example #2
Source File: categorySlice.js    From community-forum-frontend with GNU General Public License v3.0 6 votes vote down vote up
addCategory = createAsyncThunk(
  "category/add",
  async (addCategoryData, { rejectWithValue }) => {
    const tokenHeader = `Bearer ${localStorage.getItem("token")}`;
    const response = await axios
      .post(
        process.env.REACT_APP_GRAPHQL_API_ENDPOINT,
        {
          query: `mutation{ createCategory(
            categoryInput: {
              name: "${addCategoryData.name}"
              description: "${addCategoryData.description}"
            }
        ) {
          _id
        }}`,
        },
        {
          headers: {
            "Content-Type": "application/json",
            Authorization: tokenHeader,
          },
        }
      )
      .catch((error) => {
        if (error.response) {
          return error.response.data.errors[0].message;
        }
      });
    if (response.data != undefined) {
      return response.data.data.createCategory;
    }
    return rejectWithValue(response);
  }
)
Example #3
Source File: likedVideo.js    From youtubeclone-frontend with MIT License 6 votes vote down vote up
getLikedVideos = createAsyncThunk(
  "likedVideo/getLikedVideos",
  async () => {
    const { data } = await client(
      `${process.env.REACT_APP_BE}/users/likedVideos`
    );
    return data;
  }
)
Example #4
Source File: postsSlice.js    From tclone with MIT License 6 votes vote down vote up
getFeed = createAsyncThunk('posts/getFeed', async (_, { dispatch, getState }) => {
    try {
        let {
            posts: { feed_page: p },
        } = getState()
        let url = `/api/home_timeline?p=${p + 1}`
        let data = await request(url, { dispatch })
        let posts = data.posts || []
        posts = posts.filter(Boolean).map(post => ({ ...post, is_feed_post: true }))
        dispatch(parsePosts(posts))
        return posts.length
    } catch (err) {
        console.log(err)
        throw err
    }
})
Example #5
Source File: getArticle.js    From genshin with MIT License 5 votes vote down vote up
getArticle = createAsyncThunk(
  'article/get',
  async (slug) => {
    const response = await fetch(`https://genshin.cchampou.me/articles?slug=${slug}`);
    return response.json();
  },
)
Example #6
Source File: createQueue.js    From simplQ-frontend with GNU General Public License v3.0 5 votes vote down vote up
createQueue = createAsyncThunk(typePrefix)
Example #7
Source File: authSlice.js    From community-forum-frontend with GNU General Public License v3.0 5 votes vote down vote up
login = createAsyncThunk(
  'auth/login', 
  async (loginData, { rejectWithValue }) => {
  const tokenHeader = `Bearer ${localStorage.getItem("token")}`;
  const response = await axios
    .post(
      process.env.REACT_APP_GRAPHQL_API_ENDPOINT,
      {
        query: `query{ login(
          email: "${loginData.email}"
          password: "${loginData.password}"
        ) {
        _id
        name {
          firstName
          lastName
        }
        email
        phone
        info {
          about {
            shortDescription
            designation
          }
        }
        socialMedia {
          twitter
        }
        token
        isFirstAdmin
        isAdmin
        isModerator
        }}`,
      },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: tokenHeader,
        },
      }
    )
    .catch((error) => {
      if (error.response) {
        return error.response.data.errors[0].message;
      }
    });
  if(response.data != undefined) {
    localStorage.setItem("token", response.data.data.login.token);
    return response.data.data.login;
  }
  return rejectWithValue(response)
})
Example #8
Source File: channelRecommendation.js    From youtubeclone-frontend with MIT License 5 votes vote down vote up
getChannels = createAsyncThunk(
  "channelRecommendation/getChannels",
  async () => {
    const { data } = await client(`${process.env.REACT_APP_BE}/users`);
    return data;
  }
)
Example #9
Source File: notifySlice.js    From tclone with MIT License 5 votes vote down vote up
_fetchNotifs = createAsyncThunk('notifs/fetchAll', async (_, { dispatch }) => {
    let { notifications } = await request('/api/notifications', { dispatch })
    if (!notifications) throw Error('No notifications')
    return dispatch(notificationsAdded(notifications))
})