recoil#selector TypeScript Examples

The following examples show how to use recoil#selector. 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: authContext.ts    From frames with Mozilla Public License 2.0 6 votes vote down vote up
AuthContextHandler = selector<{ fade?: boolean, process?: Process, error?: string | null }>({
    key: 'AuthContextHandler',
    get: ({get}) => {
        const errors = get(AuthErrors);
        const error = get(AuthContextErrorAtom);
        let newError: string = '';

        if (errors.emailError)
            newError = 'enter a valid email address';

        else if (errors.passError)
            newError = 'Password must contain a number, a capital and a small letter';

        else if (errors.authError)
            newError = 'invalid auth key';

        return {
            fade: error ? false : get(AuthFade),
            process: get(AuthContextProcessAtom),
            error: newError === '' ? error : newError
        }
    }, set: ({set, get}, newValue) => {
        const auth = get(Authenticated);
        if (!(newValue instanceof DefaultValue)) {
            newValue.process && NProgress.start();
            if (newValue.process && auth)
                set(AuthContextProcessAtom, newValue.process);

            if (newValue.error !== undefined)
                set(AuthContextErrorAtom, newValue.error);

            if (newValue.fade !== undefined && auth) {
                set(AuthFade, newValue.fade);
                NProgress.done();
            }
        }
    }
})
Example #2
Source File: contactSelectors.ts    From react-tutorials with MIT License 6 votes vote down vote up
sendEmail = selector({
  key: 'sendEmailSelector',
  get: async ({ get }) => {
    const payload = get(contactState)
    try {
      let urlWithString =
        `http://localhost:8081/contact_us?email=` +
        payload.email +
        `&customerName=` +
        payload.name +
        `&message=` +
        payload.message
      const res = await axios({
        url: urlWithString,
        method: 'get',
      })
      const status = `${res.data.status}`
      console.log('API :: sendEmail :: results: ' + JSON.stringify(status))
      return res?.data?.status
    } catch (err) {
      console.warn(err)
      return `Error: ` + err
    }
  },
})
Example #3
Source File: cars.state.ts    From mojito_pdm with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
CarState = {
    rawCars: atom<Car[]>({
        key: 'cars',
        default: cars
    }),
    categorySearch: atom<string>({
        key: 'categorySearch',
        default: ''
    }),
    filteredCars: selector<Car[]>({
        key: 'filteredCars',
        get: ({get}) => {
            const cars: Car[] = get(CarState.rawCars)
            const searchCategory: string = get(CarState.categorySearch).trim()

            return !searchCategory ? cars : [...cars].filter(item => item.category === searchCategory)
        }
    })
}
Example #4
Source File: mod-categories-state.ts    From ow-mod-manager with MIT License 6 votes vote down vote up
enabledModList = selector({
  key: 'EnabledMods',
  get: ({ get }) =>
    get(filteredModList).filter(
      (mod) =>
        (mod.localVersion && mod.isEnabled) ||
        get(modIsLoadingState(mod.uniqueName))
    ),
})
Example #5
Source File: auth.ts    From abrechnung with GNU Affero General Public License v3.0 6 votes vote down vote up
userData = atom<User>({
    key: "userData",
    default: selector({
        key: "userData/default",
        get: async ({ get }) => {
            try {
                return await fetchProfile();
            } catch (err) {
                return null;
            }
        },
    }),
    effects_UNSTABLE: [
        ({ setSelf, trigger }) => {
            const userID = getUserIDFromToken();
            if (userID !== null) {
                ws.subscribe("user", userID, (subscriptionType, { subscription_type, element_id }) => {
                    if (subscription_type === "user" && element_id === userID) {
                        fetchProfile().then((result) => {
                            setSelf(result);
                        });
                    }
                });
                // TODO: handle registration errors

                return () => {
                    ws.unsubscribe("user", userID);
                };
            }
        },
    ],
})
Example #6
Source File: bindings.tsx    From arduino-web-oscilloscope with MIT License 6 votes vote down vote up
voltagesState = selector({
  key: 'voltages',
  get: ({ get }) => {
    const signal = get(dataState)[0].map(({ v }) => v)
    const vmax = Math.max(...signal)
    const vmin = Math.min(...signal)
    const vpp = vmax - vmin
    const vavr = sum(signal) / signal.length

    return {
      vavr,
      vpp,
      vmin,
      vmax
    }
  }
})
Example #7
Source File: analysisStatusGlobal.state.ts    From nextclade with MIT License 6 votes vote down vote up
canRunAtom = selector({
  key: 'canRun',
  get({ get }) {
    const status = get(analysisStatusGlobalAtom)
    return (
      status === AlgorithmGlobalStatus.idle ||
      status === AlgorithmGlobalStatus.done ||
      status === AlgorithmGlobalStatus.failed
    )
  },
})
Example #8
Source File: selectors.ts    From phosphor-home with MIT License 6 votes vote down vote up
filteredQueryResultsSelector = selector<ReadonlyArray<IconEntry>>({
  key: "filteredQueryResultsSelector",
  get: ({ get }) => {
    const query = get(searchQueryAtom).trim().toLowerCase();
    if (!query) return icons;

    return new Promise((resolve) =>
      resolve(fuse.search(query).map((value) => value.item))
    );
  },
})
Example #9
Source File: editPlaylist.tsx    From frames with Mozilla Public License 2.0 5 votes vote down vote up
playlistVideoSelector = selector({
    key: 'playlistVideoSelector',
    get: ({get}) => {
        const playlist = get(PlaylistAtom);
        const videos = get(PlaylistVideoAtom);
        return playlist.videos.concat(videos);
    }
})
Example #10
Source File: forceGraphSelectors.ts    From react-tutorials with MIT License 5 votes vote down vote up
getPowerChartData = selector({
  key: 'GetPowerChartData',
  get: () => {
    return getDataFromAPI()
  },
})
Example #11
Source File: mod-categories-state.ts    From ow-mod-manager with MIT License 5 votes vote down vote up
nonAddonModList = selector({
  key: 'NonAddonMods',
  get: ({ get }) => get(filteredModList).filter((mod) => !mod.parent),
})
Example #12
Source File: auth.ts    From abrechnung with GNU Affero General Public License v3.0 5 votes vote down vote up
isGuestUser = selector<boolean>({
    key: "isGuestUser",
    get: async ({ get }) => {
        const user = get(userData);
        return user.is_guest_user;
    },
})
Example #13
Source File: bindings.tsx    From arduino-web-oscilloscope with MIT License 5 votes vote down vote up
use50percentTriggerVoltage = selector({
  key: '50% trigger voltage',
  get: () => false,
  set: ({ set, get }) => {
    const { vavr } = get(voltagesState)
    set(useTriggerVoltage.send, vavr)
  }
})
Example #14
Source File: analysisStatusGlobal.state.ts    From nextclade with MIT License 5 votes vote down vote up
hasRanAtom = selector({
  key: 'hasRan',
  get({ get }) {
    const status = get(analysisStatusGlobalAtom)
    return status !== AlgorithmGlobalStatus.idle
  },
})